-
Notifications
You must be signed in to change notification settings - Fork 13
/
http.js
116 lines (102 loc) · 2.85 KB
/
http.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
var request = require("request");
var config = require("./config.json");
var NodeRtmpClient = require("node-media-server/node_rtmp_client");
var HTTP = function (main) {
this.servers = main.servers;
var _self = this;
setInterval(function () {
_self.checkStatus();
}, 30000);
};
HTTP.prototype.checkStatus = function () {
var servers = this.servers,
t = 0;
var _self = this;
Object.keys(servers).forEach(function (name) {
var server = servers[name];
if (server.type === "chat") return;
if (server.type === "ingest") {
setTimeout(function () {
_self.checkIngestAddress.call(
_self,
server.name,
server.host,
server.port
);
}, (t += 1000));
return;
}
setTimeout(function () {
_self.checkWebAddress.call(
_self,
server.name,
server.host,
server.port,
server.path
);
}, (t += 1000));
});
};
HTTP.prototype.checkWebAddress = function (name, host, port, path) {
var servers = this.servers,
startTime = Date.now(),
path = path || "/",
url = "http" + (port === 443 ? "s" : "") + "://" + host + ":" + port + path,
isAPI = host.toLowerCase() === "api.twitch.tv";
request.get(
{
url: url,
qs: {
kappa: Math.random(),
},
headers: isAPI
? {
"Client-ID": config.irc.client_id,
Authorization: "Bearer " + config.irc.access_token,
}
: undefined,
timeout: 30000,
},
function (err, res, body) {
if (!err && res.statusCode === 200) {
servers[name].lag = Date.now() - startTime;
servers[name].status = "online";
// I consider >= 3000 slow because of CDNs
// 1935 is an ingest port, but some ingests are across the world
if (servers[name].lag >= 3000 && port !== 1935) {
servers[name].status = "slow";
}
} else {
servers[name].lag = 999999;
servers[name].status = "offline";
}
}
);
};
HTTP.prototype.checkIngestAddress = function (name, host, port) {
var servers = this.servers;
var startTime = Date.now();
var client = new NodeRtmpClient(
"rtmp://" + host + ":" + port + "/app/fakestreamkey"
);
// we destroy the connection before attempting to read from a stream key
client.rtmpSendCreateStream = function () {};
client.on("status", function (info) {
switch (info.code) {
case "NetConnection.Connect.Success":
servers[name].lag = Date.now() - startTime;
servers[name].status = "online";
client.socket.destroy();
break;
default:
console.log("unhandled ingest info", info);
break;
}
});
client.startPull();
client.socket.on("error", () => {
servers[name].lag = 999999;
servers[name].status = "offline";
});
};
module.exports = HTTP;