-
Notifications
You must be signed in to change notification settings - Fork 43
/
socks.js
95 lines (79 loc) · 3.46 KB
/
socks.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
//This is example of connection to agar.io's server through SOCKS4/SOCKS5 server
if(process.argv.length < 5) {
var warning = [
'Please launch this script like',
' node ./examples/socks.js SOCKS_VERSION SOCKS_IP SOCKS_PORT',
'SOCKS_IP - IP of SOCKS server',
'SOCKS_PORT - port of SOCKS server',
'SOCKS_VERSION - SOCKS server version. 4/4a/5. You can use "4" for "4a"',
'*This script uses `socks` lib and this is params used by lib',
];
for (var line in warning) console.log( warning[line] );
process.exit(0);
}
console.log('Example will use SOCKS server ' + process.argv[3] + ':' + process.argv[4] + ' version ' + process.argv[2]);
//First we need to create agent for connection, you can do it any way with any lib you want.
//I will use `socks` lib https://www.npmjs.com/package/socks
//var Socks = require('socks'); //DO THIS, not what i do
var Socks;
try {
Socks = require('socks');
} catch(e) {
var warning = [
'Failed to load `socks` lib. Install it in examples path using:',
' mkdir ./node_modules/examples/node_modules',
' npm install socks --prefix ./node_modules/agario-client/examples',
' node ./node_modules/agario-client/examples/socks.js'
];
for (var line in warning) console.log( warning[line] );
process.exit(0);
}
//And we need agario-client
var AgarioClient = require('../agario-client.js'); //Use next line in your code
//var AgarioClient = require('agario-client'); //Use this in your code
//We will need to create new agent for every new connection so we will make function
function createAgent() {
return new Socks.Agent({
proxy: {
ipaddress: process.argv[3],
port: parseInt(process.argv[4]),
type: parseInt(process.argv[2])
}}
);
}
//Here is main code
//You need to request server/key and connect to that server from same IP
//So you need to request server/key through same SOCKS server that you will be connecting from
//Create new agent
var agent = createAgent();
//Options for getFFAServer
var get_server_opt = {
region: 'EU-London', //server region
agent: agent //our agent
};
//SOCKS version 4 do not accept domain names, we will need to resolve it
//SOCKS version 4a and 5 can accept domain names as targets
if(process.argv[2] == '4') {
get_server_opt.resolve = true;
}
//Requesting server's IP and key
AgarioClient.servers.getFFAServer(get_server_opt, function(srv) {
if(!srv.server) {
console.log('Failed to request server (error=' + srv.error + ', error_source=' + srv.error_source + ')');
process.exit(0);
}
console.log('Got agar.io server ' + srv.server + ' with key ' + srv.key);
//Here we already have server and key requested through SOCKS server
//Now we will create agario-client
var client = new AgarioClient('worker');
client.debug = 2;
//Create new agent for client
client.agent = createAgent();
client.on('leaderBoardUpdate', function(old_highlights, highlights, old_names, names) {
client.log('Leaders on server: ' + names.join(', '));
console.log('[SUCCESS!] Example succesfully connected to server through SOCKS server and received data. Example is over.');
client.disconnect();
});
//Connecting to server
client.connect('ws://' + srv.server, srv.key);
});