Commit 624aaed2 authored by 神楽坂玲奈's avatar 神楽坂玲奈

brand new

parent a242cd83
module.exports = {
port: 9997,
servers: [
{ip: '122.0.65.70', port: 7977}
]
}
\ No newline at end of file
/**
* Created by zh99998 on 16/1/25.
*/
const http = require('http');
const crypto = require('crypto');
// Create an HTTP tunneling proxy
var servers = [{
"address": "122.0.65.73",
"port": 233
}];
var 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];
if (!username || !password) {
throw 'auth';
}
//TODO: Auth
console.log(username + ' requested match.');
} catch (error) {
res.statusCode = 403;
res.end();
return;
}
if (pending) {
var server = servers[Math.floor(Math.random() * servers.length)];
var result = JSON.stringify({
"address": server.address,
"port": server.port,
"password": crypto.randomBytes(12).toString('base64')
});
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);
} else {
pending = res;
res.on('close', ()=> {
console.log('connection closed.')
if (pending == res) {
pending = null;
}
})
}
}).listen(3001);
\ No newline at end of file
http = require("http")
url = require("url")
_ = require("underscore")
settings = require("./config")
room_index = 0
waiting = []
server = http.createServer (request, response)->
console.log "#{new Date()} Received request for #{request.url} from #{request.connection.remoteAddress})"
if url.parse(request.url).pathname != '/match.json'
response.writeHead(404);
response.end();
return
response.writeHead(200, {"Content-Type": "application/json"})
if waiting.length == 0
waiting.push response
request.connection.addListener 'close', ->
index = waiting.indexOf(response);
if index != -1
waiting.splice(response, 1);
console.log "#{new Date()} Peer #{request.connection.remoteAddress} disconnected during waiting."
response.connection.setTimeout(0)
else
s = settings.servers[0]
room = "mycard://#{s.ip}:#{s.port}/M##{room_index}$#{Math.floor(Math.random()*1000)}" #new Buffer("Hello World").toString('base64'));
console.log "matched: #{room}"
opponent_response = waiting.pop()
opponent_response.end room
response.end room
room_index = room_index + 1 % 100000
.listen(settings.port)
{
"name": "ygopro-match",
"version": "0.0.1"
}
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