-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
176 lines (142 loc) · 4.17 KB
/
api.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
)
var apiSecret string
type Api struct {
MTAServer *MTAServer
}
func NewApi(mtaServer *MTAServer) *Api {
api := new(Api)
api.MTAServer = mtaServer
// Bind routes
api.BindRoutes()
// Get API secret
apiSecret = os.Getenv("API_SECRET")
return api
}
func (api *Api) Listen() {
http.ListenAndServe(":8080", nil)
}
func (api *Api) BindRoutes() {
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, `
Help:
- /start : Starts the MTA server
- /stop : Stops the MTA server
- /restart : Restarts the MTA server (waits until stopped and starts then)
- /logs : Retrieves a the last n lines of the standard output (uses a ring buffer internally)
- /status: Retrieves the status of the MTA server process
- /command : Executes a command on the server's console
- /upload : Uploads a resource archive
`)
})
http.HandleFunc("/start", func(res http.ResponseWriter, req *http.Request) {
if !api.CheckAPISecret(req) {
api.SendStatusMessage(&res, "Wrong API secret")
return
}
err := api.MTAServer.Start()
api.SendStatusError(&res, err)
})
http.HandleFunc("/stop", func(res http.ResponseWriter, req *http.Request) {
if !api.CheckAPISecret(req) {
api.SendStatusMessage(&res, "Wrong API secret")
return
}
err := api.MTAServer.Stop(false)
api.SendStatusError(&res, err)
})
http.HandleFunc("/restart", func(res http.ResponseWriter, req *http.Request) {
if !api.CheckAPISecret(req) {
api.SendStatusMessage(&res, "Wrong API secret")
return
}
err := api.MTAServer.Restart()
api.SendStatusError(&res, err)
})
http.HandleFunc("/logs", func(res http.ResponseWriter, req *http.Request) {
if !api.CheckAPISecret(req) {
api.SendStatusMessage(&res, "Wrong API secret")
return
}
output := api.MTAServer.TailBuffer()
json.NewEncoder(res).Encode(ConsoleOutputMessage{ApiMessage: ApiMessage{Status: "OK"}, Output: output})
})
http.HandleFunc("/status", func(res http.ResponseWriter, req *http.Request) {
if !api.CheckAPISecret(req) {
api.SendStatusMessage(&res, "Wrong API secret")
return
}
json.NewEncoder(res).Encode(*api.MTAServer.Status())
})
http.HandleFunc("/command", func(res http.ResponseWriter, req *http.Request) {
if !api.CheckAPISecret(req) {
api.SendStatusMessage(&res, "Wrong API secret")
return
}
// Parse POST parameters
req.ParseForm()
if req.Method != "POST" {
api.SendStatusMessage(&res, "Bad method")
} else {
command := req.Form.Get("command")
var err error
if command != "" {
err = api.MTAServer.ExecCommand(command)
} else {
err = errors.New("Empty command")
}
api.SendStatusError(&res, err)
}
})
http.HandleFunc("/upload", func(res http.ResponseWriter, req *http.Request) {
if !api.CheckAPISecret(req) {
api.SendStatusMessage(&res, "Wrong API secret")
return
}
// Parse multipart form (uploaded file)
req.ParseMultipartForm(150 * 1024 * 1024) // Max 150MiB
file, _, err := req.FormFile("file")
if err != nil {
api.SendStatusMessage(&res, "Invalid request")
return
}
defer file.Close()
// Open resource package writer
writer := NewResourcePackageWriter(&file, "./artifacts.tar.gz")
// Write archive
err = writer.Write()
if err != nil {
api.SendStatusMessage(&res, "Could not write archive: "+err.Error())
return
}
// Extract archive
err = writer.Extract("/var/lib/mtasa/mods/deathmatch/resources/")
if err != nil {
api.SendStatusMessage(&res, "Could not extract archive: "+err.Error())
return
}
api.SendOkMessage(&res)
})
}
func (api *Api) SendOkMessage(res *http.ResponseWriter) {
json.NewEncoder(*res).Encode(ApiMessage{Status: "OK"})
}
func (api *Api) SendStatusMessage(res *http.ResponseWriter, message string) {
json.NewEncoder(*res).Encode(ApiMessage{Status: message})
}
func (api *Api) SendStatusError(res *http.ResponseWriter, err error) {
if err != nil {
json.NewEncoder(*res).Encode(ApiMessage{Status: err.Error()})
} else {
json.NewEncoder(*res).Encode(ApiMessage{Status: "OK"})
}
}
func (api *Api) CheckAPISecret(req *http.Request) bool {
return req.Header.Get("API_SECRET") == apiSecret
}