diff options
author | kali kaneko (leap communications) <kali@leap.se> | 2020-09-02 21:38:13 +0200 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2021-05-04 14:58:39 +0200 |
commit | 335bb742b957370bbf40ae77a661559805ab307f (patch) | |
tree | 3d8b9a8023b1bcca2b531bf57b1fd846916f4a8e /pkg/backend/webapi.go | |
parent | 68f566cf7fddbe9e5eb08c4c07a76375148b682b (diff) |
[feat] expose gateway selection in webapi
Diffstat (limited to 'pkg/backend/webapi.go')
-rw-r--r-- | pkg/backend/webapi.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/pkg/backend/webapi.go b/pkg/backend/webapi.go index 568980d..11abc24 100644 --- a/pkg/backend/webapi.go +++ b/pkg/backend/webapi.go @@ -1,11 +1,13 @@ package backend import ( + "encoding/json" "fmt" "log" "net/http" "os" "strconv" + "time" "0xacab.org/leap/bitmask-vpn/pkg/bitmask" ) @@ -36,6 +38,52 @@ func webStatus(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, ctx.Status.String()) } +func webGatewayGet(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, ctx.bm.GetCurrentGateway()) +} + +func webGatewaySet(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "POST": + if err := r.ParseForm(); err != nil { + fmt.Fprintf(w, "ParseForm() err: %v", err) + return + } + gwLabel := r.FormValue("gw") + fmt.Fprintf(w, "selected gateway: %s\n", gwLabel) + // FIXME catch error here, return it (error code) + useGateway(gwLabel) + // TODO make sure we don't tear the fw down on reconnect... + SwitchOff() + // a little sleep is needed, though, because iptables takes some time + time.Sleep(500 * time.Millisecond) + SwitchOn() + default: + fmt.Fprintf(w, "Only POST supported.") + } +} + +func webGatewayList(w http.ResponseWriter, r *http.Request) { + gws, err := ctx.bm.ListGateways(ctx.Provider) + if err != nil { + fmt.Fprintf(w, "ListGateways() err: %v", err) + } + gwJson, _ := json.Marshal(gws) + fmt.Fprintf(w, string(gwJson)) +} + +// TODO +func webTransportGet(w http.ResponseWriter, r *http.Request) { +} + +// TODO +func webTransportSet(w http.ResponseWriter, r *http.Request) { +} + +// TODO +func webTransportList(w http.ResponseWriter, r *http.Request) { +} + func webQuit(w http.ResponseWriter, r *http.Request) { log.Println("Web UI: quit") Quit() @@ -48,6 +96,12 @@ func enableWebAPI(port int) { token := bitmask.ReadAuthToken() http.Handle("/vpn/start", CheckAuth(http.HandlerFunc(webOn), token)) http.Handle("/vpn/stop", CheckAuth(http.HandlerFunc(webOff), token)) + http.Handle("/vpn/gw/get", CheckAuth(http.HandlerFunc(webGatewayGet), token)) + http.Handle("/vpn/gw/set", CheckAuth(http.HandlerFunc(webGatewaySet), token)) + http.Handle("/vpn/gw/list", CheckAuth(http.HandlerFunc(webGatewayList), token)) + //http.Handle("/vpn/transport/get", CheckAuth(http.HandlerFunc(webTransportGet), token)) + //http.Handle("/vpn/transport/set", CheckAuth(http.HandlerFunc(webTransportSet), token)) + //http.Handle("/vpn/transport/list", CheckAuth(http.HandlerFunc(webTransportList), token)) http.Handle("/vpn/status", CheckAuth(http.HandlerFunc(webStatus), token)) http.Handle("/vpn/quit", CheckAuth(http.HandlerFunc(webQuit), token)) http.ListenAndServe(":"+strconv.Itoa(port), nil) |