-
Notifications
You must be signed in to change notification settings - Fork 2
/
socksyproxy.js
executable file
·64 lines (53 loc) · 1.45 KB
/
socksyproxy.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const socks = require('socksv5');
function get_config() {
const filename = '.config/socksyproxy/config.yaml';
const filepath = path.join(process.env.HOME, filename);
const data = fs.readFileSync(filepath);
return yaml.safeLoad(data);
}
function get_proxy(proxies, host, port) {
const dest = `${host}:${port}`;
return proxies.find((proxy) => proxy.regex.some((regex) => dest.match(regex)));
}
function connection(proxies) {
return (info, accept, deny) => {
const proxy = get_proxy(proxies, info.dstAddr, info.dstPort);
if (!proxy) {
accept();
return;
}
socket_in = accept(true);
process.nextTick(function() {
socket_in.pause()
});
socks.connect({
host: info.dstAddr,
port: info.dstPort,
proxyHost: proxy.host,
proxyPort: proxy.port,
localDNS: !proxy.dns,
auths: [
socks.auth.None(),
],
}, (socket_out) => {
socket_in.pipe(socket_out);
socket_out.pipe(socket_in);
socket_in.resume();
});
}
}
function run() {
const config = get_config();
const host = config.address || 'localhost';
const port = config.port || 8888;
const srv = socks.createServer(connection(config.proxy));
srv.useAuth(socks.auth.None());
srv.listen(port, host, () => {
console.log('SOCKS server listening on', `${host}:${port}`);
});
}
run();