forked from brozeph/simple-socks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createServerWithAuthentication.js
60 lines (49 loc) · 1.62 KB
/
createServerWithAuthentication.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
import socks5 from '../src/socks5.js';
const server = socks5.createServer({
authenticate : function (username, password, socket, callback) {
// verify username/password
if (username !== 'foo' || password !== 'bar') {
// respond with auth failure (can be any error)
return setImmediate(callback, new Error('invalid credentials'));
}
// return successful authentication
return setImmediate(callback);
}
});
// start listening!
server.listen(1080);
server.on('handshake', function () {
console.log();
console.log('------------------------------------------------------------');
console.log('new client connection');
});
// When authentication succeeds
server.on('authenticate', function (username) {
console.log('user %s successfully authenticated!', username);
});
// When authentication fails
server.on('authenticateError', function (username, err) {
console.log('user %s failed to authenticate...', username);
console.log(err);
});
// When a reqest arrives for a remote destination
server.on('proxyConnect', function (info, destination) {
console.log('connected to remote server at %s:%d', info.address, info.port);
destination.on('data', function (data) {
console.log(data.length);
});
});
server.on('proxyData', function (data) {
console.log(data.length);
});
// When an error occurs connecting to remote destination
server.on('proxyError', function (err) {
console.error('unable to connect to remote server');
console.error(err);
});
// When a proxy connection ends
server.on('proxyEnd', function (response, args) {
console.log('socket closed with code %d', response);
console.log(args);
console.log();
});