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.
This commit is contained in:
KhasMek 2018-03-12 18:41:53 -06:00
parent b63c20b757
commit 4b8d4aeb1b
2 changed files with 117 additions and 3 deletions

View file

@ -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 {