-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathproxy.js
More file actions
149 lines (132 loc) · 4.33 KB
/
proxy.js
File metadata and controls
149 lines (132 loc) · 4.33 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
'use strict'
const http = require('http')
const https = require('https')
/**
* Creates a proxy-aware connect function based on proxy configuration
* @param {Object} proxyConfig - Proxy configuration
* @param {string} proxyConfig.type - Proxy type ('socks4', 'socks5', 'http', 'https')
* @param {string} proxyConfig.host - Proxy host
* @param {number} proxyConfig.port - Proxy port
* @param {Object} [proxyConfig.auth] - Authentication credentials
* @param {string} [proxyConfig.auth.username] - Username
* @param {string} [proxyConfig.auth.password] - Password
* @param {string} targetHost - Target Minecraft server host
* @param {number} targetPort - Target Minecraft server port
* @returns {Function} Connection handler function
*/
function createProxyConnect (proxyConfig, targetHost, targetPort) {
switch (proxyConfig.type.toLowerCase()) {
case 'http':
case 'https':
return createHttpConnect(proxyConfig, targetHost, targetPort)
case 'socks4':
case 'socks5':
return createSocksConnect(proxyConfig, targetHost, targetPort)
default:
throw new Error(`Unsupported proxy type: ${proxyConfig.type}`)
}
}
/**
* Creates HTTP CONNECT proxy function (based on existing client_http_proxy example)
*/
function createHttpConnect (proxyConfig, targetHost, targetPort) {
return function (client) {
const connectOptions = {
host: proxyConfig.host,
port: proxyConfig.port,
method: 'CONNECT',
path: `${targetHost}:${targetPort}`
}
// Add authentication header if provided
if (proxyConfig.auth) {
const credentials = Buffer.from(`${proxyConfig.auth.username}:${proxyConfig.auth.password}`).toString('base64')
connectOptions.headers = {
'Proxy-Authorization': `Basic ${credentials}`
}
}
const httpModule = proxyConfig.type.toLowerCase() === 'https' ? https : http
const req = httpModule.request(connectOptions)
req.on('connect', (res, stream) => {
if (res.statusCode === 200) {
client.setSocket(stream)
client.emit('connect')
} else {
client.emit('error', new Error(`HTTP CONNECT failed: ${res.statusCode} ${res.statusMessage}`))
}
})
req.on('error', (err) => {
client.emit('error', new Error(`HTTP proxy error: ${err.message}`))
})
req.end()
}
}
/**
* Creates SOCKS proxy function (based on existing client_socks_proxy example)
*/
function createSocksConnect (proxyConfig, targetHost, targetPort) {
return function (client) {
let socks
try {
socks = require('socks').SocksClient
} catch (err) {
client.emit('error', new Error('SOCKS proxy requires "socks" package: npm install socks'))
return
}
const socksOptions = {
proxy: {
host: proxyConfig.host,
port: proxyConfig.port,
type: proxyConfig.type === 'socks4' ? 4 : 5
},
command: 'connect',
destination: {
host: targetHost,
port: targetPort
}
}
// Add authentication if provided (SOCKS5 only)
if (proxyConfig.auth && proxyConfig.type === 'socks5') {
socksOptions.proxy.userId = proxyConfig.auth.username
socksOptions.proxy.password = proxyConfig.auth.password
}
socks.createConnection(socksOptions, (err, info) => {
if (err) {
client.emit('error', new Error(`SOCKS proxy error: ${err.message}`))
return
}
client.setSocket(info.socket)
client.emit('connect')
})
}
}
/**
* Creates a proxy-aware agent for HTTP requests (used for authentication)
*/
function createProxyAgent (proxyConfig) {
try {
const ProxyAgent = require('proxy-agent')
const protocol = proxyConfig.type.toLowerCase() === 'https'
? 'https:'
: proxyConfig.type.toLowerCase() === 'http'
? 'http:'
: proxyConfig.type.toLowerCase() === 'socks5'
? 'socks5:'
: proxyConfig.type.toLowerCase() === 'socks4' ? 'socks4:' : 'http:'
const agentOptions = {
protocol,
host: proxyConfig.host,
port: proxyConfig.port
}
if (proxyConfig.auth) {
agentOptions.auth = `${proxyConfig.auth.username}:${proxyConfig.auth.password}`
}
return new ProxyAgent(agentOptions)
} catch (err) {
// Fallback to basic agent if proxy-agent not available
return new http.Agent()
}
}
module.exports = {
createProxyConnect,
createProxyAgent
}