misc: each module now has its own tagged logging

This commit is contained in:
evilsocket 2019-02-12 15:16:02 +01:00
commit 9cd4e380fb
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
47 changed files with 343 additions and 349 deletions

View file

@ -6,7 +6,6 @@ import (
"net/http"
"time"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/session"
"github.com/bettercap/bettercap/tls"
@ -157,15 +156,15 @@ func (api *RestAPI) Configure() error {
return err
}
log.Debug("%+v", cfg)
log.Info("generating TLS key to %s", api.keyFile)
log.Info("generating TLS certificate to %s", api.certFile)
api.Debug("%+v", cfg)
api.Info("generating TLS key to %s", api.keyFile)
api.Info("generating TLS certificate to %s", api.certFile)
if err := tls.Generate(cfg, api.certFile, api.keyFile); err != nil {
return err
}
} else {
log.Info("loading TLS key from %s", api.keyFile)
log.Info("loading TLS certificate from %s", api.certFile)
api.Info("loading TLS key from %s", api.keyFile)
api.Info("loading TLS certificate from %s", api.certFile)
}
}
@ -192,7 +191,7 @@ func (api *RestAPI) Configure() error {
api.server.Handler = router
if api.username == "" || api.password == "" {
log.Warning("api.rest.username and/or api.rest.password parameters are empty, authentication is disabled.")
api.Warning("api.rest.username and/or api.rest.password parameters are empty, authentication is disabled.")
}
return nil
@ -207,10 +206,10 @@ func (api *RestAPI) Start() error {
var err error
if api.isTLS() {
log.Info("api server starting on https://%s", api.server.Addr)
api.Info("api server starting on https://%s", api.server.Addr)
err = api.server.ListenAndServeTLS(api.certFile, api.keyFile)
} else {
log.Info("api server starting on http://%s", api.server.Addr)
api.Info("api server starting on http://%s", api.server.Addr)
err = api.server.ListenAndServe()
}

View file

@ -7,7 +7,6 @@ import (
"strconv"
"strings"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/session"
"github.com/gorilla/mux"
@ -22,18 +21,18 @@ type APIResponse struct {
Message string `json:"msg"`
}
func setAuthFailed(w http.ResponseWriter, r *http.Request) {
log.Warning("Unauthorized authentication attempt from %s", r.RemoteAddr)
func (api *RestAPI) setAuthFailed(w http.ResponseWriter, r *http.Request) {
api.Warning("Unauthorized authentication attempt from %s", r.RemoteAddr)
w.Header().Set("WWW-Authenticate", `Basic realm="auth"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorized"))
}
func toJSON(w http.ResponseWriter, o interface{}) {
func (api *RestAPI) toJSON(w http.ResponseWriter, o interface{}) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(o); err != nil {
log.Error("error while encoding object to JSON: %v", err)
api.Error("error while encoding object to JSON: %v", err)
}
}
@ -59,7 +58,7 @@ func (api *RestAPI) checkAuth(r *http.Request) bool {
}
func (api *RestAPI) showSession(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I)
api.toJSON(w, session.I)
}
func (api *RestAPI) showBle(w http.ResponseWriter, r *http.Request) {
@ -67,28 +66,28 @@ func (api *RestAPI) showBle(w http.ResponseWriter, r *http.Request) {
mac := strings.ToLower(params["mac"])
if mac == "" {
toJSON(w, session.I.BLE)
api.toJSON(w, session.I.BLE)
} else if dev, found := session.I.BLE.Get(mac); found {
toJSON(w, dev)
api.toJSON(w, dev)
} else {
http.Error(w, "Not Found", 404)
}
}
func (api *RestAPI) showEnv(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I.Env)
api.toJSON(w, session.I.Env)
}
func (api *RestAPI) showGateway(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I.Gateway)
api.toJSON(w, session.I.Gateway)
}
func (api *RestAPI) showInterface(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I.Interface)
api.toJSON(w, session.I.Interface)
}
func (api *RestAPI) showModules(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I.Modules)
api.toJSON(w, session.I.Modules)
}
func (api *RestAPI) showLan(w http.ResponseWriter, r *http.Request) {
@ -96,24 +95,24 @@ func (api *RestAPI) showLan(w http.ResponseWriter, r *http.Request) {
mac := strings.ToLower(params["mac"])
if mac == "" {
toJSON(w, session.I.Lan)
api.toJSON(w, session.I.Lan)
} else if host, found := session.I.Lan.Get(mac); found {
toJSON(w, host)
api.toJSON(w, host)
} else {
http.Error(w, "Not Found", 404)
}
}
func (api *RestAPI) showOptions(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I.Options)
api.toJSON(w, session.I.Options)
}
func (api *RestAPI) showPackets(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I.Queue)
api.toJSON(w, session.I.Queue)
}
func (api *RestAPI) showStartedAt(w http.ResponseWriter, r *http.Request) {
toJSON(w, session.I.StartedAt)
api.toJSON(w, session.I.StartedAt)
}
func (api *RestAPI) showWiFi(w http.ResponseWriter, r *http.Request) {
@ -121,11 +120,11 @@ func (api *RestAPI) showWiFi(w http.ResponseWriter, r *http.Request) {
mac := strings.ToLower(params["mac"])
if mac == "" {
toJSON(w, session.I.WiFi)
api.toJSON(w, session.I.WiFi)
} else if station, found := session.I.WiFi.Get(mac); found {
toJSON(w, station)
api.toJSON(w, station)
} else if client, found := session.I.WiFi.GetClient(mac); found {
toJSON(w, client)
api.toJSON(w, client)
} else {
http.Error(w, "Not Found", 404)
}
@ -142,7 +141,7 @@ func (api *RestAPI) runSessionCommand(w http.ResponseWriter, r *http.Request) {
} else if err = session.I.Run(cmd.Command); err != nil {
http.Error(w, err.Error(), 400)
} else {
toJSON(w, APIResponse{Success: true})
api.toJSON(w, APIResponse{Success: true})
}
}
@ -170,7 +169,7 @@ func (api *RestAPI) showEvents(w http.ResponseWriter, r *http.Request) {
}
}
toJSON(w, events[nevents-n:])
api.toJSON(w, events[nevents-n:])
}
}
@ -182,7 +181,7 @@ func (api *RestAPI) sessionRoute(w http.ResponseWriter, r *http.Request) {
api.setSecurityHeaders(w)
if !api.checkAuth(r) {
setAuthFailed(w, r)
api.setAuthFailed(w, r)
return
} else if r.Method == "POST" {
api.runSessionCommand(w, r)
@ -239,7 +238,7 @@ func (api *RestAPI) eventsRoute(w http.ResponseWriter, r *http.Request) {
api.setSecurityHeaders(w)
if !api.checkAuth(r) {
setAuthFailed(w, r)
api.setAuthFailed(w, r)
return
}

View file

@ -6,7 +6,6 @@ import (
"strings"
"time"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/session"
"github.com/gorilla/websocket"
@ -24,14 +23,14 @@ const (
func (api *RestAPI) streamEvent(ws *websocket.Conn, event session.Event) error {
msg, err := json.Marshal(event)
if err != nil {
log.Error("Error while creating websocket message: %s", err)
api.Error("Error while creating websocket message: %s", err)
return err
}
ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := ws.WriteMessage(websocket.TextMessage, msg); err != nil {
if !strings.Contains(err.Error(), "closed connection") {
log.Error("Error while writing websocket message: %s", err)
api.Error("Error while writing websocket message: %s", err)
return err
}
}
@ -42,7 +41,7 @@ func (api *RestAPI) streamEvent(ws *websocket.Conn, event session.Event) error {
func (api *RestAPI) sendPing(ws *websocket.Conn) error {
ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
log.Error("Error while writing websocket ping message: %s", err)
api.Error("Error while writing websocket ping message: %s", err)
return err
}
return nil
@ -55,7 +54,7 @@ func (api *RestAPI) streamWriter(ws *websocket.Conn, w http.ResponseWriter, r *h
events := session.I.Events.Sorted()
n := len(events)
if n > 0 {
log.Debug("Sending %d events.", n)
api.Debug("Sending %d events.", n)
for _, event := range events {
if err := api.streamEvent(ws, event); err != nil {
return
@ -65,7 +64,7 @@ func (api *RestAPI) streamWriter(ws *websocket.Conn, w http.ResponseWriter, r *h
session.I.Events.Clear()
log.Debug("Listening for events and streaming to ws endpoint ...")
api.Debug("Listening for events and streaming to ws endpoint ...")
pingTicker := time.NewTicker(pingPeriod)
listener := session.I.Events.Listen()
@ -82,7 +81,7 @@ func (api *RestAPI) streamWriter(ws *websocket.Conn, w http.ResponseWriter, r *h
return
}
case <-api.quit:
log.Info("Stopping websocket events streamer ...")
api.Info("Stopping websocket events streamer ...")
return
}
}
@ -96,7 +95,7 @@ func (api *RestAPI) streamReader(ws *websocket.Conn) {
for {
_, _, err := ws.ReadMessage()
if err != nil {
log.Debug("Closing websocket reader.")
api.Debug("Closing websocket reader.")
break
}
}
@ -106,12 +105,12 @@ func (api *RestAPI) startStreamingEvents(w http.ResponseWriter, r *http.Request)
ws, err := api.upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Error("Error while updating api.rest connection to websocket: %s", err)
api.Error("Error while updating api.rest connection to websocket: %s", err)
}
return
}
log.Debug("Websocket streaming started for %s", r.RemoteAddr)
api.Debug("Websocket streaming started for %s", r.RemoteAddr)
go api.streamWriter(ws, w, r)
api.streamReader(ws)