-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccel-ppp-webd.go
629 lines (576 loc) · 14.8 KB
/
accel-ppp-webd.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
golang backend for ACCEL-PPP WEB
SPDX-License-Identifier: LGPL-2.1-or-later
(c) 2024 Denys Fedoryshchenko <denys.f@collabora.com>
*/
package main
import (
"bufio"
"encoding/json"
"log"
"log/syslog"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
// jwt https://github.com/golang-jwt/jwt
"flag"
jwt "github.com/golang-jwt/jwt"
)
var jwtSecret string
var bindaddr string
var noauth bool
func getFileContent(filename string) string {
// verify if file exists
_, err := os.Stat(filename)
if os.IsNotExist(err) {
log.Println("File not found: " + filename)
return ""
}
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// read all lines
//scanner := bufio.NewScanner(file)
//scanner.Scan()
//return scanner.Text()
content := ""
scanner := bufio.NewScanner(file)
for scanner.Scan() {
content += scanner.Text()
content += "\n"
}
return content
}
func verifyToken(jwtdata string) bool {
// parse token
token, err := jwt.Parse(jwtdata, func(token *jwt.Token) (interface{}, error) {
return []byte(jwtSecret), nil
})
if err != nil {
return false
}
// check if token is valid
if token.Valid {
return true
}
return false
}
func verifyAuth(w http.ResponseWriter, r *http.Request) bool {
if noauth {
return true
}
// check cookie
cookie, err := r.Cookie("jwt")
if err != nil {
//http.Error(w, "No cookie", http.StatusUnauthorized)
// redirect to login.html
http.Redirect(w, r, "/login.html", http.StatusSeeOther)
return false
}
// check token
if !verifyToken(cookie.Value) {
//http.Error(w, "Invalid token", http.StatusUnauthorized)
// redirect to login.html
http.Redirect(w, r, "/login.html", http.StatusSeeOther)
return false
}
return true
}
func execCommand(command string) string {
// execute command and collect output
cmd := exec.Command("sh", "-c", command)
out, err := cmd.Output()
if err != nil {
log.Fatal("execCommand() error: ", err)
}
return string(out)
}
type Session struct {
Ifname string `json:"ifname"`
User string `json:"user"`
Mac string `json:"mac"`
Ip string `json:"ip"`
Proto string `json:"proto"`
Comp string `json:"comp"`
State string `json:"state"`
Uptime string `json:"uptime"`
}
type Sysinfo struct {
Accelstats string `json:"accelstats"`
Accelversion string `json:"accelversion"`
Systemload float32 `json:"systemload"`
Sessions []Session `json:"sessions"`
}
func convertStringToFloat(s string) float32 {
f, err := strconv.ParseFloat(s, 32)
if err != nil {
log.Fatal(err)
}
return float32(f)
}
func getSessions() []Session {
// get sessions
accelStats := execCommand("accel-cmd show sessions")
lines := strings.Split(accelStats, "\n")
sessions := make([]Session, 0)
count := 0
for _, line := range lines {
// split by | and remove spaces, field might be empty!
// like comp in typical pppoe setup is empty
fields := strings.FieldsFunc(line, func(r rune) bool {
return r == '|'
})
if len(fields) < 8 {
continue
}
// skip first line
if count == 0 {
count++
continue
}
// iterate over fields and strip spaces
for i, field := range fields {
fields[i] = strings.TrimSpace(field)
}
session := Session{fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6], fields[7]}
sessions = append(sessions, session)
}
return sessions
}
func getCoreCount() int {
// get number of cores
snapshot1 := getFileContent("/proc/cpuinfo")
fields1 := strings.Fields(snapshot1)
cores := 0
for _, field := range fields1 {
if field == "processor" {
cores++
}
}
return cores
}
type Load struct {
Total uint64
Idle uint64
}
type LoadSnapshot struct {
TotalLoad Load
CoreLoad []Load
}
func loadProcessLine(line string) (string, []uint64) {
// split by space
fields := strings.Fields(line)
// get process name
process := fields[0]
// get process load
loads := make([]uint64, 0)
for i := 1; i < len(fields); i++ {
load, err := strconv.ParseUint(fields[i], 10, 64)
if err != nil {
log.Fatal(err)
}
loads = append(loads, load)
}
return process, loads
}
func getCoreLoad(loads []uint64) Load {
var load Load
// 3rd field is idle
numfields := len(loads)
for i := 0; i < numfields; i++ {
if i == 3 {
load.Idle = loads[i]
} else {
load.Total += loads[i]
}
}
return load
}
func CoreLoadSnapshot() LoadSnapshot {
var totalload LoadSnapshot
// get number of cores
fh, err := os.Open("/proc/stat")
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(fh)
linen := 0
for scanner.Scan() {
line := scanner.Text()
linen++
// total
if linen == 1 {
// total
_, loads := loadProcessLine(line)
for i := 0; i < len(loads); i++ {
if i == 3 {
totalload.TotalLoad.Idle = loads[i]
} else {
totalload.TotalLoad.Total += loads[i]
}
}
} else {
// core
procname, loads := loadProcessLine(line)
// check if it's core (starts with cpu)
if strings.HasPrefix(procname, "cpu") {
coreload := getCoreLoad(loads)
totalload.CoreLoad = append(totalload.CoreLoad, coreload)
}
}
}
return totalload
}
func getSystemload() float32 {
var diffLoad LoadSnapshot
// snapshots with delay of 1 sec
snapshot1 := CoreLoadSnapshot()
time.Sleep(1 * time.Second)
snapshot2 := CoreLoadSnapshot()
// calculate diff
diffLoad.TotalLoad.Total = snapshot2.TotalLoad.Total - snapshot1.TotalLoad.Total
diffLoad.TotalLoad.Idle = snapshot2.TotalLoad.Idle - snapshot1.TotalLoad.Idle
numcores := len(snapshot1.CoreLoad)
if numcores != len(snapshot2.CoreLoad) {
log.Fatal("Number of cores mismatch")
}
if numcores == 0 {
log.Fatal("No cores found?")
}
for i := 0; i < numcores; i++ {
diffLoad.CoreLoad = append(diffLoad.CoreLoad, Load{})
diffLoad.CoreLoad[i].Total = snapshot2.CoreLoad[i].Total - snapshot1.CoreLoad[i].Total
}
sumTotal := diffLoad.TotalLoad.Total + diffLoad.TotalLoad.Idle
totalCPUbusy := diffLoad.TotalLoad.Total * 100 / sumTotal
return convertStringToFloat(strconv.FormatUint(totalCPUbusy, 10))
}
func handlerSysinfo(w http.ResponseWriter, r *http.Request) {
accelStats := execCommand("accel-cmd show stat")
// accel-cmd -V
accelVersion := execCommand("accel-cmd -V")
// get CPU load
systemLoad := getSystemload()
// pack in accelstats json
sessions := getSessions()
sysinfo := Sysinfo{accelStats, accelVersion, systemLoad, sessions}
// output json
json.NewEncoder(w).Encode(sysinfo)
}
func handleTerm(w http.ResponseWriter, r *http.Request) {
ifname := r.URL.Query().Get("ifname")
if ifname == "" {
http.Error(w, "No ifname specified", http.StatusBadRequest)
return
}
re := regexp.MustCompile("^[a-z0-9]+$")
if !re.MatchString(ifname) {
http.Error(w, "Invalid ifname", http.StatusBadRequest)
return
}
execCommand("accel-cmd terminate if " + ifname)
// return json result=ok
output := make(map[string]string)
output["result"] = "ok"
json.NewEncoder(w).Encode(output)
}
func handleLive(w http.ResponseWriter, r *http.Request) {
// return live.html
http.ServeFile(w, r, "live.html")
}
func handleStat(w http.ResponseWriter, r *http.Request) {
ifname := r.URL.Query().Get("ifname")
if ifname == "" {
http.Error(w, "No ifname specified", http.StatusBadRequest)
return
}
// sanitize ifname ^[a-z0-9]+$
re := regexp.MustCompile("^[a-z0-9]+$")
if !re.MatchString(ifname) {
http.Error(w, "Invalid ifname", http.StatusBadRequest)
return
}
// /sys/class/net/ppp100/statistics/rx_bytes and tx_bytes
rxBytes := getFileContent("/sys/class/net/" + ifname + "/statistics/rx_bytes")
txBytes := getFileContent("/sys/class/net/" + ifname + "/statistics/tx_bytes")
unixTime := time.Now().Unix()
// pack in json
output := make(map[string]string)
output["rx_bytes"] = rxBytes
output["tx_bytes"] = txBytes
output["timestamp"] = strconv.FormatInt(unixTime, 10)
// output json
json.NewEncoder(w).Encode(output)
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
// is POST?
if r.Method != "POST" {
http.Error(w, "Invalid method", http.StatusBadRequest)
return
}
// is Content-Type: application/json?
if r.Header["Content-Type"] == nil {
http.Error(w, "No Content-Type header", http.StatusBadRequest)
return
}
if r.Header["Content-Type"][0] != "application/json" {
http.Error(w, "Invalid Content-Type", http.StatusBadRequest)
return
}
// parse json
var data map[string]string
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// is token present?
if data["token"] == "" {
http.Error(w, "No token", http.StatusBadRequest)
return
}
// check token
if !verifyToken(data["token"]) {
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
// set token in cookie
http.SetCookie(w, &http.Cookie{
Name: "jwt",
Value: data["token"],
HttpOnly: true,
Path: "/",
})
// return json result=ok
output := make(map[string]string)
output["result"] = "ok"
json.NewEncoder(w).Encode(output)
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
// remove cookie
http.SetCookie(w, &http.Cookie{
Name: "jwt",
Value: "",
HttpOnly: true,
Expires: time.Unix(0, 0),
})
// return json result=ok
output := make(map[string]string)
output["result"] = "ok"
json.NewEncoder(w).Encode(output)
}
type IfactionResult struct {
Result string `json:"result"`
Content string `json:"content"`
}
func handlerIfaction(w http.ResponseWriter, r *http.Request) {
var result IfactionResult
ifname := r.URL.Query().Get("ifname")
action := r.URL.Query().Get("action")
if ifname == "" {
result.Result = "error"
result.Content = "No ifname specified"
json.NewEncoder(w).Encode(result)
http.Error(w, "No ifname specified", http.StatusBadRequest)
return
}
if action == "" {
result.Result = "error"
result.Content = "No action specified"
json.NewEncoder(w).Encode(result)
http.Error(w, "No action specified", http.StatusBadRequest)
return
}
// sanitize ifname ^[a-z0-9]+$
re := regexp.MustCompile("^[a-z0-9]+$")
if !re.MatchString(ifname) {
result.Result = "error"
result.Content = "Invalid ifname"
json.NewEncoder(w).Encode(result)
http.Error(w, "Invalid ifname", http.StatusBadRequest)
return
}
_, err := os.Stat("/sys/class/net/" + ifname)
if os.IsNotExist(err) {
result.Result = "error"
result.Content = "Interface not found"
json.NewEncoder(w).Encode(result)
http.Error(w, "Interface not found", http.StatusBadRequest)
return
}
// action - showshaper
if action == "shaperinfo" {
//verify if interface exists
content := ""
output := execCommand("tc qdisc show dev " + ifname)
content += "Qdisc:\n" + output + "\n"
output = execCommand("tc class show dev " + ifname)
content += "Class:\n" + output + "\n"
output = execCommand("tc filter show dev " + ifname)
content += "Filter:\n" + output + "\n"
result.Result = "ok"
result.Content = content
json.NewEncoder(w).Encode(result)
return
}
// action - showrad (/var/run/radattr.<ifname>)
if action == "showrad" {
content := getFileContent("/var/run/radattr." + ifname)
result.Result = "ok"
result.Content = content
json.NewEncoder(w).Encode(result)
return
}
}
func verifyStart() string {
// is accel-cmd available?
_, err := exec.LookPath("accel-cmd")
if err != nil {
return "accel-cmd not found"
}
// is accel-cmd return valid output?
out := execCommand("accel-cmd show stat")
if out == "" {
return "accel-cmd show stat returned empty output"
}
return ""
}
func genJWTToken(username string) {
// generate JWT token for user
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["username"] = username
claims["exp"] = time.Now().Add(time.Hour * 24 * 365).Unix()
tokenString, err := token.SignedString([]byte(jwtSecret))
if err != nil {
log.Fatal(err)
}
log.Println(tokenString)
}
func setLogSyslog() {
// set log to syslog
logwriter, err := syslog.New(syslog.LOG_NOTICE, "accel-ppp-webd")
if err != nil {
log.Fatal(err)
}
log.SetOutput(logwriter)
}
func setLogFilename(filename string) {
// set log to file
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
log.SetOutput(file)
}
func main() {
var genuser string
var stderr bool
var logfilename string
// arguments:
// -jwtsecret <secret>
flag.StringVar(&jwtSecret, "jwtsecret", "", "JWT secret")
// -bindaddr <ip:port>
flag.StringVar(&bindaddr, "bindaddr", ":8080", "Bind address")
// -noauth
flag.BoolVar(&noauth, "noauth", false, "Disable authentication")
// -genuser <username>
flag.StringVar(&genuser, "genuser", "", "Generate JWT token for username")
// -stderr
flag.BoolVar(&stderr, "stderr", false, "Log to stderr or file, otherwise to syslog(default)")
// -logfilename <filename>
flag.StringVar(&logfilename, "logfile", "", "Log filename")
flag.Parse()
// generate JWT token for user
if genuser != "" {
genJWTToken(genuser)
return
}
if logfilename != "" {
setLogFilename(logfilename)
} else if stderr {
log.SetOutput(os.Stderr)
} else {
setLogSyslog()
}
// check if start requirements are met
err := verifyStart()
if err != "" {
log.Fatal(err)
}
/*
http.HandleFunc("/login.html", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "login.html")
})
*/
// /api/ifaction?ifname=<ifname>&action=<command>
http.HandleFunc("/api/ifaction", func(w http.ResponseWriter, r *http.Request) {
if !verifyAuth(w, r) {
return
}
handlerIfaction(w, r)
})
http.HandleFunc("/api/sysinfo", func(w http.ResponseWriter, r *http.Request) {
if !verifyAuth(w, r) {
return
}
handlerSysinfo(w, r)
})
http.HandleFunc("/api/terminate", func(w http.ResponseWriter, r *http.Request) {
if !verifyAuth(w, r) {
return
}
handleTerm(w, r)
})
http.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) {
if !verifyAuth(w, r) {
return
}
handleLive(w, r)
})
http.HandleFunc("/api/stat", func(w http.ResponseWriter, r *http.Request) {
if !verifyAuth(w, r) {
return
}
handleStat(w, r)
})
// /api/login verify token in body as json {token: <token>} and set it in cookie as httponly
http.HandleFunc("/api/login", func(w http.ResponseWriter, r *http.Request) {
handleLogin(w, r)
})
// /api/logout
http.HandleFunc("/api/logout", func(w http.ResponseWriter, r *http.Request) {
handleLogout(w, r)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// always allow /login.html
if r.URL.Path == "/login.html" {
http.ServeFile(w, r, "login.html")
return
}
if !verifyAuth(w, r) {
return
}
// if requested *.html check if file exists
if strings.HasSuffix(r.URL.Path, ".html") {
fname := filepath.Base(r.URL.Path)
_, err := os.Stat(fname)
if os.IsNotExist(err) {
http.Error(w, "File not found", http.StatusNotFound)
return
} else {
http.ServeFile(w, r, r.URL.Path[1:])
return
}
}
http.ServeFile(w, r, "index.html")
})
log.Fatal(http.ListenAndServe(bindaddr, nil))
}