-
Notifications
You must be signed in to change notification settings - Fork 1
/
http2
133 lines (112 loc) · 3.46 KB
/
http2
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
#!/usr/bin/env node
// sudo npm i -g optimist ansi-to-html
// cd ~/bin && npm i optimist ansi-to-html
'use strict';
// const node_modules = '~/bin/node_modules/';
const $PWD = process.cwd() + "/";
const node_modules = $PWD + './node_modules/';
var argv = require(node_modules + 'optimist').argv;
var host = "0.0.0.0";
var port = 3000;
var verbose = 1
port = argv.p || argv.port || parseInt(process.env.PORT, 10) || port;
host = argv.a || host;
const exec = require('child_process').exec;
console.log('exec', 'modules ' + node_modules + ' ./myip.sh -n 10 > ~/index.html');
exec ('./myip.sh -n 10 > ~/index.html');
exec ('echo "service --status-all\n" >> ~/index.html');
exec ('sudo service --status-all >> ~/index.html');
var options = { };
var ssl = 0; // TODO:
if (ssl) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
key: argv.K || argv.key || 'key.pem'
};
}
const wait = function () {
if (process.platform === 'win32') {
require('readline').createInterface({
input: process.stdin,
output: process.stdout
}).on('SIGINT', function () {
process.emit('SIGINT');
});
}
process.on('SIGINT', function () {
console.log('http-server stopped.');
process.exit();
});
process.on('SIGTERM', function () {
console.log('http-server stopped.');
process.exit();
});
}
const fs = require('fs');
var content = "";
function readIndex(index, then) {
const enc = "binary";
fs.readFile(index, enc, function (err, data) {
if (err) {
console.log("read failure", index, err)
then("Error with index file");
throw err;
}
content = data.toString('binary');
if (content && content.length > 0) {
// content = content + ("</pre></code>").toString('binary');
if (verbose >= 2 && data.length > 0) {
console.log(index, content)
}
then(content);
}
});
}
const os = require('os');
const home = os.homedir();
var home_index = home + "/index.html";
console.log("home", home);
// sudo npm i --global ansi-to-html
// https://github.com/rburns/ansi-to-html
var convert = new (require(node_modules + 'ansi-to-html/lib/ansi_to_html.js'));
// console.log(convert.toHtml('\x1b[30mblack\x1b[37mwhite'));
const http = require('http');
var serv = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
readIndex(home_index, function (content) {
var html = content;
res.writeHeader(200, {"Content-Type": "text/html"});
res.write('<html><head><title>check</title>');
var css = `
body {
background-color: black; color: white;
}
@media screen and (prefers-color-scheme: light) {
body {
background-color: white;
color: black;
}
}`;
res.write(' <style>' + css + '</style>');
res.write('</head><body');
res.write('Hello World <br>\n');
res.write('<pre><code>');
var htmlCode = convert.toHtml(html.toString());
res.write(htmlCode);
res.write('</code></pre>');
res.end();
});
});
// test index
// verbose = 1; readIndex(home_index, function (content) { console.log(content.toString()) });
// serv.on
serv.listen(port, host);
// Cyan '\x1b[36m%s
console.log('\x1b[36mSimple http NodeJs server running at http://' + host + ':' + port + '/');
console.log('Press Ctrl+C for SIGINT exit. ');
wait();
// ~/bin/myip.sh > index.html
// ~/bin/http -p 3001
// ssh -L 3001:localhost:3001 $ip
// ~/bin/http -p 3000 &
// ssh -L 3000:localhost:3000 $ip sleep 1d &