-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (65 loc) · 1.86 KB
/
index.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
const argv=require("yargs").alias('p',"port").describe("p","specify the listen port").alias("a","auth").describe("a","specify basic auth in user:pass format").argv;
const express=require("express")
const cp=require('child_pty')
const servestatic=require("serve-static")
var port=process.env.PORT ||3000;
if(argv.port) port=argv.port
var app=express()
if(argv.auth)
{
var users={};
if(Array.isArray(argv.auth))
{
for(var i=0;i<argv.auth.length;i++)
{
var chunks=argv.auth[i].split(":");
users[chunks.shift()]=chunks.join(":");
}
}
else
{
var chunks=argv.auth.split(":")
users[chunks.shift()]=chunks.join(":");
}
const bauth=require("express-basic-auth");
app.use(bauth({
users:users,
challenge:true,
realm:"webterminal"
}));
}
var http=require("http").createServer(app);
var io=require("socket.io")(http);
var p=cp.spawn("bash",[],{name:"xterm-color",columns:80,rows:25})
console.log(` process id ${p.pid}`)
p.stdout.on('resize', function() {
console.log('New columns: ' + this.columns);
console.log('New rows: ' + this.rows);
})
p.on("exit",(num,sig) => {
console.log("process exit");
})
p.stdout.on("data",(data)=> {
io.emit("chat message",data.toString("binary"));
});
p.stderr.on("data",(data)=> {
io.emit("chat message",data.toString());
});
app.use(servestatic("./"))
app.get("/",(req,res) => {
res.sendFile(__dirname+"/index.html");
});
io.on("connection", (socket) => {
console.log("a user logon");
socket.emit("chat message","Welcome to here\r\n>")
socket.on("disconnect",() => {
console.log("user disconnected");
});
socket.on("chat message",function(m) {
//console.log("message: "+m);
p.stdin.write(m);
});
});
http.listen(port,"0.0.0.0",() => {
console.log("listen on "+port);
});