forked from LunaNode/lobster
/
lobster.go
338 lines (292 loc) · 13 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
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
type Lobster struct {
router *mux.Router
db *Database
wsMutex sync.Mutex
ws *websockify.Websockify
ssh *wssh.Wssh
}
func LobsterHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 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(db *Database, userId int, ip string, name string, details string) {
db.Exec("INSERT INTO actions (user_id, ip, name, details) VALUES (?, ?, ?, ?)", userId, ip, name, details)
}
func MakeLobster(cfgPath string) *Lobster {
this := new(Lobster)
cfg = LoadConfig(cfgPath)
this.router = mux.NewRouter()
this.db = MakeDatabase()
return this
}
func (this *Lobster) RegisterPanelHandler(path string, f PanelHandlerFunc, onlyPost bool) {
result := this.router.HandleFunc(path, this.db.WrapHandler(SessionWrap(panelWrap(f))))
if onlyPost {
result.Methods("POST")
}
}
func (this *Lobster) RegisterAPIHandler(path string, f APIHandlerFunc, method string) {
this.router.HandleFunc(path, this.db.WrapHandler(apiWrap(f))).Methods(method)
}
func (this *Lobster) RegisterAdminHandler(path string, f AdminHandlerFunc, onlyPost bool) {
result := this.router.HandleFunc(path, this.db.WrapHandler(SessionWrap(adminWrap(f))))
if onlyPost {
result.Methods("POST")
}
}
func (this *Lobster) RegisterHttpHandler(path string, f http.HandlerFunc, onlyPost bool) {
result := this.router.HandleFunc(path, f)
if onlyPost {
result.Methods("POST")
}
}
func (this *Lobster) RegisterVmInterface(region string, vmi VmInterface) {
if regionInterfaces[region] != nil {
log.Fatalf("Duplicate VM interface for region %s", region)
}
regionInterfaces[region] = vmi
}
func (this *Lobster) RegisterPaymentInterface(method string, payInterface PaymentInterface) {
if paymentInterfaces[method] != nil {
log.Fatalf("Duplicate payment interface for method %s", method)
}
paymentInterfaces[method] = payInterface
}
func (this *Lobster) GetConfig() *Config {
return cfg
}
func (this *Lobster) GetDatabase() *Database {
return this.db
}
// Creates websockify instance if not already setup, initializes token, and returns URL to redirect to
func (this *Lobster) HandleWebsockify(ipport string, password string) string {
this.wsMutex.Lock()
defer this.wsMutex.Unlock()
if this.ws == nil {
this.ws = &websockify.Websockify{
Listen: cfg.Novnc.Listen,
}
this.ws.Run()
}
token := this.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 (this *Lobster) HandleWssh(ipport string, username string, password string) string {
this.wsMutex.Lock()
defer this.wsMutex.Unlock()
if this.ssh == nil {
this.ssh = &wssh.Wssh{
Debug: cfg.Default.Debug,
Listen: cfg.Wssh.Listen,
}
this.ssh.Run()
}
token := this.ssh.Register(ipport, username, password)
return strings.Replace(cfg.Wssh.Url, "TOKEN", token, 1)
}
func (this *Lobster) Init() {
lang, err := i18n.LoadFile("language/" + cfg.Default.Language + ".json")
checkErr(err)
LA = lang.S
L = LA("lobster")
loadTemplates()
loadEmail()
decoder = schema.NewDecoder()
decoder.IgnoreUnknownKeys(true)
// splash/static routes
this.router.HandleFunc("/", getSplashHandler("index"))
this.router.HandleFunc("/about", getSplashHandler("about"))
this.router.HandleFunc("/pricing", getSplashHandler("pricing"))
this.router.HandleFunc("/contact", getSplashHandler("contact"))
this.router.HandleFunc("/terms", getSplashHandler("terms"))
this.router.HandleFunc("/privacy", getSplashHandler("privacy"))
this.router.HandleFunc("/message", getSplashHandler("splash_message"))
this.router.HandleFunc("/login", this.db.WrapHandler(SessionWrap(getSplashFormHandler("login"))))
this.router.HandleFunc("/create", this.db.WrapHandler(SessionWrap(getSplashFormHandler("create"))))
this.router.HandleFunc("/pwreset", this.db.WrapHandler(SessionWrap(authPwresetHandler)))
this.router.Handle("/assets/{path:.*}", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets/"))))
this.router.NotFoundHandler = http.HandlerFunc(splashNotFoundHandler)
// auth routes
this.router.HandleFunc("/auth/login", this.db.WrapHandler(SessionWrap(authLoginHandler))).Methods("POST")
this.router.HandleFunc("/auth/create", this.db.WrapHandler(SessionWrap(authCreateHandler))).Methods("POST")
this.router.HandleFunc("/auth/logout", this.db.WrapHandler(SessionWrap(authLogoutHandler)))
this.router.HandleFunc("/auth/pwreset_request", this.db.WrapHandler(SessionWrap(authPwresetRequestHandler))).Methods("POST")
this.router.HandleFunc("/auth/pwreset_submit", this.db.WrapHandler(SessionWrap(authPwresetSubmitHandler))).Methods("POST")
// panel routes
this.router.HandleFunc("/panel{slash:/*}", RedirectHandler("/panel/dashboard"))
this.RegisterPanelHandler("/panel/dashboard", panelDashboard, false)
this.RegisterPanelHandler("/panel/vms", panelVirtualMachines, false)
this.RegisterPanelHandler("/panel/newvm", panelNewVM, false)
this.RegisterPanelHandler("/panel/newvm/{region:[^/]+}", panelNewVMRegion, false)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}", panelVM, false)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/start", panelVMStart, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/stop", panelVMStop, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/reboot", panelVMReboot, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/delete", panelVMDelete, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/action/{action:[^/]+}", panelVMAction, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/vnc", panelVMVnc, false)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/reimage", panelVMReimage, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/rename", panelVMRename, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/snapshot", panelVMSnapshot, true)
this.RegisterPanelHandler("/panel/vm/{id:[0-9]+}/resize", panelVMResize, true)
this.RegisterPanelHandler("/panel/billing", panelBilling, false)
this.RegisterPanelHandler("/panel/pay", panelPay, false)
this.RegisterPanelHandler("/panel/charges", panelCharges, false)
this.RegisterPanelHandler("/panel/charges/{year:[0-9]+}/{month:[0-9]+}", panelCharges, false)
this.RegisterPanelHandler("/panel/account", panelAccount, false)
this.RegisterPanelHandler("/panel/account/passwd", panelAccountPassword, true)
this.RegisterPanelHandler("/panel/api/add", panelApiAdd, true)
this.RegisterPanelHandler("/panel/api/{id:[0-9]+}/remove", panelApiRemove, true)
this.RegisterPanelHandler("/panel/images", panelImages, false)
this.RegisterPanelHandler("/panel/images/add", panelImageAdd, true)
this.RegisterPanelHandler("/panel/image/{id:[0-9]+}", panelImageDetails, false)
this.RegisterPanelHandler("/panel/image/{id:[0-9]+}/remove", panelImageRemove, true)
this.RegisterPanelHandler("/panel/support", panelSupport, false)
this.RegisterPanelHandler("/panel/support/open", panelSupportOpen, false)
this.RegisterPanelHandler("/panel/support/{id:[0-9]+}", panelSupportTicket, false)
this.RegisterPanelHandler("/panel/support/{id:[0-9]+}/reply", panelSupportTicketReply, true)
this.RegisterPanelHandler("/panel/support/{id:[0-9]+}/close", panelSupportTicketClose, true)
this.RegisterPanelHandler("/panel/csrftoken", panelToken, false)
// api routes
this.RegisterAPIHandler("/api/vms", apiVMList, "GET")
this.RegisterAPIHandler("/api/vms", apiVMCreate, "POST")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}", apiVMInfo, "GET")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}/action", apiVMAction, "POST")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}/reimage", apiVMReimage, "POST")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}/resize", apiVMResize, "POST")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}", apiVMDelete, "DELETE")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips", apiVMAddresses, "GET")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips/add", apiVMAddressAdd, "POST")
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips/remove", apiVMAddressRemove, "POST") // use POST instead of DELETE since we need both public/private ip
this.RegisterAPIHandler("/api/vms/{id:[0-9]+}/ips/{ip:[^/]+}/rdns", apiVMAddressRdns, "POST")
this.RegisterAPIHandler("/api/images", apiImageList, "GET")
this.RegisterAPIHandler("/api/images", apiImageFetch, "POST")
this.RegisterAPIHandler("/api/images/{id:[0-9]+}", apiImageInfo, "GET")
this.RegisterAPIHandler("/api/images/{id:[0-9]+}", apiImageDelete, "DELETE")
this.RegisterAPIHandler("/api/plans", apiPlanList, "GET")
// admin routes
this.RegisterAdminHandler("/admin/dashboard", adminDashboard, false)
this.RegisterAdminHandler("/admin/users", adminUsers, false)
this.RegisterAdminHandler("/admin/user/{id:[0-9]+}", adminUser, false)
this.RegisterAdminHandler("/admin/user/{id:[0-9]+}/login", adminUserLogin, true)
this.RegisterAdminHandler("/admin/user/{id:[0-9]+}/credit", adminUserCredit, true)
this.RegisterAdminHandler("/admin/user/{id:[0-9]+}/password", adminUserPassword, true)
this.RegisterAdminHandler("/admin/user/{id:[0-9]+}/disable", adminUserDisable, true)
this.RegisterAdminHandler("/admin/support", adminSupport, false)
this.RegisterAdminHandler("/admin/support/open/{id:[0-9]+}", adminSupportOpen, false)
this.RegisterAdminHandler("/admin/support/{id:[0-9]+}", adminSupportTicket, false)
this.RegisterAdminHandler("/admin/support/{id:[0-9]+}/reply", adminSupportTicketReply, true)
this.RegisterAdminHandler("/admin/support/{id:[0-9]+}/close", adminSupportTicketClose, true)
this.RegisterAdminHandler("/admin/plans", adminPlans, false)
this.RegisterAdminHandler("/admin/plans/add", adminPlansAdd, false)
this.RegisterAdminHandler("/admin/plan/{id:[0-9]+}/delete", adminPlanDelete, true)
this.RegisterAdminHandler("/admin/images", adminImages, false)
this.RegisterAdminHandler("/admin/images/add", adminImagesAdd, false)
this.RegisterAdminHandler("/admin/image/{id:[0-9]+}/delete", adminImageDelete, 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 (this *Lobster) Run() {
// fake cron routine
go func() {
for {
this.cron()
time.Sleep(time.Minute)
}
}()
go func() {
for {
this.cached()
time.Sleep(5 * time.Second)
}
}()
httpServer := &http.Server{
Addr: cfg.Http.Addr,
Handler: LobsterHandler(context.ClearHandler(this.router)),
}
log.Fatal(httpServer.ListenAndServe())
}
func (this *Lobster) cron() {
defer errorHandler(nil, nil, true)
vmRows := this.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(this.db, vmId, false)
}
userRows := this.db.Query("SELECT id FROM users WHERE last_billing_notify < DATE_SUB(NOW(), INTERVAL 24 HOUR)")
defer userRows.Close()
for userRows.Next() {
var userId int
userRows.Scan(&userId)
userBilling(this.db, userId)
}
serviceBilling(this.db)
// cleanup
this.db.Exec("DELETE FROM form_tokens WHERE time < DATE_SUB(NOW(), INTERVAL 1 HOUR)")
this.db.Exec("DELETE FROM sessions WHERE active_time < DATE_SUB(NOW(), INTERVAL 1 HOUR)")
this.db.Exec("DELETE FROM antiflood WHERE time < DATE_SUB(NOW(), INTERVAL 2 HOUR)")
this.db.Exec("DELETE FROM pwreset_tokens WHERE time < DATE_SUB(NOW(), INTERVAL ? MINUTE)", PWRESET_EXPIRE_MINUTES)
}
func (this *Lobster) cached() {
defer errorHandler(nil, nil, true)
rows := this.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(this.db, userId, imageId)
if imageInfo != nil {
if imageInfo.Info.Status == ImageError {
this.db.Exec("UPDATE images SET status = ? WHERE id = ?", "error", imageId)
} else if imageInfo.Info.Status == ImageActive {
this.db.Exec("UPDATE images SET status = ? WHERE id = ?", "active", imageId)
}
}
}
}