Repository navigation
Expand file tree
/
Copy pathProxyWorker.dart
More file actions
198 lines (142 loc) · 5.75 KB
/
Copy pathProxyWorker.dart
File metadata and controls
198 lines (142 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import 'dart:core';
import 'dart:io';
import 'Protocol.dart';
class ProxyWorker {
Socket controlSocket;
List forwardingSockets = [];
List remoteSockets = [];
void register(brokerHost, brokerPort, exposePort, {Function onDone, Function onError}) {
if (onDone == null) {
onDone = () => print('worker registered');
}
if (onError == null) {
onError = (e) => print('registration failed');
}
if (controlSocket != null) {
onError('already connected!');
return;
}
Socket.connect(brokerHost, brokerPort).then((Socket sock) {
controlSocket = sock;
// send register request to the broker
print('sending register request to broker at $brokerHost:$brokerPort for port $exposePort');
List<int> msg = buildRegisterWorkerMsg(exposePort);
controlSocket.add(msg);
// wait for messages from the broker in this "control" socket
controlSocket.listen((List<int> data) {
print('incoming ${data.length} bytes from broker');
Map msg = parseProtocolMsg(data);
if (msg == null) {
print('invalid msg');
return;
} else if (msg['version'] == 0x00) {
// response msg received
if (msg['retCode'] == RCODE_REGISTER_SUCCESS) {
print('registration succeeded');
onDone();
return;
} else if (msg['retCode'] == RCODE_REGISTER_FAILED) {
print('registration failed, closing connection');
controlSocket.destroy();
onError('registration failed');
return;
}
} else if (msg['version'] == 0xff && msg['cmdCode'] == CMD_OPEN_FORWARDING) {
// received a request to open a reverse connection
print('request to open forwarding connection to port: ${msg['port']}');
Socket.connect(brokerHost, msg['port']).then((Socket forwardSocket) {
print('forwarding connection stablished!');
bool granted = false;
Socket remoteSocket;
// save socket to close it later
forwardingSockets.add(forwardSocket);
forwardSocket.listen((List<int> data) {
print('${data.length} bytes of data received from a forwarding connection');
// NOTE: here starts the normal SOCKS protocol. The first msg should be a SOCKS4 request
// to connect to some site. The following msgs just need to be forwarded
if (!granted) {
// initial state: parse received message that should be a SOCKS request
msg = parseProtocolMsg(data);
print(msg);
if (msg == null) {
print('invalid socks request, closing connection');
forwardSocket.close();
// TODO: should I return an error msg here instead of closing the connection?
return;
} else if (msg['cmdCode'] == CMD_SOCKS_OPEN_CONNECTION) {
// received a valid SOCKS request, opening a connection with the remote host
print('valid socks request, stablishing connection with remote host');
Socket.connect(msg['ip'], msg['port']).then((Socket sock) {
remoteSocket = sock;
// save socket to close it later
remoteSockets.add(remoteSocket);
remoteSocket.listen((List<int> data) {
print('received ${data.length} bytes from remote host, retransmiting to client host');
forwardSocket.add(data);
}, onDone: () {
remoteSocket.close();
});
// connection with the remote host is stablised, inform the browser
print('connection stablished with remote host, sending back confirmation to the browser');
var resp = buildGrantedResponse();
forwardSocket.add(resp);
// set flag so following msgs are not interpreted as SOCKS
granted = true;
});
} else {
// the msg is a valid SOCKS msg but is not a request. do nothing
print('valid socks msg received but is not of type request, closing the connection');
forwardSocket.close();
return;
}
} else {
// already granted state: forward data to the remote host
print('just retransmiting');
remoteSocket.add(data);
}
}, onDone: () {
print('forwardSocket was closed by the broker, closing here');
forwardSocket.close();
});
});
}
}, onDone: () {
print('controlSocket was closed by the broker, stopping');
stop();
});
}).catchError( (e) => onError(e) );
}
void stop({Function() onDone, Function(String) onError}) {
if (onDone == null) {
onDone = () => print('worker stopped');
}
if (onError == null) {
onError = (e) => print('error stopping worker: $e');
}
if (controlSocket == null) {
onError('not connected');
} else {
for (var socket in forwardingSockets) {
socket.destroy();
}
for (var socket in remoteSockets) {
socket.destroy();
}
controlSocket.destroy();
onDone();
controlSocket = null;
}
}
}
void main(List<String> args) async {
var exposePort = 8050;
if (args.length > 0) {
exposePort = int.parse(args[0]);
}
var worker = new ProxyWorker();
worker.register('localhost', 9999, exposePort);
// wait 5 seconds
// await new Future.delayed(const Duration(seconds : 5));
// disconnect worker
// worker.stop();
}