forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
api_rest.go
235 lines (194 loc) · 6.09 KB
/
api_rest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package modules
import (
"context"
"fmt"
"net/http"
"time"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/session"
"github.com/bettercap/bettercap/tls"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/evilsocket/islazy/fs"
)
type RestAPI struct {
session.SessionModule
server *http.Server
username string
password string
certFile string
keyFile string
allowOrigin string
useWebsocket bool
upgrader websocket.Upgrader
quit chan bool
}
func NewRestAPI(s *session.Session) *RestAPI {
api := &RestAPI{
SessionModule: session.NewSessionModule("api.rest", s),
server: &http.Server{},
quit: make(chan bool),
useWebsocket: false,
allowOrigin: "*",
upgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
}
api.AddParam(session.NewStringParameter("api.rest.address",
session.ParamIfaceAddress,
session.IPv4Validator,
"Address to bind the API REST server to."))
api.AddParam(session.NewIntParameter("api.rest.port",
"8081",
"Port to bind the API REST server to."))
api.AddParam(session.NewStringParameter("api.rest.alloworigin",
api.allowOrigin,
"",
"Value of the Access-Control-Allow-Origin header of the API server."))
api.AddParam(session.NewStringParameter("api.rest.username",
"",
"",
"API authentication username."))
api.AddParam(session.NewStringParameter("api.rest.password",
"",
"",
"API authentication password."))
api.AddParam(session.NewStringParameter("api.rest.certificate",
"",
"",
"API TLS certificate."))
tls.CertConfigToModule("api.rest", &api.SessionModule, tls.DefaultLegitConfig)
api.AddParam(session.NewStringParameter("api.rest.key",
"",
"",
"API TLS key"))
api.AddParam(session.NewBoolParameter("api.rest.websocket",
"false",
"If true the /api/events route will be available as a websocket endpoint instead of HTTPS."))
api.AddHandler(session.NewModuleHandler("api.rest on", "",
"Start REST API server.",
func(args []string) error {
return api.Start()
}))
api.AddHandler(session.NewModuleHandler("api.rest off", "",
"Stop REST API server.",
func(args []string) error {
return api.Stop()
}))
return api
}
type JSSessionRequest struct {
Command string `json:"cmd"`
}
type JSSessionResponse struct {
Error string `json:"error"`
}
func (api *RestAPI) Name() string {
return "api.rest"
}
func (api *RestAPI) Description() string {
return "Expose a RESTful API."
}
func (api *RestAPI) Author() string {
return "Simone Margaritelli <evilsocket@protonmail.com>"
}
func (api *RestAPI) isTLS() bool {
return api.certFile != "" && api.keyFile != ""
}
func (api *RestAPI) Configure() error {
var err error
var ip string
var port int
if api.Running() {
return session.ErrAlreadyStarted
} else if err, ip = api.StringParam("api.rest.address"); err != nil {
return err
} else if err, port = api.IntParam("api.rest.port"); err != nil {
return err
} else if err, api.allowOrigin = api.StringParam("api.rest.alloworigin"); err != nil {
return err
} else if err, api.certFile = api.StringParam("api.rest.certificate"); err != nil {
return err
} else if api.certFile, err = fs.Expand(api.certFile); err != nil {
return err
} else if err, api.keyFile = api.StringParam("api.rest.key"); err != nil {
return err
} else if api.keyFile, err = fs.Expand(api.keyFile); err != nil {
return err
} else if err, api.username = api.StringParam("api.rest.username"); err != nil {
return err
} else if err, api.password = api.StringParam("api.rest.password"); err != nil {
return err
} else if err, api.useWebsocket = api.BoolParam("api.rest.websocket"); err != nil {
return err
}
if api.isTLS() {
if !fs.Exists(api.certFile) || !fs.Exists(api.keyFile) {
err, cfg := tls.CertConfigFromModule("api.rest", api.SessionModule)
if err != nil {
return err
}
log.Debug("%+v", cfg)
log.Info("generating TLS key to %s", api.keyFile)
log.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.server.Addr = fmt.Sprintf("%s:%d", ip, port)
router := mux.NewRouter()
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/modules", 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
if api.username == "" || api.password == "" {
log.Warning("api.rest.username and/or api.rest.password parameters are empty, authentication is disabled.")
}
return nil
}
func (api *RestAPI) Start() error {
if err := api.Configure(); err != nil {
return err
}
api.SetRunning(true, func() {
var err error
if api.isTLS() {
log.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)
err = api.server.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
panic(err)
}
})
return nil
}
func (api *RestAPI) Stop() error {
return api.SetRunning(false, func() {
go func() {
api.quit <- true
}()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
api.server.Shutdown(ctx)
})
}