@@ -7,6 +7,7 @@ import arraybuffers = require('../../arraybuffers/arraybuffers');
77import rtc_to_net = require( '../../rtc-to-net/rtc-to-net' ) ;
88import socks_to_rtc = require( '../../socks-to-rtc/socks-to-rtc' ) ;
99import net = require( '../../net/net.types' ) ;
10+ import tcp = require( '../../net/tcp' ) ;
1011import signals = require( '../../webrtc/signals' ) ;
1112
1213import logging = require( '../../logging/logging' ) ;
@@ -58,8 +59,86 @@ var pcConfig :freedom_RTCPeerConnection.RTCConfiguration = {
5859var socksRtc :socks_to_rtc . SocksToRtc ;
5960var 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
0 commit comments