-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (62 loc) · 1.42 KB
/
main.go
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
package main
import (
"fmt"
"github.com./gorilla/websocket"
"golang.org/x/crypto/ssh"
"html/template"
"net/http"
)
var (
//Allow cross-domain
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
user = "root"
password = ""
host = ""
port = 22
)
func wsHandle(w http.ResponseWriter, r *http.Request) {
var (
conn *websocket.Conn
client *ssh.Client
sshConn *SSHConnect
err error
)
if conn, err = upgrader.Upgrade(w, r, nil); err != nil {
return
}
defer conn.Close()
//Create ssh client
if client, err = createSSHClient(user, password, host, port); err != nil {
WsSendText(conn, []byte(err.Error()))
return
}
defer client.Close()
//connect to ssh
if sshConn, err = NewSSHConnect(client); err != nil {
WsSendText(conn, []byte(err.Error()))
return
}
quit := make(chan int)
go sshConn.Output(conn, quit)
go sshConn.Recv(conn, quit)
<-quit
}
func home(w http.ResponseWriter, r *http.Request) {
temp, e := template.ParseFiles("./template/index.html")
if e != nil {
fmt.Println(e)
}
temp.Execute(w, nil)
return
}
func main() {
http.Handle("/static/css/", http.StripPrefix("/static/css/", http.FileServer(http.Dir("static/css/"))))
http.Handle("/static/js/", http.StripPrefix("/static/js/", http.FileServer(http.Dir("static/js/"))))
http.HandleFunc("/index", home)
http.HandleFunc("/ws/v1", wsHandle)
http.ListenAndServe(":8080", nil)
}