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" ]
/** 'use strict';
* Created by zh99998 on 16/1/25.
*/
const http = require('http'); const http = require('http');
const crypto = require('crypto'); const crypto = require('crypto');
// Create an HTTP tunneling proxy let servers = [{
var servers = [{
"address": "122.0.65.73", "address": "122.0.65.73",
"port": 233 "port": 7911
}]; }];
var pending = null; let pending = null;
http.createServer((req, res) => { http.createServer((req, res) => {
try { try {
var credentials = new Buffer(req.headers['authorization'].split(' ')[1], 'base64').toString().split(':'); let credentials = new Buffer(req.headers['authorization'].split(' ')[1], 'base64').toString().split(':');
var username = credentials[0]; let username = credentials[0];
var password = credentials[1]; let password = credentials[1];
if (!username || !password) { if (!username || !password) {
throw 'auth'; throw 'auth';
} }
//TODO: Auth //TODO: Auth
console.log(username + ' requested match.'); console.log(username + ' requested match.');
res.username = username;
res.password = password;
} catch (error) { } catch (error) {
res.statusCode = 403; res.statusCode = 403;
res.end(); res.end();
...@@ -29,27 +28,43 @@ http.createServer((req, res) => { ...@@ -29,27 +28,43 @@ http.createServer((req, res) => {
} }
if (pending) { if (pending) {
var server = servers[Math.floor(Math.random() * servers.length)]; let server = servers[Math.floor(Math.random() * servers.length)];
var result = JSON.stringify({
"address": server.address, let room_id = crypto.randomBytes(9).toString('base64').slice(0, 11);
"port": server.port, let options_buffer = new Buffer(6);
"password": crypto.randomBytes(12).toString('base64') options_buffer.writeUInt8(4 << 4, 1);
}); let checksum = 0;
for (let i = 1; i < options_buffer.length; i++) {
for (var client of [res, pending]) { 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": password
});
client.writeHead(200, {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'}); client.writeHead(200, {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
client.end(result) client.end(result)
} }
pending = null; pending = null;
console.log('matched ' + result); console.log('matched ' + room_id);
} else { } else {
pending = res; pending = res;
res.on('close', ()=> { res.on('close', ()=> {
console.log('connection closed.') console.log('connection closed.');
if (pending == res) { if (pending == res) {
pending = null; pending = null;
} }
}) })
} }
}).listen(3001); }).listen(80);
\ No newline at end of file
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