-
Notifications
You must be signed in to change notification settings - Fork 1
/
myhttp
executable file
·124 lines (104 loc) · 2.83 KB
/
myhttp
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
#!/usr/bin/env node
'use strict';
// sudo npm i -g optimist ansi-to-html
const node_modules = '/usr/local/lib/node_modules/';
const os = require('os');
const home = os.homedir();
const http = require('http');
const fs = require('fs');
const convert = new (require(node_modules + 'ansi-to-html'));
var argv = require(node_modules + 'optimist').argv;
var host = "0.0.0.0";
var port = 3001;
var verbose = 1;
port = argv.p || argv.port || parseInt(process.env.PORT, 10) || port;
host = argv.a || host;
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();
});
}
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) {
if (verbose >= 2 && data.length > 0) {
console.log(index, content)
}
then(content);
}
});
}
var home_index = home + "/index.html";
console.log("home", home);
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:#206C80;
color: white;
font-size: 120%;
}
a, a:visited {
color: #40F0F0;
}
@media screen and (prefers-color-scheme: light) {
body {
background-color: white; color: black;
}
a, a:visited {
color: blue;
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);
if (verbose >= 2) {
console.log(html.toString());
}
res.write('</code></pre>');
res.end();
});
});
// 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();
// bash ~/bin/myip.sh -f > ~/index.html
// ~/bin/myhttp -p 3001
// ssh -L 3001:localhost:3001 $ip sleep 1d &