forked from LunaNode/lobster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lobster.go
347 lines (301 loc) · 12.6 KB
/
lobster.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package lobster
import "github.com/gorilla/context"
import "github.com/gorilla/mux"
import "github.com/gorilla/schema"
import "github.com/LunaNode/lobster/i18n"
import "github.com/LunaNode/lobster/websockify"
import "github.com/LunaNode/lobster/wssh"
import crand "crypto/rand"
import "encoding/binary"
import "log"
import "math/rand"
import "net/http"
import "strings"
import "sync"
import "time"
var decoder *schema.Decoder
var cfg *Config
var L *i18n.Section
var LA i18n.SectionFunc
var router *mux.Router
var db *Database
var wsMutex sync.Mutex
var ws *websockify.Websockify
var ssh *wssh.Wssh
func LobsterHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer errorHandler(w, r, true)
// replace proxy IP if set
if cfg.Default.ProxyHeader != "" {
actualIP := r.Header.Get(cfg.Default.ProxyHeader)
if actualIP != "" {
r.RemoteAddr = actualIP
}
}
if cfg.Default.Debug {
log.Printf("Request [%s] %s %s", r.RemoteAddr, r.Method, r.URL)
}
h.ServeHTTP(w, r)
})
}
func RedirectHandler(target string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target, 302)
}
}
func LogAction(userId int, ip string, name string, details string) {
db.Exec("INSERT INTO actions (user_id, ip, name, details) VALUES (?, ?, ?, ?)", userId, ip, name, details)
}
func RegisterSplashRoute(path string, template string) {
router.HandleFunc(path, getSplashHandler(template))
}
func RegisterPanelHandler(path string, f PanelHandlerFunc, onlyPost bool) {
result := router.HandleFunc(path, SessionWrap(panelWrap(f)))
if onlyPost {
result.Methods("POST")
}
}
func RegisterAPIHandler(path string, f APIHandlerFunc, method string) {
router.HandleFunc(path, apiWrap(f)).Methods(method)
}
func RegisterAdminHandler(path string, f AdminHandlerFunc, onlyPost bool) {
result := router.HandleFunc(path, SessionWrap(adminWrap(f)))
if onlyPost {
result.Methods("POST")
}
}
func RegisterHttpHandler(path string, f http.HandlerFunc, onlyPost bool) {
result := router.HandleFunc(path, f)
if onlyPost {
result.Methods("POST")
}
}
func RegisterVmInterface(region string, vmi VmInterface) {
if regionInterfaces[region] != nil {
log.Fatalf("Duplicate VM interface for region %s", region)
}
regionInterfaces[region] = vmi
}
func RegisterPaymentInterface(method string, payInterface PaymentInterface) {
if paymentInterfaces[method] != nil {
log.Fatalf("Duplicate payment interface for method %s", method)
}
paymentInterfaces[method] = payInterface
}
func GetConfig() *Config {
return cfg
}
func GetDatabase() *Database {
return db
}
func GetDecoder() *schema.Decoder {
return decoder
}
// Creates websockify instance if not already setup, initializes token, and returns URL to redirect to
func HandleWebsockify(ipport string, password string) string {
wsMutex.Lock()
defer wsMutex.Unlock()
if ws == nil {
ws = &websockify.Websockify{
Debug: cfg.Default.Debug,
Listen: cfg.Novnc.Listen,
}
ws.Run()
}
token := ws.Register(ipport)
return strings.Replace(strings.Replace(cfg.Novnc.Url, "TOKEN", token, 1), "PASSWORD", password, 1)
}
// Creates wssh instance if not already setup, initializes token, and returns URL to redirect to
func HandleWssh(ipport string, username string, password string) string {
wsMutex.Lock()
defer wsMutex.Unlock()
if ssh == nil {
ssh = &wssh.Wssh{
Debug: cfg.Default.Debug,
Listen: cfg.Wssh.Listen,
}
ssh.Run()
}
token := ssh.Register(ipport, username, password)
return strings.Replace(cfg.Wssh.Url, "TOKEN", token, 1)
}
func Setup(cfgPath string) {
cfg = LoadConfig(cfgPath)
router = mux.NewRouter()
db = MakeDatabase()
lang, err := i18n.LoadFile("language/" + cfg.Default.Language + ".json")
checkErr(err)
LA = lang.S
L = LA("lobster")
loadTemplates()
loadEmail()
loadPanelWidgets()
decoder = schema.NewDecoder()
decoder.IgnoreUnknownKeys(true)
// splash/static routes
router.HandleFunc("/message", getSplashHandler("splash_message"))
router.HandleFunc("/login", SessionWrap(getSplashFormHandler("login")))
router.HandleFunc("/create", SessionWrap(getSplashFormHandler("create")))
router.HandleFunc("/pwreset", SessionWrap(authPwresetHandler))
router.Handle("/assets/{path:.*}", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets/"))))
router.NotFoundHandler = http.HandlerFunc(splashNotFoundHandler)
// auth routes
router.HandleFunc("/auth/login", SessionWrap(authLoginHandler)).Methods("POST")
router.HandleFunc("/auth/create", SessionWrap(authCreateHandler)).Methods("POST")
router.HandleFunc("/auth/logout", SessionWrap(authLogoutHandler))
router.HandleFunc("/auth/pwreset_request", SessionWrap(authPwresetRequestHandler)).Methods("POST")
router.HandleFunc("/auth/pwreset_submit", SessionWrap(authPwresetSubmitHandler)).Methods("POST")
// panel routes
router.HandleFunc("/panel{slash:/*}", RedirectHandler("/panel/dashboard"))
RegisterPanelHandler("/panel/dashboard", panelDashboard, false)
RegisterPanelHandler("/panel/vms", panelVirtualMachines, false)
RegisterPanelHandler("/panel/newvm", panelNewVM, false)
RegisterPanelHandler("/panel/newvm/{region:[^/]+}", panelNewVMRegion, false)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}", panelVM, false)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/start", panelVMStart, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/stop", panelVMStop, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/reboot", panelVMReboot, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/delete", panelVMDelete, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/action/{action:[^/]+}", panelVMAction, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/vnc", panelVMVnc, false)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/reimage", panelVMReimage, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/rename", panelVMRename, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/snapshot", panelVMSnapshot, true)
RegisterPanelHandler("/panel/vm/{id:[0-9]+}/resize", panelVMResize, true)
RegisterPanelHandler("/panel/billing", panelBilling, false)
RegisterPanelHandler("/panel/pay", panelPay, false)
RegisterPanelHandler("/panel/charges", panelCharges, false)
RegisterPanelHandler("/panel/charges/{year:[0-9]+}/{month:[0-9]+}", panelCharges, false)
RegisterPanelHandler("/panel/account", panelAccount, false)
RegisterPanelHandler("/panel/account/passwd", panelAccountPassword, true)
RegisterPanelHandler("/panel/api/add", panelApiAdd, true)
RegisterPanelHandler("/panel/api/{id:[0-9]+}/remove", panelApiRemove, true)
RegisterPanelHandler("/panel/images", panelImages, false)
RegisterPanelHandler("/panel/images/add", panelImageAdd, true)
RegisterPanelHandler("/panel/image/{id:[0-9]+}", panelImageDetails, false)
RegisterPanelHandler("/panel/image/{id:[0-9]+}/remove", panelImageRemove, true)
RegisterPanelHandler("/panel/keys", panelKeys, false)
RegisterPanelHandler("/panel/keys/add", panelKeyAdd, true)
RegisterPanelHandler("/panel/key/{id:[0-9]+}/remove", panelKeyRemove, true)
RegisterPanelHandler("/panel/csrftoken", panelToken, false)
// api routes
RegisterAPIHandler("/api/vms", apiVMList, "GET")
RegisterAPIHandler("/api/vms", apiVMCreate, "POST")
RegisterAPIHandler("/api/vms/{id:[0-9]+}", apiVMInfo, "GET")
RegisterAPIHandler("/api/vms/{id:[0-9]+}/action", apiVMAction, "POST")
RegisterAPIHandler("/api/vms/{id:[0-9]+}/reimage", apiVMReimage, "POST")
RegisterAPIHandler("/api/vms/{id:[0-9]+}/resize", apiVMResize, "POST")
RegisterAPIHandler("/api/vms/{id:[0-9]+}", apiVMDelete, "DELETE")
RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips", apiVMAddresses, "GET")
RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips/add", apiVMAddressAdd, "POST")
RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips/remove", apiVMAddressRemove, "POST") // use POST instead of DELETE since we need both public/private ip
RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips/{ip:[^/]+}/rdns", apiVMAddressRdns, "POST")
RegisterAPIHandler("/api/images", apiImageList, "GET")
RegisterAPIHandler("/api/images", apiImageFetch, "POST")
RegisterAPIHandler("/api/images/{id:[0-9]+}", apiImageInfo, "GET")
RegisterAPIHandler("/api/images/{id:[0-9]+}", apiImageDelete, "DELETE")
RegisterAPIHandler("/api/plans", apiPlanList, "GET")
RegisterAPIHandler("/api/keys", apiKeyList, "GET")
RegisterAPIHandler("/api/keys", apiKeyAdd, "POST")
RegisterAPIHandler("/api/keys/{id:[0-9]+}", apiKeyRemove, "DELETE")
// admin routes
RegisterAdminHandler("/admin/dashboard", adminDashboard, false)
RegisterAdminHandler("/admin/users", adminUsers, false)
RegisterAdminHandler("/admin/user/{id:[0-9]+}", adminUser, false)
RegisterAdminHandler("/admin/user/{id:[0-9]+}/login", adminUserLogin, true)
RegisterAdminHandler("/admin/user/{id:[0-9]+}/credit", adminUserCredit, true)
RegisterAdminHandler("/admin/user/{id:[0-9]+}/password", adminUserPassword, true)
RegisterAdminHandler("/admin/user/{id:[0-9]+}/disable", adminUserDisable, true)
RegisterAdminHandler("/admin/user/{id:[0-9]+}/enable", adminUserEnable, true)
RegisterAdminHandler("/admin/vms", adminVirtualMachines, false)
RegisterAdminHandler("/admin/vm/{id:[0-9]+}/suspend", adminVMSuspend, true)
RegisterAdminHandler("/admin/vm/{id:[0-9]+}/unsuspend", adminVMUnsuspend, true)
RegisterAdminHandler("/admin/plans", adminPlans, false)
RegisterAdminHandler("/admin/plans/add", adminPlansAdd, true)
RegisterAdminHandler("/admin/plans/autopopulate", adminPlansAutopopulate, true)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}", adminPlan, false)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}/delete", adminPlanDelete, true)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}/enable", adminPlanEnable, true)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}/disable", adminPlanDisable, true)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}/associate", adminPlanAssociateRegion, true)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}/deassociate/{region:[^/]+}", adminPlanDeassociateRegion, true)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}/set", adminPlanSetMetadata, true)
RegisterAdminHandler("/admin/plan/{id:[0-9]+}/unset", adminPlanUnsetMetadata, true)
RegisterAdminHandler("/admin/regions", adminRegions, false)
RegisterAdminHandler("/admin/region/{region:[^/]+}/enable", adminRegionEnable, true)
RegisterAdminHandler("/admin/region/{region:[^/]+}/disable", adminRegionDisable, true)
RegisterAdminHandler("/admin/images", adminImages, false)
RegisterAdminHandler("/admin/images/add", adminImagesAdd, true)
RegisterAdminHandler("/admin/image/{id:[0-9]+}/delete", adminImageDelete, true)
RegisterAdminHandler("/admin/images/autopopulate", adminImagesAutopopulate, true)
// seed math/rand via crypt/rand in case interfaces want to use it for non-secure randomness source
// (math/rand is much faster)
seedBytes := make([]byte, 8)
_, err = crand.Read(seedBytes)
if err != nil {
log.Printf("Warning: failed to seed math/rand: %s", err.Error())
} else {
seed, _ := binary.Varint(seedBytes)
rand.Seed(seed)
}
}
func Run() {
// fake cron routine
go func() {
for {
cron()
time.Sleep(time.Minute)
}
}()
go func() {
for {
cached()
time.Sleep(5 * time.Second)
}
}()
httpServer := &http.Server{
Addr: cfg.Http.Addr,
Handler: LobsterHandler(context.ClearHandler(router)),
}
log.Fatal(httpServer.ListenAndServe())
}
func cron() {
defer errorHandler(nil, nil, true)
vmRows := db.Query("SELECT id FROM vms WHERE time_billed < DATE_SUB(NOW(), INTERVAL ? HOUR)", BILLING_VM_FREQUENCY)
defer vmRows.Close()
for vmRows.Next() {
var vmId int
vmRows.Scan(&vmId)
vmBilling(vmId, false)
}
userRows := db.Query("SELECT id FROM users WHERE last_billing_notify < DATE_SUB(NOW(), INTERVAL ? HOUR)", cfg.BillingNotifications.Frequency)
defer userRows.Close()
for userRows.Next() {
var userId int
userRows.Scan(&userId)
userBilling(userId)
}
serviceBilling()
// cleanup
db.Exec("DELETE FROM form_tokens WHERE time < DATE_SUB(NOW(), INTERVAL 1 HOUR)")
db.Exec("DELETE FROM sessions WHERE active_time < DATE_SUB(NOW(), INTERVAL 1 HOUR)")
db.Exec("DELETE FROM antiflood WHERE time < DATE_SUB(NOW(), INTERVAL 2 HOUR)")
db.Exec("DELETE FROM pwreset_tokens WHERE time < DATE_SUB(NOW(), INTERVAL ? MINUTE)", PWRESET_EXPIRE_MINUTES)
}
func cached() {
defer errorHandler(nil, nil, true)
rows := db.Query("SELECT id, user_id FROM images WHERE status = 'pending' ORDER BY RAND() LIMIT 3")
defer rows.Close()
for rows.Next() {
var imageId, userId int
rows.Scan(&imageId, &userId)
imageInfo := imageInfo(userId, imageId)
if imageInfo != nil {
if imageInfo.Info.Status == ImageError {
db.Exec("UPDATE images SET status = ? WHERE id = ?", "error", imageId)
} else if imageInfo.Info.Status == ImageActive {
db.Exec("UPDATE images SET status = ? WHERE id = ?", "active", imageId)
}
}
}
}