mirror of
https://github.com/bettercap/bettercap
synced 2025-07-16 10:03:39 -07:00
some progress but still doesn't work
This commit is contained in:
parent
894cbe76a1
commit
b243e67828
4 changed files with 168 additions and 32 deletions
|
@ -10,23 +10,33 @@ import (
|
||||||
"github.com/bettercap/bettercap/log"
|
"github.com/bettercap/bettercap/log"
|
||||||
"github.com/bettercap/bettercap/session"
|
"github.com/bettercap/bettercap/session"
|
||||||
"github.com/bettercap/bettercap/tls"
|
"github.com/bettercap/bettercap/tls"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RestAPI struct {
|
type RestAPI struct {
|
||||||
session.SessionModule
|
session.SessionModule
|
||||||
server *http.Server
|
server *http.Server
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
certFile string
|
certFile string
|
||||||
keyFile string
|
keyFile string
|
||||||
useWebsocket bool
|
useWebsocket bool
|
||||||
|
upgrader websocket.Upgrader
|
||||||
|
eventListener <-chan session.Event
|
||||||
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRestAPI(s *session.Session) *RestAPI {
|
func NewRestAPI(s *session.Session) *RestAPI {
|
||||||
api := &RestAPI{
|
api := &RestAPI{
|
||||||
SessionModule: session.NewSessionModule("api.rest", s),
|
SessionModule: session.NewSessionModule("api.rest", s),
|
||||||
server: &http.Server{},
|
server: &http.Server{},
|
||||||
|
quit: make(chan bool),
|
||||||
useWebsocket: false,
|
useWebsocket: false,
|
||||||
|
upgrader: websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
api.AddParam(session.NewStringParameter("api.rest.address",
|
api.AddParam(session.NewStringParameter("api.rest.address",
|
||||||
|
@ -163,6 +173,10 @@ func (api *RestAPI) Start() error {
|
||||||
|
|
||||||
func (api *RestAPI) Stop() error {
|
func (api *RestAPI) Stop() error {
|
||||||
return api.SetRunning(false, func() {
|
return api.SetRunning(false, func() {
|
||||||
|
go func() {
|
||||||
|
api.quit <- true
|
||||||
|
}()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
api.server.Shutdown(ctx)
|
api.server.Shutdown(ctx)
|
||||||
|
|
|
@ -5,8 +5,21 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bettercap/bettercap/log"
|
||||||
"github.com/bettercap/bettercap/session"
|
"github.com/bettercap/bettercap/session"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Time allowed to write an event to the client.
|
||||||
|
writeWait = 10 * time.Second
|
||||||
|
// Time allowed to read the next pong message from the client.
|
||||||
|
pongWait = 60 * time.Second
|
||||||
|
// Send pings to client with this period. Must be less than pongWait.
|
||||||
|
pingPeriod = (pongWait * 9) / 10
|
||||||
)
|
)
|
||||||
|
|
||||||
type CommandRequest struct {
|
type CommandRequest struct {
|
||||||
|
@ -18,7 +31,9 @@ type APIResponse struct {
|
||||||
Message string `json:"msg"`
|
Message string `json:"msg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func setAuthFailed(w http.ResponseWriter) {
|
func setAuthFailed(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Warning("Unauthorized authentication attempt from %s", r.RemoteAddr)
|
||||||
|
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="auth"`)
|
w.Header().Set("WWW-Authenticate", `Basic realm="auth"`)
|
||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
w.Write([]byte("Unauthorized"))
|
w.Write([]byte("Unauthorized"))
|
||||||
|
@ -66,27 +81,119 @@ func (api *RestAPI) runSessionCommand(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *RestAPI) showEvents(w http.ResponseWriter, r *http.Request) {
|
func (api *RestAPI) streamWriter(ws *websocket.Conn, w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
defer ws.Close()
|
||||||
|
|
||||||
|
// first we stream what we already have
|
||||||
events := session.I.Events.Sorted()
|
events := session.I.Events.Sorted()
|
||||||
nmax := len(events)
|
n := len(events)
|
||||||
n := nmax
|
if n > 0 {
|
||||||
|
log.Info("Sending %d events.", n)
|
||||||
q := r.URL.Query()
|
for _, event := range events {
|
||||||
vals := q["n"]
|
msg, err := json.Marshal(event)
|
||||||
if len(vals) > 0 {
|
if err != nil {
|
||||||
n, err = strconv.Atoi(q["n"][0])
|
log.Error("Error while creating websocket message: %s", err)
|
||||||
if err == nil {
|
return
|
||||||
if n > nmax {
|
}
|
||||||
n = nmax
|
|
||||||
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
if err := ws.WriteMessage(websocket.TextMessage, msg); err != nil {
|
||||||
|
log.Error("Error while writing websocket message: %s", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
n = nmax
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON(w, events[0:n])
|
session.I.Events.Clear()
|
||||||
|
|
||||||
|
log.Info("Listening for events and streaming to ws endpoint ...")
|
||||||
|
|
||||||
|
api.eventListener = api.Session.Events.Listen()
|
||||||
|
|
||||||
|
pingTicker := time.NewTicker(pingPeriod)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-pingTicker.C:
|
||||||
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
log.Info("Ping")
|
||||||
|
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
||||||
|
log.Error("Error while writing websocket ping message: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case event := <-api.eventListener:
|
||||||
|
log.Info("Event")
|
||||||
|
msg, err := json.Marshal(event)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Error while creating websocket message: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
if err := ws.WriteMessage(websocket.TextMessage, msg); err != nil {
|
||||||
|
log.Error("Error while writing websocket message: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Info("Sent")
|
||||||
|
|
||||||
|
case <-api.quit:
|
||||||
|
log.Info("Quit")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *RestAPI) streamReader(ws *websocket.Conn) {
|
||||||
|
defer ws.Close()
|
||||||
|
ws.SetReadLimit(512)
|
||||||
|
ws.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
|
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
||||||
|
for {
|
||||||
|
_, _, err := ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Closing reader.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *RestAPI) showEvents(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if api.useWebsocket {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Websocket streaming started for %s", r.RemoteAddr)
|
||||||
|
|
||||||
|
go api.streamWriter(ws, w, r)
|
||||||
|
api.streamReader(ws)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
events := session.I.Events.Sorted()
|
||||||
|
nmax := len(events)
|
||||||
|
n := nmax
|
||||||
|
|
||||||
|
q := r.URL.Query()
|
||||||
|
vals := q["n"]
|
||||||
|
if len(vals) > 0 {
|
||||||
|
n, err = strconv.Atoi(q["n"][0])
|
||||||
|
if err == nil {
|
||||||
|
if n > nmax {
|
||||||
|
n = nmax
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
n = nmax
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON(w, events[0:n])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *RestAPI) clearEvents(w http.ResponseWriter, r *http.Request) {
|
func (api *RestAPI) clearEvents(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -97,7 +204,7 @@ func (api *RestAPI) sessionRoute(w http.ResponseWriter, r *http.Request) {
|
||||||
setSecurityHeaders(w)
|
setSecurityHeaders(w)
|
||||||
|
|
||||||
if api.checkAuth(r) == false {
|
if api.checkAuth(r) == false {
|
||||||
setAuthFailed(w)
|
setAuthFailed(w, r)
|
||||||
} else if r.Method == "GET" {
|
} else if r.Method == "GET" {
|
||||||
api.showSession(w, r)
|
api.showSession(w, r)
|
||||||
} else if r.Method == "POST" {
|
} else if r.Method == "POST" {
|
||||||
|
@ -111,7 +218,7 @@ func (api *RestAPI) eventsRoute(w http.ResponseWriter, r *http.Request) {
|
||||||
setSecurityHeaders(w)
|
setSecurityHeaders(w)
|
||||||
|
|
||||||
if api.checkAuth(r) == false {
|
if api.checkAuth(r) == false {
|
||||||
setAuthFailed(w)
|
setAuthFailed(w, r)
|
||||||
} else if r.Method == "GET" {
|
} else if r.Method == "GET" {
|
||||||
api.showEvents(w, r)
|
api.showEvents(w, r)
|
||||||
} else if r.Method == "DELETE" {
|
} else if r.Method == "DELETE" {
|
||||||
|
|
|
@ -12,10 +12,11 @@ import (
|
||||||
|
|
||||||
type EventsStream struct {
|
type EventsStream struct {
|
||||||
session.SessionModule
|
session.SessionModule
|
||||||
ignoreList *IgnoreList
|
ignoreList *IgnoreList
|
||||||
waitFor string
|
waitFor string
|
||||||
waitChan chan *session.Event
|
waitChan chan *session.Event
|
||||||
quit chan bool
|
eventListener <-chan session.Event
|
||||||
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEventsStream(s *session.Session) *EventsStream {
|
func NewEventsStream(s *session.Session) *EventsStream {
|
||||||
|
@ -124,10 +125,12 @@ func (s *EventsStream) Configure() error {
|
||||||
|
|
||||||
func (s *EventsStream) Start() error {
|
func (s *EventsStream) Start() error {
|
||||||
return s.SetRunning(true, func() {
|
return s.SetRunning(true, func() {
|
||||||
|
|
||||||
|
s.eventListener = s.Session.Events.Listen()
|
||||||
for {
|
for {
|
||||||
var e session.Event
|
var e session.Event
|
||||||
select {
|
select {
|
||||||
case e = <-s.Session.Events.NewEvents:
|
case e = <-s.eventListener:
|
||||||
if e.Tag == s.waitFor {
|
if e.Tag == s.waitFor {
|
||||||
s.waitFor = ""
|
s.waitFor = ""
|
||||||
s.waitChan <- &e
|
s.waitChan <- &e
|
||||||
|
|
|
@ -39,21 +39,29 @@ func (e Event) Label() string {
|
||||||
type EventPool struct {
|
type EventPool struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
NewEvents chan Event
|
|
||||||
debug bool
|
debug bool
|
||||||
silent bool
|
silent bool
|
||||||
events []Event
|
events []Event
|
||||||
|
listeners []chan Event
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEventPool(debug bool, silent bool) *EventPool {
|
func NewEventPool(debug bool, silent bool) *EventPool {
|
||||||
return &EventPool{
|
return &EventPool{
|
||||||
NewEvents: make(chan Event, 0xff),
|
|
||||||
debug: debug,
|
debug: debug,
|
||||||
silent: silent,
|
silent: silent,
|
||||||
events: make([]Event, 0),
|
events: make([]Event, 0),
|
||||||
|
listeners: make([]chan Event, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *EventPool) Listen() <-chan Event {
|
||||||
|
p.Lock()
|
||||||
|
defer p.Unlock()
|
||||||
|
l := make(chan Event)
|
||||||
|
p.listeners = append(p.listeners, l)
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
func (p *EventPool) SetSilent(s bool) {
|
func (p *EventPool) SetSilent(s bool) {
|
||||||
p.Lock()
|
p.Lock()
|
||||||
defer p.Unlock()
|
defer p.Unlock()
|
||||||
|
@ -71,7 +79,11 @@ func (p *EventPool) Add(tag string, data interface{}) {
|
||||||
defer p.Unlock()
|
defer p.Unlock()
|
||||||
e := NewEvent(tag, data)
|
e := NewEvent(tag, data)
|
||||||
p.events = append([]Event{e}, p.events...)
|
p.events = append([]Event{e}, p.events...)
|
||||||
p.NewEvents <- e
|
|
||||||
|
// broadcast the event to every listener
|
||||||
|
for _, l := range p.listeners {
|
||||||
|
l <- e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *EventPool) Log(level int, format string, args ...interface{}) {
|
func (p *EventPool) Log(level int, format string, args ...interface{}) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue