-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
160 lines (148 loc) · 3.57 KB
/
http.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
152
153
154
155
156
157
158
159
160
package main
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"strings"
)
func HttpServer() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/manifest", manifestHandler)
http.HandleFunc("/info/{id}", infoHandler)
http.HandleFunc("/connect/{id}", connectHandler)
http.HandleFunc("/disconnect/{id}", disconnectHandler)
http.HandleFunc("/shell/{id}", shellHandler)
http.HandleFunc("/keepalive/{id}", keepaliveHandler)
slog.Info(fmt.Sprint("Start API server on port ", CFG.APIPort))
err := http.ListenAndServe(fmt.Sprint(":", CFG.APIPort), nil)
if err != nil {
panic(err)
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
// TODO
fmt.Fprintf(w, "rlpa-server\n")
}
func manifestHandler(w http.ResponseWriter, r *http.Request) {
// TODO
fmt.Fprintf(w, "manifest\n")
}
func infoHandler(w http.ResponseWriter, r *http.Request) {
// TODO
id := r.PathValue("id")
if verify(id, r.Header.Get("Password")) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, fmt.Sprint("hello ", id))
} else {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Unauthorized")
}
}
func connectHandler(w http.ResponseWriter, r *http.Request) {
// TODO
id := r.PathValue("id")
passwd := r.Header.Get("Password")
if verify(id, passwd) {
c, err := FindClient(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "rlpa client disconnected")
return
}
if c.APILocked {
w.WriteHeader(http.StatusConflict)
return
}
c.APILocked = true
c.StartOrResetTimer()
w.WriteHeader(http.StatusOK)
return
} else {
w.WriteHeader(http.StatusUnauthorized)
return
}
}
func disconnectHandler(w http.ResponseWriter, r *http.Request) {
// TODO 释放连接
}
func keepaliveHandler(w http.ResponseWriter, r *http.Request) {
// TODO
id := r.PathValue("id")
passwd := r.Header.Get("Password")
if verify(id, passwd) {
c, err := FindClient(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "rlpa client disconnected")
return
}
if !c.APILocked || c.KeepAliveTimer == nil {
w.WriteHeader(http.StatusTeapot)
return
}
c.StartOrResetTimer()
return
} else {
w.WriteHeader(http.StatusUnauthorized)
return
}
}
func shellHandler(w http.ResponseWriter, r *http.Request) {
// TODO 执行 shell
id := r.PathValue("id")
passwd := r.Header.Get("Password")
if verify(id, passwd) {
c, err := FindClient(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "rlpa client disconnected")
return
}
// decode json body
var payload ShellRequest
err = json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "bad request")
return
}
switch payload.Type {
case TypeFinish:
c.Close(ResultFinished)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Closed")
return
case TypeExecute:
if c.ResponseWaiting {
w.WriteHeader(http.StatusConflict)
fmt.Fprintf(w, "already has one lpac shell running")
return
}
c.DebugLog("command " + payload.Command)
err = c.processOpenLpac(strings.Split(strings.TrimSpace(payload.Command), " ")...)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
fmt.Fprintf(w, "failed to open lpac")
return
}
c.ResponseWaiting = true
resp := <-c.ResponseChan
c.ResponseWaiting = false
fmt.Fprintf(w, string(resp))
return
}
} else {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Unauthorized")
}
}
func verify(id, passwd string) bool {
if v, exists := Credentials[id]; exists {
if passwd == v {
return true
}
return false
}
return false
}