-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
151 lines (125 loc) · 2.75 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"custom-browser-ssh-terminal/config"
"log"
"net/http"
"strings"
"github.com/BurntSushi/toml"
"github.com/gorilla/websocket"
)
var clients = make(map[*websocket.Conn]bool)
var broadcast = make(chan Message)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type Message struct {
Message string `json:"message"`
}
var (
configServer *config.Servers
configCommand *config.Commands
)
func main() {
if _, err := toml.DecodeFile("./servers.toml", &configServer); err != nil {
log.Println(err)
return
}
if _, err := toml.DecodeFile("./commands.toml", &configCommand); err != nil {
log.Println(err)
return
}
fs := http.FileServer(http.Dir("public"))
http.Handle("/", fs)
http.HandleFunc("/ws", handleConnections)
go handleMessages()
log.Println("http server started on :8000")
err := http.ListenAndServe("0.0.0.0:8000", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func handleConnections(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
clients[ws] = true
for {
var msg Message
err := ws.ReadJSON(&msg)
if err != nil {
log.Printf("error: %v", err)
delete(clients, ws)
break
}
broadcast <- msg
}
}
func handleMessages() {
for {
msg := <-broadcast
for client := range clients {
msg.Message = cmdExec(msg.Message)
err := client.WriteJSON(msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients, client)
}
}
}
}
func cmdExec(cmd string) string {
if cmd == "help" {
return helpResponse()
}
s := strings.Split(cmd, " ")
if len(s) > 1 {
server := findServer(s[0])
if server.Name == "unknown" {
return " '" + s[0] + "' Server not found!"
}
command := findCmd(s[1])
if command.ShortCmd == "unknown" {
return " " + s[1] + " : command not found"
}
return sshCmd(server, command)
}
return " " + cmd + " : command not found \n try 'help' command"
}
func findServer(server string) config.Server {
for _, s := range configServer.Server {
if s.Name == server {
return s
}
}
var s config.Server
s.Name = "unknown"
return s
}
func findCmd(command string) config.Command {
for _, c := range configCommand.Command {
if c.ShortCmd == command {
return c
}
}
var c config.Command
c.ShortCmd = "unknown"
return c
}
func helpResponse() string {
var response string
response += "\n # SERVERS \n"
for _, s := range configServer.Server {
response += " * [" + s.Name + "]" + " " + s.Description + " \n"
}
response += "\n # COMMANDS \n"
for _, c := range configCommand.Command {
response += " * [" + c.ShortCmd + "]" + " " + c.Description + " \n"
}
response += "\n"
return response
}