Skip to content

Commit 14f6557

Browse files
committed
Merge pull request #177 from uProxy/trevj-merge-control
uproxy-networking merge: copy copypaste command port
2 parents ac449fe + 006966e commit 14f6557

File tree

4 files changed

+130
-4
lines changed

4 files changed

+130
-4
lines changed

bin/connect-pair.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/python
2+
3+
# Connects two copy-paste samples running on localhost.
4+
# TODO: add host/port args.
5+
6+
import socket
7+
import time
8+
9+
first = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10+
first.connect(("localhost", 9000))
11+
12+
second = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
13+
second.connect(("localhost", 9010))
14+
15+
first.send("GET\n")
16+
time.sleep(0.5)
17+
first_sdp = first.recv(4096)
18+
second.send("GIVE " + first_sdp + "\n")
19+
time.sleep(0.5)
20+
second_sdp = second.recv(4096)
21+
first.send(second_sdp)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "uproxy-lib",
33
"description": "Shared libraries for uProxy projects.",
4-
"version": "26.1.0",
4+
"version": "26.2.0",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/uProxy/uproxy-lib"

src/samples/copypaste-socks-chromeapp/freedom-module.ts

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import arraybuffers = require('../../arraybuffers/arraybuffers');
77
import rtc_to_net = require('../../rtc-to-net/rtc-to-net');
88
import socks_to_rtc = require('../../socks-to-rtc/socks-to-rtc');
99
import net = require('../../net/net.types');
10+
import tcp = require('../../net/tcp');
1011
import signals = require('../../webrtc/signals');
1112

1213
import logging = require('../../logging/logging');
@@ -58,8 +59,86 @@ var pcConfig :freedom_RTCPeerConnection.RTCConfiguration = {
5859
var socksRtc:socks_to_rtc.SocksToRtc;
5960
var rtcNet:rtc_to_net.RtcToNet;
6061

61-
parentModule.on('start', () => {
62+
63+
// Listen for GET/GIVE requests, to control the app without user
64+
// interaction.
65+
var tcpServer:tcp.Server;
66+
67+
var localhostControlEndpoints:[net.Endpoint] = [
68+
{ address: '127.0.0.1', port: 9000 },
69+
{ address: '127.0.0.1', port: 9010 },
70+
{ address: '127.0.0.1', port: 9020 }];
71+
72+
function setupServer(endpoint:net.Endpoint) {
73+
tcpServer = new tcp.Server(endpoint);
74+
tcpServer.connectionsQueue.setSyncHandler((conn:tcp.Connection) => {
75+
conn.dataFromSocketQueue.setSyncHandler((buf:ArrayBuffer) => {
76+
var str = arraybuffers.arrayBufferToString(buf);
77+
if (str.substr(0,3).toUpperCase() == "GET") {
78+
doStart();
79+
setTimeout(() => {
80+
parentModule.emit('gatherMessage');
81+
}, 500);
82+
} else if (str.substr(0,4).toUpperCase() == "GIVE") {
83+
log.info("GIVE found.");
84+
// skip past space, and then read SDP.
85+
var sdp = str.substr(5);
86+
parentModule.emit('giveWithSDP', sdp);
87+
} else {
88+
log.info("I don't understand that command. (" + str + ")");
89+
}
90+
});
91+
});
92+
93+
tcpServer.listen().then((endpoint) => {
94+
log.info('Remote-commands available on %1', endpoint);
95+
}).catch((e:Error) => {
96+
log.error('Failed to listen on remote-command socket: %1', e.message);
97+
if (localhostControlEndpoints.length > 1) {
98+
setupServer(localhostControlEndpoints.shift());
99+
}
100+
});
101+
}
102+
103+
setupServer(localhostControlEndpoints[0]);
104+
105+
parentModule.on('giveSendBack', (data:ArrayBuffer) => {
106+
log.info('giveSendBack: with arraybuf of ' + data.byteLength);
107+
var conn:tcp.Connection = null;
108+
var all_conns = tcpServer.connections();
109+
if (all_conns.length < 1) {
110+
log.info("'giveSendBack': Weird, didn't find a connection.");
111+
return;
112+
}
113+
conn = all_conns[0];
114+
conn.send(data);
115+
conn.close();
116+
})
117+
118+
// Invokd from main.core-env.ts, upon 'gatherMessage', which comes
119+
// back with our connection ID and the SDP, as a utf-8 string.
120+
parentModule.on('getSendBack', (data:ArrayBuffer) => {
121+
log.info('getSendBack: with arraybuf of ' + data.byteLength);
122+
var conn:tcp.Connection = null;
123+
var all_conns = tcpServer.connections();
124+
if (all_conns.length < 1) {
125+
log.info("'getSendBack': Weird, didn't find a connection.");
126+
return;
127+
}
128+
conn = all_conns[0];
129+
conn.send(data);
130+
conn.dataFromSocketQueue.setNextHandler((buf:ArrayBuffer) => {
131+
var sdp = arraybuffers.arrayBufferToString(buf);
132+
log.info('got sdp ' + sdp);
133+
parentModule.emit('gotPeerSDP', sdp);
134+
conn.close();
135+
return Promise.resolve<void>();
136+
});
137+
});
138+
139+
var doStart = () => {
62140
var localhostEndpoint:net.Endpoint = { address: '127.0.0.1', port: 9999 };
141+
63142
socksRtc = new socks_to_rtc.SocksToRtc();
64143

65144
// Forward signalling channel messages to the UI.
@@ -92,10 +171,12 @@ parentModule.on('start', () => {
92171
parentModule.emit('proxyingStarted', endpoint);
93172
})
94173
.catch((e) => {
95-
console.error('socksRtc Error: ' + e + '; ' + this.socksRtc.toString());
174+
console.error('socksRtc Error: ' + e + '; ' + socksRtc.toString());
96175
});
97176
log.info('created socks-to-rtc');
98-
});
177+
}
178+
179+
parentModule.on('start', doStart);
99180

100181
// Receive signalling channel messages from the UI.
101182
// Messages are dispatched to either the socks-to-rtc or rtc-to-net

src/samples/copypaste-socks-chromeapp/main.core-env.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ module copypaste_module {
3636
model.outboundMessageValue = base64Encode(newConcatenatedJson);
3737
});
3838

39+
copypaste.on('gotPeerSDP', (peerSDP:string) => {
40+
console.log('gotPeerSDP of length ' + peerSDP.length);
41+
parseInboundMessages(peerSDP);
42+
consumeInboundMessage();
43+
});
44+
45+
copypaste.on('gatherMessage', () => {
46+
console.log('gatherMessage invoked.');
47+
console.log('gatherMessage: sending back:' + model.outboundMessageValue);
48+
49+
copypaste.emit('getSendBack',
50+
arraybuffers.stringToArrayBuffer(model.outboundMessageValue));
51+
});
52+
53+
copypaste.on('giveWithSDP', (sdp:string) => {
54+
parseInboundMessages(sdp);
55+
consumeInboundMessage();
56+
setTimeout(() => {
57+
console.log("Emitting giveSendback with SDP: " + model.outboundMessageValue);
58+
copypaste.emit('giveSendBack',
59+
arraybuffers.stringToArrayBuffer(model.outboundMessageValue));
60+
}, 500);
61+
});
62+
3963
copypaste.on('publicKeyExport', (publicKey:string) => {
4064
model.userPublicKey = publicKey;
4165
});

0 commit comments

Comments
 (0)