Commit 879f9ef4 authored by 神楽坂玲奈's avatar 神楽坂玲奈

fix and docker

parent 624aaed2
FROM node
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
#COPY package.json /usr/src/app/
#RUN npm install
COPY . /usr/src/app
CMD [ "npm", "start" ]
/**
* Created by zh99998 on 16/1/25.
*/
'use strict';
const http = require('http');
const crypto = require('crypto');
// Create an HTTP tunneling proxy
var servers = [{
let servers = [{
"address": "122.0.65.73",
"port": 233
"port": 7911
}];
var pending = null;
let pending = null;
http.createServer((req, res) => {
try {
var credentials = new Buffer(req.headers['authorization'].split(' ')[1], 'base64').toString().split(':');
var username = credentials[0];
var password = credentials[1];
let credentials = new Buffer(req.headers['authorization'].split(' ')[1], 'base64').toString().split(':');
let username = credentials[0];
let password = credentials[1];
if (!username || !password) {
throw 'auth';
}
//TODO: Auth
console.log(username + ' requested match.');
res.username = username;
res.password = password;
} catch (error) {
res.statusCode = 403;
res.end();
......@@ -29,27 +28,43 @@ http.createServer((req, res) => {
}
if (pending) {
var server = servers[Math.floor(Math.random() * servers.length)];
var result = JSON.stringify({
let server = servers[Math.floor(Math.random() * servers.length)];
let room_id = crypto.randomBytes(9).toString('base64').slice(0, 11);
let options_buffer = new Buffer(6);
options_buffer.writeUInt8(4 << 4, 1);
let checksum = 0;
for (let i = 1; i < options_buffer.length; i++) {
checksum -= options_buffer.readUInt8(i)
}
options_buffer.writeUInt8(checksum & 0xFF, 0);
for (let client of [res, pending]) {
let buffer = new Buffer(6);
let secret = parseInt(client.password) % 65535 + 1;
for (let i = 0; i < options_buffer.length; i += 2) {
buffer.writeUInt16LE(options_buffer.readUInt16LE(i) ^ secret, i)
}
let password = buffer.toString('base64') + room_id;
let result = JSON.stringify({
"address": server.address,
"port": server.port,
"password": crypto.randomBytes(12).toString('base64')
"password": password
});
for (var client of [res, pending]) {
client.writeHead(200, {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
client.end(result)
}
pending = null;
console.log('matched ' + result);
console.log('matched ' + room_id);
} else {
pending = res;
res.on('close', ()=> {
console.log('connection closed.')
console.log('connection closed.');
if (pending == res) {
pending = null;
}
})
}
}).listen(3001);
\ No newline at end of file
}).listen(80);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment