From 4b8d4aeb1b534478b801e68d60ed2ef9cb6d4998 Mon Sep 17 00:00:00 2001 From: KhasMek Date: Mon, 12 Mar 2018 18:41:53 -0600 Subject: [PATCH] api: switch request router and add more paths This switches the url router to gorilla and adds the following routes. - /api/events - /api/session - /api/session/ble - /api/session/ble/{mac} - /api/session/env - /api/session/gateway - /api/session/interface - /api/session/lan - /api/session/lan/{mac} - /api/session/options - /api/session/packets - /api/session/started-at - /api/session/wifi - /api/session/wifi/{mac} where {mac} is the mac address of a device. --- modules/api_rest.go | 17 +++++- modules/api_rest_controller.go | 103 ++++++++++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/modules/api_rest.go b/modules/api_rest.go index 920fd044..f54ebdaf 100644 --- a/modules/api_rest.go +++ b/modules/api_rest.go @@ -11,6 +11,7 @@ import ( "github.com/bettercap/bettercap/session" "github.com/bettercap/bettercap/tls" + "github.com/gorilla/mux" "github.com/gorilla/websocket" ) @@ -146,10 +147,22 @@ func (api *RestAPI) Configure() error { api.server.Addr = fmt.Sprintf("%s:%d", ip, port) - router := http.NewServeMux() + router := mux.NewRouter() - router.HandleFunc("/api/session", api.sessionRoute) router.HandleFunc("/api/events", api.eventsRoute) + router.HandleFunc("/api/session", api.sessionRoute) + router.HandleFunc("/api/session/ble", api.sessionRoute) + router.HandleFunc("/api/session/ble/{mac}", api.sessionRoute) + router.HandleFunc("/api/session/env", api.sessionRoute) + router.HandleFunc("/api/session/gateway", api.sessionRoute) + router.HandleFunc("/api/session/interface", api.sessionRoute) + router.HandleFunc("/api/session/lan", api.sessionRoute) + router.HandleFunc("/api/session/lan/{mac}", api.sessionRoute) + router.HandleFunc("/api/session/options", api.sessionRoute) + router.HandleFunc("/api/session/packets", api.sessionRoute) + router.HandleFunc("/api/session/started-at", api.sessionRoute) + router.HandleFunc("/api/session/wifi", api.sessionRoute) + router.HandleFunc("/api/session/wifi/{mac}", api.sessionRoute) api.server.Handler = router diff --git a/modules/api_rest_controller.go b/modules/api_rest_controller.go index 50e7caac..05bd28eb 100644 --- a/modules/api_rest_controller.go +++ b/modules/api_rest_controller.go @@ -11,6 +11,7 @@ import ( "github.com/bettercap/bettercap/log" "github.com/bettercap/bettercap/session" + "github.com/gorilla/mux" "github.com/gorilla/websocket" ) @@ -67,6 +68,73 @@ func (api *RestAPI) showSession(w http.ResponseWriter, r *http.Request) { toJSON(w, session.I) } +func (api *RestAPI) showBle(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.BLE) +} + +func (api *RestAPI) showBleEndpoint(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + mac := strings.ToLower(params["mac"]) + if dev, found := session.I.BLE.Get(mac); found == true { + toJSON(w, dev) + } +} + +func (api *RestAPI) showEnv(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.Env) +} + +func (api *RestAPI) showGateway(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.Gateway) +} + +func (api *RestAPI) showInterface(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.Interface) +} + +func (api *RestAPI) showLan(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.Lan) +} + +func (api *RestAPI) showLanEndpoint(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + mac := strings.ToLower(params["mac"]) + if host, found := session.I.Lan.Get(mac); found == true { + toJSON(w, host) + } +} + +func (api *RestAPI) showOptions(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.Options) +} + +func (api *RestAPI) showPackets(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.Queue) +} + +func (api *RestAPI) showStartedAt(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.StartedAt) +} + +func (api *RestAPI) showWiFi(w http.ResponseWriter, r *http.Request) { + toJSON(w, session.I.WiFi) +} + +func (api *RestAPI) showWiFiEndpoint(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + mac := strings.ToLower(params["mac"]) + if station, found := session.I.WiFi.Get(mac); found == true { + toJSON(w, station) + // cycle through station clients if not a station. + } else { + for _, ap := range session.I.WiFi.List() { + if client, found := ap.Get(mac); found == true { + toJSON(w, client) + } + } + } +} + func (api *RestAPI) runSessionCommand(w http.ResponseWriter, r *http.Request) { var err error var cmd CommandRequest @@ -211,7 +279,40 @@ func (api *RestAPI) sessionRoute(w http.ResponseWriter, r *http.Request) { if api.checkAuth(r) == false { setAuthFailed(w, r) } else if r.Method == "GET" { - api.showSession(w, r) + params := mux.Vars(r) + if r.URL.String() == "/api/session" { + api.showSession(w, r) + } else if strings.HasPrefix(r.URL.String(), "/api/session/ble") { + if params["mac"] != "" { + api.showBleEndpoint(w, r) + } else { + api.showBle(w, r) + } + } else if r.URL.String() == "/api/session/env" { + api.showEnv(w, r) + } else if r.URL.String() == "/api/session/gateway" { + api.showGateway(w, r) + } else if r.URL.String() == "/api/session/interface" { + api.showInterface(w, r) + } else if strings.HasPrefix(r.URL.String(), "/api/session/lan") { + if params["mac"] != "" { + api.showLanEndpoint(w, r) + } else { + api.showLan(w, r) + } + } else if r.URL.String() == "/api/session/options" { + api.showOptions(w, r) + } else if r.URL.String() == "/api/session/packets" { + api.showPackets(w, r) + } else if r.URL.String() == "/api/session/started-at" { + api.showStartedAt(w, r) + } else if strings.HasPrefix(r.URL.String(), "/api/session/wifi") { + if params["mac"] != "" { + api.showWiFiEndpoint(w, r) + } else { + api.showWiFi(w, r) + } + } } else if r.Method == "POST" { api.runSessionCommand(w, r) } else {