-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsock.js
238 lines (198 loc) · 5.73 KB
/
sock.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
var streamlib = require('stream');
var lob = require('lob-enc');
var net = require('net');
function getExternalIp() {
var ifconfig = require('os').networkInterfaces();
var device, i, I, protocol;
for (device in ifconfig) {
// ignore network loopback interface
if (device.indexOf('lo') !== -1 || !ifconfig.hasOwnProperty(device)) {
continue;
}
for (i=0, I=ifconfig[device].length; i<I; i++) {
protocol = ifconfig[device][i];
// filter for external IPv4 addresses
if (protocol.family === 'IPv4' && protocol.internal === false) {
console.log('found', protocol.address);
return protocol.address;
}
}
}
return null;
}
// implements https://github.com/telehash/telehash.org/blob/v3/v3/channels/sock.md
exports.name = 'sock';
function openUDPSock(open, cbOpen){
}
exports.mesh = function(mesh, cbMesh)
{
var ext = {open:{}};
function cbPolicy (){
return false;
}
ext.link = function(link, cbLink)
{
// ask this link to create a socket to the given args.ip and args.port
link.connect = function(args, cbMessage)
{
console.log("connect called")
var open = {
json:{
type:'sock',
sock: "connect",
dst:{
ip: args.ip,
port: args.port
}
}
};
var TCP = !(typeof cbMessage === "function");
if (TCP){
open.json.seq = 1; // always reliable
var channel = link.x.channel(open);
var stream = mesh.streamize(channel,"binary")
channel.send(open);
console.log("stream")
return stream;
} else {
console.log("UDP")
var channel = link.x.channel(open);
chan.receive = cbMessage;
channel.send(open)
return;
}
// if cbMessage is provided, create a UDP socket, otherwise return a stream
}
// ask the link to create a server for us, args.port and args.type is udp/tcp
link.sock_bound = {};
//
link.server = function(args, cbAccept, cbServer)
{
link.sock._cbAccept = cbAccept;
link.sock._cbServer = cbServer || function default_bind_accept(){
return true;
};
console.log("server (bind) called")
var open = {
json:{
type:'sock',
sock: "bind",
dst:{
port: args.port
}
}
};
if (args.type.toUpperCase() === "TCP"){
open.json.seq = 1; // always reliable
var channel = link.x.channel(open);
var stream = mesh.streamize(channel,"binary")
channel.send(open);
} else if (args.type.toUpperCase === "UDP") {
console.log("UDP")
var channel = link.x.channel(open);
chan.receive = cbMessage;
channel.send(open)
return;
}
// udp messages, fire cbAccept, cbServer returns a message method
// if no cbServer, no bind request, is just default accept
}
function linkPolicy (){
return undefined;
}
// just like mesh.sock for incoming requests on this link only
link.sock = function(cbP)
{
linkPolicy = cbP || function(){
return true;
}
}
link.sock._policy = function(json){
return (
((linkPolicy(json) || ((linkPolicy(json) === undefined) && (cbPolicy(json)))) && !link.sock_bound[json.dst.port])
);
}
cbLink();
}
// process any incoming connect/bind requests
mesh.sock = function(cbP)
{
cbPolicy = cbP || function (){
return true;
};
}
function openTCPSock(link, open, cbOpen){
var type = open.json.sock;
var src = open.json.src;
var dst = open.json.dst;
console.log("openTCPSock", type, src, dst)
var channel = link.x.channel(open);
channel.receive(open); // actually opens it
var socket;
if ((type === "connect" ) && dst && dst.port && link.sock._policy(open.json)){
//simple stream piping proxy
socket = new net.Socket();
console.log("incoming connect")
socket.connect(dst.port, dst.ip);
socket.on("connect",function(){
stream = mesh.streamize(channel);
socket.pipe(stream).pipe(socket)
cbOpen(null, stream);
})
} else if (type === "bind" && !(src && src.ip) && (link.sock._policy(open.json))){
var port = dst.port || 0
var server = net.createServer(function(c) {
// create 'accept' open
var address = c.address()
var accept = {
json: {
type:'sock',
sock: "accept",
dst: {
ip: getExternalIp(),
port : port
}, src: {
ip: address.address
}
}
};
accept.json.seq = 1; // always reliable
var accept_chan = link.x.channel(accept);
var stream = mesh.streamize(accept_chan);
accept_chan.send(accept);
c.pipe(stream).pipe(c);
});
link.sock_bound[dst.port] = true;
server.listen(port, function(err) { //'listening' listener
if (err) return cbOpen(err);
var port = server.address().port;
// let 'em know we're bound
channel.send({json:{dst:{ip: getExternalIp(),port : port }}});
});
} else if (type === "accept"){
if (link.sock._cbAccept)
link.sock._cbAccept(open.json, function accept_cb(err){
if (err)
return null;
var accept_channel = link.x.channel(open);
var stream = mesh.streamize(accept_channel);
accept_channel.receive(open);
return stream;
});
else
cbOpen("no accept handler");
} else {
console.log("invalid sock open")
}
}
ext.open.sock = function(args, open, cbOpen){
var link = this;
if (open.json.seq)
return openTCPSock(link, open, cbOpen)
// any accept, check link.sock_bound and fire cbAccept, else policy
cbPolicy(link, open, function(err){
// perform open request
})
}
cbMesh(undefined, ext);
}