Skip to content

Commit 6bd9e7d

Browse files
committed
Merge pull request #84 from uProxy/dborkan-setup
Emit socksToRtcSuccess or socksToRtcFailure after freedom.transport.setup succeeds or fails.
2 parents 7f01a80 + 90f74a0 commit 6bd9e7d

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "socks-rtc",
33
"description": "Library for proxying SOCKS5 over WebRTC",
4-
"version": "0.1.8",
4+
"version": "0.1.9",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/uProxy/socks-rtc"

src/chrome-app/socks_to_rtc_to_net.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ socksToRtc.on('sendSignalToPeer', function(signal) {
3232
// If all goes correctly, the rtcToNet will fire a 'sendSignalToPeer'.
3333
});
3434

35+
// Listen for socksToRtc success or failure signals, and just print them for now.
36+
socksToRtc.on('socksToRtcSuccess', function(peerId) {
37+
console.log('Received socksToRtcSuccess for peerId ' + JSON.stringify(peerId));
38+
});
39+
40+
socksToRtc.on('socksToRtcFailure', function(peerId) {
41+
console.error('Received socksToRtcFailure for peerId ' + JSON.stringify(peerId));
42+
});
43+
3544
// Server tells socksToRtc about itself.
3645
rtcToNet.on('sendSignalToPeer', function(signal) {
3746
console.log(' * RTC-NET signaling SOCKS-RTC.'); // + JSON.stringify(signal));

src/interfaces/communications.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ declare module Channel {
66
NET_CONNECT_REQUEST = 1,
77
NET_CONNECT_RESPONSE = 2,
88
NET_DISCONNECTED = 3,
9-
SOCKS_DISCONNECTED = 4
9+
SOCKS_DISCONNECTED = 4,
10+
HELLO = 5
1011
}
1112

1213
// "Top-level" message for the control channel.

src/rtc-to-net/rtc-to-net.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ module RtcToNet {
3838
this.transport = freedom['transport']();
3939
this.transport.on('onData', this.passPeerDataToNet_);
4040
this.transport.on('onClose', this.closeNetClient_);
41-
this.transport.setup('RtcToNet-' + peerId, channel.identifier);
41+
this.transport.setup('RtcToNet-' + peerId, channel.identifier).then(
42+
// TODO: emit signals when peer-to-peer connections are setup or fail.
43+
() => { dbg('RtcToNet transport.setup succeeded'); },
44+
(e) => { dbgErr('RtcToNet transport.setup failed ' + e); }
45+
);
4246
this.signallingChannel = channel.channel;
4347
this.signallingChannel.on('message', (msg) => {
4448
freedom.emit('sendSignalToPeer', {
@@ -116,9 +120,13 @@ module RtcToNet {
116120
this.transport.send('control',
117121
ArrayBuffers.stringToArrayBuffer(JSON.stringify(out)));
118122
});
123+
} else if (command.type === Channel.COMMANDS.HELLO) {
124+
// Hello command is used to establish communication from socks-to-rtc,
125+
// just ignore it.
126+
dbg('received hello.');
119127
} else {
120128
// TODO: support SocksDisconnected command
121-
dbgWarn('unsupported control command: ' + command.type);
129+
dbgWarn('unsupported control command: ' + JSON.stringify(command));
122130
}
123131
} else {
124132
dbg(message.tag + ' <--- received ' + JSON.stringify(message));

src/socks-to-rtc/socks-to-rtc.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ module SocksToRTC {
3838

3939
/**
4040
* Start the Peer, based on the remote peer's info.
41+
* This will emit a socksToRtcSuccess signal when the peer connection is esablished,
42+
* or a socksToRtcFailure signal if there is an error openeing the peer connection.
43+
* TODO: update this to return a promise that fulfills/rejects, after freedom v0.5
44+
* is ready.
4145
*/
4246
public start = (remotePeer:PeerInfo) => {
4347
this.reset(); // Begin with fresh components.
@@ -55,7 +59,17 @@ module SocksToRTC {
5559
// Messages received via signalling channel must reach the remote peer
5660
// through something other than the peerconnection. (e.g. XMPP)
5761
fCore.createChannel().then((chan) => {
58-
this.transport_.setup('SocksToRtc-' + peerId, chan.identifier);
62+
this.transport_.setup('SocksToRtc-' + peerId, chan.identifier).then(
63+
() => {
64+
dbg('SocksToRtc transport_.setup succeeded');
65+
freedom.emit('socksToRtcSuccess', remotePeer);
66+
}
67+
).catch(
68+
(e) => {
69+
dbgErr('SocksToRtc transport_.setup failed ' + e);
70+
freedom.emit('socksToRtcFailure', remotePeer);
71+
}
72+
);
5973
this.signallingChannel_ = chan.channel;
6074
this.signallingChannel_.on('message', function(msg) {
6175
freedom.emit('sendSignalToPeer', {
@@ -64,6 +78,14 @@ module SocksToRTC {
6478
});
6579
});
6680
dbg('signalling channel to SCTP peer connection ready.');
81+
// Send hello command to initiate communication, which will cause
82+
// the promise returned this.transport_.setup to fulfill.
83+
// TODO: remove hello command once freedom.transport.setup
84+
// is changed to automatically negotiate the connection.
85+
dbg('sending hello command.');
86+
var command :Channel.Command = {type: Channel.COMMANDS.HELLO};
87+
this.transport_.send('control', ArrayBuffers.stringToArrayBuffer(
88+
JSON.stringify(command)));
6789
}); // fCore.createChannel
6890

6991
// Create SOCKS server and start listening.

0 commit comments

Comments
 (0)