-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd.js
executable file
·134 lines (125 loc) · 4.66 KB
/
httpd.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
const https = require('https');
const url = require('url'); //url parsing
const fs = require('fs'); //file system
const net = require('net'); //network
const ws = require('ws'); //websocket
// https://github.com/websockets/ws
//install with:
//npm install --save ws
//on Dexter, if httpd.js is going to be in the /srv/samba/share/ folder,
//install ws there but then run it from root. e.g.
//cd /srv/samba/share/
//npm install --save ws
//cd /
//node /srv/samba/share/httpd.js
//var mime = require('mime'); //translate extensions into mime types
//skip that,it's stupidly big
const mimeTypes = {
"html": "text/html",
"mp3": "audio/mpeg",
"mp4": "video/mp4",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"svg": "image/svg+xml",
"gif": "image/gif",
"js": "text/javascript",
"css": "text/css",
};
const options = {
key: fs.readFileSync( __dirname+'/certs/privkey.pem'),
cert: fs.readFileSync( __dirname+'/certs/fullchain.pem')
};
//standard web server on port 443 to serve files
const server = https.createServer(options, function(req, res) {
var q = url.parse(req.url, true);
if ("/" == q.pathname) q.pathname = "index.html";
var filename = "/srv/samba/share/www/" + q.pathname;
console.log("serving" + q.pathname);
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {
'Content-Type': 'text/html'
});
return res.end("404 Not Found");
}
res.setHeader('Access-Control-Allow-Origin', '*');
let mimeType = mimeTypes[q.pathname.split(".").pop()] || "application/octet-stream";
console.log("Content-Type:", mimeType);
res.setHeader("Content-Type", mimeType);
res.writeHead(200);
res.write(data);
return res.end();
});
}).listen(443);
console.log("listing on port 443");
//socket server to accept websockets from the browser on port 3000
//and forward them out to DexRun as a raw socket
const browser = new ws.Server({
server
})
var bs;
const dexter = new net.Socket();
//don't open the socket yet, because Dexter only allows 1 socket connection
dexter.connected = false; //track socket status (doesn't ws do this?)
browser.on('connection', function connection(socket, req) {
console.log(process.hrtime()[1], " browser connected ", req.connection.Server);
bs = socket
socket.on('message', function(data) {
console.log(process.hrtime()[1], " browser says ", data.toString());
//Now as a client, open a raw socket to DexRun on localhost
if (!dexter.connected && !dexter.connecting) {
dexter.connect(50000, "127.0.0.1")
console.log("dexter connect")
dexter.on("connect", function() {
dexter.connected = true
console.log("dexter connected")
dexter.write(data.toString());
})
dexter.on("data", function(data) {
//console.log(process.hrtime()[1], " dexter says ", data)
//for(let i = 0; i<8*4; i+=4) {console.log(i, data[i])}
console.log(process.hrtime()[1], " dexter says ", "#" + data[1 * 4] + " op:" + String.fromCharCode(data[4 * 4]))
if (data[5 * 4]) {
console.log("error:" + data[5 * 4])
}
if (bs) {
bs.send(data, {
binary: true
})
console.log(process.hrtime()[1], " sent to browser ")
}
})
dexter.on("close", function() {
dexter.connected = false
console.log("dexter disconnect")
dexter.removeAllListeners()
//or multiple connect/data/close events next time
})
dexter.on("end", function() {
dexter.connected = false
console.log("dexter ended")
//dexter.removeAllListeners()
dexter.end()
//or multiple connect/data/close events next time
})
dexter.on("error", function() {
dexter.connected = false
console.log("dexter error")
if (bs) bs.send(null, {
binary: true
});
dexter.removeAllListeners()
dexter.destroy()
})
}
dexter.write(data.toString());
});
socket.on('close', function(data) {
console.log(process.hrtime()[1], " browser disconnected ");
bs = null
dexter.end()
});
});
//test to see if we can get a status update from DexRun
//dexter.write("1 1 1 undefined g ;")