-
Notifications
You must be signed in to change notification settings - Fork 0
/
wss.server.ts
60 lines (51 loc) · 1.81 KB
/
wss.server.ts
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
import { Server } from "socket.io";
import { NodeSSH } from "node-ssh";
import { SocketToProxy,ProxyToSocket, sshConfig } from "./wss.services";
const ssh = new NodeSSH();
const io = new Server<SocketToProxy,ProxyToSocket>({
cors:{
origin:"http://localhost:3001",
methods:["GET","POST"]
}
});
io.on('connection',(socket)=>{
console.log("[+] Connected:",socket.id);
socket.on('disconnect',()=>{
console.log("[-] Disconnected:",socket.id);
})
socket.on('init',async(conf)=>{
const config:sshConfig = {
host: conf.host,
username: conf.username,
password: conf.password,
port: conf.port
}
console.log("[>]","Trying to connect to", config.host+":"+config.port);
try{
await ssh.connect(config);
const shellStream = await ssh.requestShell();
console.log("[!]","Socket connect to", config.host+":"+config.port);
socket.on('command',(data:string)=>{
console.log(data.toString());
shellStream.write(data);
})
socket.on('disconnect',()=>{
shellStream.write("exit");
})
shellStream.on("data",(data:Buffer)=>{
socket.emit("tunnel",data.toString());
console.log(data.toString());
})
shellStream.stderr.on('data',(err:Buffer)=>{
socket.emit("error",err.toString());
console.log(err.toString());
})
}catch(err:any){
socket.emit("error",err.toString());
}
})
})
// SPECIFY THE WSS PROXY SERVER PORT HERE
const port = process.env.PORT ? parseInt(process.env.PORT) : 3000
io.listen(port)
console.log("[*] Server started at",port);