Skip to content

Instantly share code, notes, and snippets.

@idiotWu
Last active November 19, 2017 11:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idiotWu/b7729c4462635214c7e06159f82c01b2 to your computer and use it in GitHub Desktop.
Save idiotWu/b7729c4462635214c7e06159f82c01b2 to your computer and use it in GitHub Desktop.
Unlock netease music
const http = require('http');
const net = require('net');
const url = require('url');
const IP = '211.161.244.70';
const hosts = {
'p[1-4].music.126.net': 'cloudedge.chinanetcenter.com',
'm10.music.126.net': 'ws.acgvideo.com',
};
const regex = /^Host:.+$/im;
const seperator = String.fromCharCode(0x3A) + String.fromCharCode(0x20);
const linebreak = String.fromCharCode(0x0D) + String.fromCharCode(0x0A);
function mapHost(source) {
const match = Object.keys(hosts).find((rule) => {
const re = new RegExp(rule);
return re.test(source);
});
return match ? hosts[match] : source;
}
function request(req, res) {
const u = url.parse(req.url);
const options = {
hostname: mapHost(u.hostname),
port: u.port || 80,
path: u.path,
method: req.method,
headers: Object.assign({}, req.headers, {
'X-Real-IP': IP,
}),
};
const proxyReq = http.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
}).on('error', (e) => {
res.end();
});
req.pipe(proxyReq);
}
function connect(req, socket, head) {
const u = url.parse(`http://${req.url}`);
const proxySocket = net.connect(u.port, mapHost(u.hostname), () => {
socket.write(`HTTP/${req.httpVersion} 200 Connection Established\r\n\r\n`);
proxySocket.pipe(socket);
}).on('error', (e) => {
socket.end();
});
if (u.port !== '80') {
return socket.pipe(proxySocket);
}
socket.on('data', (data) => {
const t = data.toString();
if (regex.test(t)) {
data = t.replace(regex, `$&${linebreak}X-Real-IP${seperator}${IP}`);
}
proxySocket.write(data);
}).on('end', () => {
proxySocket.end();
});
}
http.createServer()
.on('request', request)
.on('connect', connect)
.listen(26569, '0.0.0.0');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment