summaryrefslogtreecommitdiff
path: root/pkg/backend/webapi.go
blob: a8844e82e0ca4365faa5f3b65741702b43adee83 (plain)
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
package backend

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"0xacab.org/leap/bitmask-vpn/pkg/bitmask"
)

func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
	for _, adapter := range adapters {
		h = adapter(h)
	}
	return h
}

type Adapter func(http.Handler) http.Handler

func CheckAuth(token string) Adapter {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			t := r.Header.Get("X-Auth-Token")
			if t == token {
				h.ServeHTTP(w, r)
			} else {
				w.WriteHeader(http.StatusUnauthorized)
				w.Write([]byte("401 - Unauthorized"))
			}

		})
	}
}

func webOn(w http.ResponseWriter, r *http.Request) {
	log.Println("Web UI: on")
	SwitchOn()
}

func webOff(w http.ResponseWriter, r *http.Request) {
	log.Println("Web UI: off")
	SwitchOff()
}

func webStatus(w http.ResponseWriter, r *http.Request) {
	log.Println("Web UI: status")
	fmt.Fprintf(w, ctx.Status.String())
}

func webQuit(w http.ResponseWriter, r *http.Request) {
	log.Println("Web UI: quit")
	Quit()
	os.Exit(0)
}

func enableWebAPI() {
	bitmask.GenerateAuthToken()
	auth := CheckAuth(bitmask.ReadAuthToken())
	http.Handle("/vpn/start", Adapt(http.HandlerFunc(webOn), auth))
	http.Handle("/vpn/stop", Adapt(http.HandlerFunc(webOff), auth))
	http.Handle("/vpn/status", Adapt(http.HandlerFunc(webStatus), auth))
	http.Handle("/vpn/quit", Adapt(http.HandlerFunc(webQuit), auth))
	http.ListenAndServe(":8080", nil)
}