mirror of
https://github.com/bettercap/bettercap
synced 2025-08-21 05:53:20 -07:00
new: implemented GET /api/events route (ref #5).
This commit is contained in:
parent
ee4b783015
commit
269d7d845b
14 changed files with 172 additions and 38 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/evilsocket/bettercap-ng/core"
|
||||
|
@ -20,7 +21,7 @@ type RestAPI struct {
|
|||
|
||||
func NewRestAPI(s *session.Session) *RestAPI {
|
||||
api := &RestAPI{
|
||||
SessionModule: session.NewSessionModule(s),
|
||||
SessionModule: session.NewSessionModule("api.rest", s),
|
||||
server: &http.Server{},
|
||||
username: "",
|
||||
password: "",
|
||||
|
@ -58,6 +59,7 @@ func NewRestAPI(s *session.Session) *RestAPI {
|
|||
}))
|
||||
|
||||
http.HandleFunc("/api/session", api.sessRoute)
|
||||
http.HandleFunc("/api/events", api.eventsRoute)
|
||||
|
||||
return api
|
||||
}
|
||||
|
@ -113,6 +115,45 @@ func (api *RestAPI) sessRoute(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func (api *RestAPI) eventsRoute(w http.ResponseWriter, r *http.Request) {
|
||||
if api.checkAuth(w, r) == false {
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "GET" {
|
||||
var err error
|
||||
|
||||
events := api.Session.Events.Events()
|
||||
nmax := len(events)
|
||||
n := nmax
|
||||
|
||||
keys, ok := r.URL.Query()["n"]
|
||||
if len(keys) == 1 && ok {
|
||||
sn := keys[0]
|
||||
n, err = strconv.Atoi(sn)
|
||||
if err == nil {
|
||||
if n > nmax {
|
||||
n = nmax
|
||||
}
|
||||
} else {
|
||||
n = nmax
|
||||
}
|
||||
}
|
||||
|
||||
js, err := json.Marshal(events[0:n])
|
||||
if err != nil {
|
||||
log.Errorf("Error while returning events: %s", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(js)
|
||||
} else {
|
||||
http.Error(w, "Not Found", 404)
|
||||
}
|
||||
}
|
||||
|
||||
func (api RestAPI) checkAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||
if api.Authenticated(w, r) == false {
|
||||
log.Warningf("Unauthenticated access!")
|
||||
|
|
|
@ -17,7 +17,7 @@ type ArpSpoofer struct {
|
|||
|
||||
func NewArpSpoofer(s *session.Session) *ArpSpoofer {
|
||||
p := &ArpSpoofer{
|
||||
SessionModule: session.NewSessionModule(s),
|
||||
SessionModule: session.NewSessionModule("arp.spoof", s),
|
||||
Done: make(chan bool),
|
||||
}
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ func (p HttpProxy) logAction(req *http.Request, jsres *JSResponse) {
|
|||
|
||||
func NewHttpProxy(s *session.Session) *HttpProxy {
|
||||
p := &HttpProxy{
|
||||
SessionModule: session.NewSessionModule(s),
|
||||
SessionModule: session.NewSessionModule("http.proxy", s),
|
||||
proxy: nil,
|
||||
address: "",
|
||||
redirection: nil,
|
||||
|
|
|
@ -15,7 +15,7 @@ type Prober struct {
|
|||
|
||||
func NewProber(s *session.Session) *Prober {
|
||||
p := &Prober{
|
||||
SessionModule: session.NewSessionModule(s),
|
||||
SessionModule: session.NewSessionModule("net.probe", s),
|
||||
}
|
||||
|
||||
p.AddParam(session.NewIntParameter("net.probe.throttle",
|
||||
|
|
|
@ -18,7 +18,7 @@ type Discovery struct {
|
|||
|
||||
func NewDiscovery(s *session.Session) *Discovery {
|
||||
d := &Discovery{
|
||||
SessionModule: session.NewSessionModule(s),
|
||||
SessionModule: session.NewSessionModule("net.recon", s),
|
||||
|
||||
refresh: 1,
|
||||
before: nil,
|
||||
|
|
|
@ -117,7 +117,7 @@ type Sniffer struct {
|
|||
|
||||
func NewSniffer(s *session.Session) *Sniffer {
|
||||
sniff := &Sniffer{
|
||||
SessionModule: session.NewSessionModule(s),
|
||||
SessionModule: session.NewSessionModule("net.sniffer", s),
|
||||
Stats: nil,
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue