summaryrefslogtreecommitdiff
path: root/pkg/backend
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2020-09-02 21:38:13 +0200
committerkali kaneko (leap communications) <kali@leap.se>2021-05-04 14:58:39 +0200
commit335bb742b957370bbf40ae77a661559805ab307f (patch)
tree3d8b9a8023b1bcca2b531bf57b1fd846916f4a8e /pkg/backend
parent68f566cf7fddbe9e5eb08c4c07a76375148b682b (diff)
[feat] expose gateway selection in webapi
Diffstat (limited to 'pkg/backend')
-rw-r--r--pkg/backend/actions.go20
-rw-r--r--pkg/backend/api.go10
-rw-r--r--pkg/backend/webapi.go54
3 files changed, 84 insertions, 0 deletions
diff --git a/pkg/backend/actions.go b/pkg/backend/actions.go
index e45b026..6e143f9 100644
--- a/pkg/backend/actions.go
+++ b/pkg/backend/actions.go
@@ -19,3 +19,23 @@ func stopVPN() {
log.Println(err)
}
}
+
+// TODO return bool?
+func useGateway(label string) {
+ err := ctx.bm.UseGateway(label)
+ if err != nil {
+ log.Println(err)
+ }
+}
+
+func getGateway() string {
+ return ctx.bm.GetCurrentGateway()
+}
+
+// TODO return bool?
+func useTransport(transport string) {
+ err := ctx.bm.UseTransport(transport)
+ if err != nil {
+ log.Println(err)
+ }
+}
diff --git a/pkg/backend/api.go b/pkg/backend/api.go
index 293dd9e..8d6d049 100644
--- a/pkg/backend/api.go
+++ b/pkg/backend/api.go
@@ -54,6 +54,16 @@ func SwitchOff() {
go stopVPN()
}
+// TODO implement Reconnect?
+
+func UseGateway(label string) {
+ ctx.bm.UseGateway(label)
+}
+
+func UseTransport(label string) {
+ ctx.bm.UseTransport(label)
+}
+
func Quit() {
if ctx.autostart != nil {
ctx.autostart.Disable()
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)