-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
164 lines (137 loc) · 4.28 KB
/
client.js
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
/**
iZ³ | Izzzio blockchain - https://izzz.io
Copyright 2018 Izio Ltd (OOO "Изио")
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const RELAY_ADDRESS = 'proxyRelay';
const NODES = ["ws://176.9.104.200:6031"];
const PROXY_HOST = 'localhost';
const PROXY_PORT = 1080;
const PROXY_USER = 'user';
const PROXY_PASSWORD = 'password';
//************************************
const socks = require('socksv5');
const net = require('net');
const Candy = require('iz3-candy');
const utils = require('./utils');
let sockets = {};
const MESSAGES = {
handshake: 'HANDSHAKE',
handshakeOk: 'HANDSHAKEOK',
incomeData: 'RCV',
outcomeData: 'SND',
newConnection: 'CNT',
endConnection: 'END',
socketConnected: 'CNTOK'
};
/**
* Send message to
* @param {string} to
* @param {object} message
* @param {string} messageId
*/
function sendMessage(to, message, messageId) {
let msg = candy.starwave.createMessage(message, to, undefined, messageId);
candy.starwave.sendMessage(msg);
}
console.log('Starting Candy connection...');
let candy = new Candy(NODES).start();
/**
* Connecting Candy
*/
candy.onready = function () {
console.log('Connected.');
setTimeout(function () {
console.log('Waiting for handshake...');
sendMessage(RELAY_ADDRESS, {msg: 'Hello'}, MESSAGES.handshake);
}, 1000);
};
/**
* Handshake handler
*/
candy.starwave.registerMessageHandler(MESSAGES.handshakeOk, function (message) {
console.log('OK Handshake received. Starting...');
startServer();
});
/**
* Income data handler
*/
candy.starwave.registerMessageHandler(MESSAGES.incomeData, function (message) {
try {
let payload = Buffer.from(message.data.data, 'hex');//utils.hexString2Uint8Array(utils.unicode2HexString(message.data.data));
sockets[message.data.socketId].socket.write(Buffer.from(payload));
} catch (e) {
console.log(e);
}
});
/**
* Remote connection handler
*/
candy.starwave.registerMessageHandler(MESSAGES.socketConnected, function (message) {
let socketId = message.data.socketId;
let socket = sockets[socketId].socket;
});
/**
* Ending connection handler
*/
candy.starwave.registerMessageHandler(MESSAGES.endConnection, function (message) {
let socketId = message.data.socketId;
let socket = sockets[socketId].socket;
socket.end();
});
/**
* Start SOCKS5
*/
function startServer() {
/**
* Start socks server
*/
var srv = socks.createServer(function (info, accept, deny) {
let socketId = candy.getid();
socket = accept(true);
sockets[socketId] = {socket: socket, id: socketId};
/**
* Data from client
*/
socket.on('data', function (data) {
sendMessage(RELAY_ADDRESS, {
data: /*utils.hexString2Unicode*/(data.toString('hex')),
socketId: socketId
}, MESSAGES.outcomeData)
});
socket.on('end', function () {
sendMessage(RELAY_ADDRESS, {
socketId: socketId
}, MESSAGES.endConnection);
});
socket.on('error', function () {
sendMessage(RELAY_ADDRESS, {
socketId: socketId
}, MESSAGES.endConnection);
});
//Send new connection beacon
sendMessage(RELAY_ADDRESS, {
port: info.dstPort,
address: info.dstAddr,
socketId: socketId
}, MESSAGES.newConnection);
});
srv.listen(PROXY_PORT, PROXY_HOST, function () {
console.log('SOCKSv5 server listening on ', PROXY_HOST, PROXY_PORT);
});
if(PROXY_PASSWORD.length > 0) {
srv.useAuth(socks.auth.UserPassword(function (user, password, cb) {
cb(user === PROXY_USER && password === PROXY_PASSWORD);
}));
} else {
srv.useAuth(socks.auth.None());
}
}