-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmeship.uc
More file actions
executable file
·80 lines (70 loc) · 1.6 KB
/
meship.uc
File metadata and controls
executable file
·80 lines (70 loc) · 1.6 KB
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
import * as socket from "socket";
const PORT = 4404;
let s = null;
let bridge = false;
export function setup(config)
{
if (!config.meship) {
return;
}
if (config.meship.bridge) {
bridge = true;
}
s = socket.create(socket.AF_INET, socket.SOCK_DGRAM, 0);
s.bind({
port: PORT
});
s.listen();
};
export function shutdown()
{
};
export function handle()
{
return s;
};
export function isBridge()
{
return bridge;
};
export function recv()
{
try {
const m = s.recvmsg(65535);
const msg = json(m.data);
msg.ipaddress = m.address.address;
// Avoid messages from remote networks if we don't have our own ip forwarder available.
// This avoid async messages where we receive from remote networks but cannot reply.
if (!bridge && msg.transport === "native" && !platform.canAcceptIPAddress(msg.ipaddress)) {
return null;
}
return msg;
}
catch (_) {
return null;
}
};
export function send(to, msg, canforward)
{
const targets = platform.getTargetsByIdAndNamekey(to, msg.namekey, canforward);
const data = sprintf("%J", msg);
const from = msg.from;
for (let i = length(targets) - 1; i >= 0; i--) {
const t = targets[i];
if (t.id !== from) {
const r = s.send(data, 0, {
address: t.ip,
port: PORT
});
if (r === null) {
DEBUG0("meship:send error: %s\n", socket.error());
}
}
}
};
export function tick()
{
};
export function process(msg)
{
};