-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServiceProvider.js
226 lines (189 loc) · 7.49 KB
/
ServiceProvider.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
import Peer from "./Peer.js";
import Message from "./Message.js";
import HyperDHT from '@hyperswarm/dht';
import Hyperswarm from 'hyperswarm';
import Net from 'net';
import Crypto from 'crypto';
import Sodium from 'sodium-universal';
import b4a from 'b4a';
import UDPNet from './UDPNet.js';
import HttpApi from "./HttpApi.js";
import Utils from "./Utils.js";
export default class ServiceProvider extends Peer {
constructor(secret, opts) {
super(secret, false, opts);
this.services = {};
this.refresh();
}
startHttpApi(listenOn) {
super.startHttpApi(listenOn);
this.httpApi.register("/services", (url, body) => {
return this.services;
});
this.httpApi.register("/routingTable", (url, body) => {
return this._createRoutingTableFragment();
});
}
addService(gatePort, serviceHost, servicePort) {
console.info("Register service " + gatePort + " " + serviceHost + " " + servicePort);
this.services[gatePort] = {
serviceHost,
servicePort,
protocol: servicePort || "tcp"
};
}
_getService(gatePort) {
if (this.gateway) {
throw "Cannot get service of a gateway";
}
return this.services[gatePort];
}
_createRoutingTableFragment() {
if (Object.keys(this.services).length == 0) return undefined;
const routingTable = {};
for (const gatePort in this.services) {
// const protocol=this.services[gatePort].protocol;
routingTable[gatePort] = [{
route: "",
// protocol:protocol
}];
}
return routingTable;
}
async refresh() {
if (this.stopped) return;
super.refresh();
this.refreshing = true;
// Advertise local routes to newly connected peer
const rfr = this._createRoutingTableFragment();
if (rfr) {
await this.broadcast(Message.create(Message.actions().advRoutes, { routes: rfr }));
console.log("Broadcast routing fragment", rfr)
}
this.refreshing = false;
}
async onAuthorizedMessage(peer, msg) {
await super.onAuthorizedMessage(peer, msg);
try {
if (!peer.channels) {
peer.channels = {};
}
// close channel bidirectional
const closeChannel = (channelPort) => {
const channel = peer.channels[channelPort];
if (!channel) return;
try {
if (channel.route) {
this.send(channel.route, Message.create(Message.actions().close, {
channelPort: channelPort
}));
}
} catch (e) {
console.error(e);
}
if (channel.socket) {
channel.socket.end();
}
channel.alive = false;
delete peer.channels[channelPort];
}
// open new channel
if (msg.actionId == Message.actions().open) {
// open connection to service
const gatePort = msg.gatePort;
const service = this._getService(gatePort);
if (!service) throw "Service not found " + gatePort;
const isUDP = service.protocol == "udp";
// Service not found, tell peer there was an error
if (!service) {
console.error("service not found");
this.send(peer.info.publicKey,
Message.create(Message.actions().open,
{ channelPort: msg.channelPort, error: "Service " + gatePort + " not found" }));
// closeChannel(msg.channelPort);
return;
}
// connect to service
console.log("Connect to", service.serviceHost, service.servicePort, isUDP ? "UDP" : "TCP", "on channel", msg.channelPort);
const serviceConn = (isUDP ? UDPNet : Net).connect({
host: service.serviceHost,
port: service.servicePort,
allowHalfOpen: true
});
// create channel
const channel = {
socket: serviceConn,
duration: 1000 * 60, // 1 minute
expire: Date.now() + 1000 * 60,
gatePort: gatePort,
alive: true,
route: peer.info.publicKey,
channelPort: msg.channelPort,
service: service
};
peer.channels[channel.channelPort] = channel;
// pipe from service to route
serviceConn.on("data", data => {
// every time data is piped, reset channel expire time
channel.expire = Date.now() + channel.duration;
this.send(channel.route, Message.create(Message.actions().stream, {
channelPort: msg.channelPort,
data: data
}));
});
const timeout = () => {
if (!channel.alive) return;
try {
if (channel.expire < Date.now()) {
console.log("Channel expired!");
closeChannel(channel.channelPort);
}
} catch (e) {
console.error(e);
}
setTimeout(timeout, 1000 * 60);
};
timeout();
serviceConn.on("error", (err) => {
console.error(err);
closeChannel(channel.channelPort);
});
serviceConn.on("close", () => {
closeChannel(channel.channelPort);
});
serviceConn.on("end", () => {
closeChannel(channel.channelPort);
});
// confirm open channel
this.send(peer.info.publicKey, Message.create(Message.actions().open, {
channelPort: msg.channelPort,
gatePort: msg.gatePort
}));
} else {
// pipe from route to service
if (msg.actionId == Message.actions().stream) {
const channel = peer.channels[msg.channelPort];
if (channel) {
// console.log("Pipe to route");
// every time data is piped, reset channel expire time
channel.expire = Date.now() + channel.duration;
channel.socket.write(msg.data);
}
} else if (msg.actionId == Message.actions().close) { // close channel
if (msg.channelPort <= 0) {
for (const channelPort in peer.channels) {
closeChannel(channelPort);
}
} else {
closeChannel(msg.channelPort);
}
}
}
} catch (e) {
console.error(e);
this.send(peer.info.publicKey, Message.create(msg.actionId, {
error: e.toString()
}));
}
}
}