-
Notifications
You must be signed in to change notification settings - Fork 26
/
install.js
140 lines (129 loc) · 4.04 KB
/
install.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
'use strict';
var http = require("http"),
https = require("https"),
fs = require("fs"),
siri = require("./siri"),
pingImg = new Buffer("R0lGODlhAQABAJH/AP///wAAAMDAwAAAACH5BAEAAAIALAAAAAABAAEAAAICVAEAOw==", "base64"),
httpsServer = null,
siriServer = null,
clients = [];
function commandHandle(cmd, dev) { //{{{
console.log("Command:“" + cmd + "”");
if (/Hello/.test(cmd)) {
dev.end("Siri Proxy says Hello!");
} else if (/你好/.test(cmd)) {
dev.end("Siri代理向你问好!");
} else {
dev.proxy();
}
} // }}}
function pong(res) { // {{{
res.setHeader("Connection", "close")
res.setHeader("Content-Type", "image/gif");
res.end(pingImg);
} // }}}
function destroySockets() { // {{{
while ((function(socket) {
socket && socket.destroy();
return !!socket;
})(clients.pop()));
} // }}}
function startHttpsServer(callback) { // {{{
if (!httpsServer) {
stopSiriServer(function() {
httpsServer = https.createServer({
key: fs.readFileSync(__dirname + '/keys/server-key.pem'),
cert: fs.readFileSync(__dirname + '/keys/server-cert.pem')
}, function(req, res) {
pong(res);
});
httpsServer.on("connection", function(socket) {
clients.push(socket);
});
console.log("Starting HTTPS server...");
httpsServer.listen(443, function() {
console.log("HTTPS server started.");
callback();
});
});
} else {
callback();
}
} // }}}
function stopHttpsServer(callback) { // {{{
if (httpsServer) {
console.log("Stopping HTTPS server...");
destroySockets();
httpsServer.close(function() {
httpsServer = null;
console.log("HTTPS server stopped.");
callback();
});
} else {
callback();
}
} // }}}
function startSiriServer(callback) { // {{{
if (!siriServer) {
stopHttpsServer(function() {
siriServer = siri.createServer(commandHandle);
siriServer.on("connection", function(socket) {
clients.push(socket);
});
console.log("Starting siri server...");
siriServer.start(function() {
console.log("Siri server started.");
callback();
});
/*
siriServer.listen(443, function() {
console.log("Siri server started.");
callback();
});
*/
});
} else {
callback();
}
} // }}}
function stopSiriServer(callback) { // {{{
if (siriServer) {
console.log("Stopping siri server...");
destroySockets();
siriServer.stop(function() {
siriServer = null;
console.log("Siri server stopped.");
callback();
});
} else {
callback();
}
} // }}}
startSiriServer(function() { // {{{
console.log("Starting HTTP Server...");
http.createServer(function(req, res) {
if (/^\/(\?.*)?$/.test(req.url)) {
res.setHeader("Content-Type", "text/html");
res.end(fs.readFileSync(__dirname + "/install.html"));
} else if (/^\/ping.gif(\?.*)?$/.test(req.url)) {
pong(res);
} else if (/^\/https.gif(\?.*)?$/.test(req.url)) {
startHttpsServer(function() {
pong(res);
});
} else if (/^\/siri.gif(\?.*)?$/.test(req.url)) {
startSiriServer(function() {
pong(res);
});
} else if (/^\/ca(\?.*)?$/.test(req.url)) {
res.setHeader("Content-Type", "application/x-x509-ca-cert");
res.end(fs.readFileSync(__dirname + "/keys/server-cert.pem"));
} else {
res.writeHead(404);
res.end("404 Not Found!");
}
}).listen(80, function() {
console.log("HTTP Server started.");
});
}); // }}}
// vim600: sw=4 ts=4 fdm=marker syn=javascript