-
Notifications
You must be signed in to change notification settings - Fork 9
/
sendfile.js
119 lines (98 loc) · 2.79 KB
/
sendfile.js
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
var dgram = require('dgram')
, socket = dgram.createSocket('udp4')
, packet = require('packet')
, packets = require('./packets')
, __slice = [].slice;
const MAX_MSG_NO = 0x1FFFFFFF;
var Socket = require('./index').Socket;
function die () {
console.log.apply(console, __slice.call(arguments, 0));
return process.exit(1);
}
function say () { return console.log.apply(console, __slice.call(arguments, 0)) }
function extend (to, from) {
for (var key in from) to[key] = from[key];
return to;
}
var role = process.argv[2] ? 'client' : 'server';
socket.bind(role == 'client' ? 9001 : 9000);
function formatQuad (address) {
var quad = [];
for (i = 3; i >= 0; i--) {
quad[i] = Math.floor(address / ( Math.pow(256, i) ))
address = address % Math.pow(256, i)
}
return quad.join('.');
}
function parseQuad (quad) {
var address = 0;
quad = quad.split('.');
for (i = 3; i >= 0; i--) {
address = address + quad[i] * Math.pow(256, i);
}
return 0;
}
var parser = new packet.createParser(), serializer = packet.createSerializer();
for (var name in packets) {
parser.packet(name, packets[name]);
if (name == 'header') continue;
serializer.packet(name, packets.header + ',' + packets[name]);
}
var got;
socket.on('message', function (buffer, remote) {
got = buffer;
parser.extract('header', receive);
parser.parse(buffer);
});
var buffer = new Buffer(8192);
if (process.argv[2]) clientStart();
function clientStart () {
var handshake =
{ control: 1
, type: 0
, additional: 0
, timestamp: 0
, destination: 0
, version: 4
, socketType: 1
, sequence: 1582673141
, maxPacketSize: 1500
, windowSize: 8192
, connectionType: 1
, socketId: 13
, synCookie: 0
, address: 16777343
};
serializer.serialize('handshake', handshake);
serializer.write(buffer);
socket.send(buffer, 0, serializer.length, 9293, '127.0.0.1');
}
var controlTypes = 'handshake'.split(/\s+/);
function receive (header) {
if (header.control) {
if (!controlTypes[header.type]) throw new Error("bogotronic");
parser.extract(controlTypes[header.type], receptionist[role][header.type](header));
}
}
var receptionist = { client: [] };
receptionist.client.push(function (header) { return function (handshake) {
switch (handshake.connectionType) {
case -1:
die('here', header, handshake);
break;
case 1:
handshake = extend(handshake, header);
handshake.connectionType = -1;
handshake.destination = 0;
serializer.reset();
serializer.serialize('handshake', handshake);
serializer.write(buffer);
socket.send(buffer, 0, serializer.length, 9293, '127.0.0.1');
break;
default:
throw new Error("badtastic");
}
}});
function toArray (buffer) {
return buffer.toString('hex').replace(/(..)/g, ':$1').replace(/(.{12})/g, '\n$1').replace(/\n:/g, '\n');
}