MCPcopy
hub / github.com/bettercap/bettercap / Configure

Method Configure

modules/api_rest/api_rest.go:185–268  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

183}
184
185func (mod *RestAPI) Configure() error {
186 var err error
187 var ip string
188 var port int
189
190 if mod.Running() {
191 return session.ErrAlreadyStarted(mod.Name())
192 } else if err, ip = mod.StringParam("api.rest.address"); err != nil {
193 return err
194 } else if err, port = mod.IntParam("api.rest.port"); err != nil {
195 return err
196 } else if err, mod.allowOrigin = mod.StringParam("api.rest.alloworigin"); err != nil {
197 return err
198 } else if err, mod.certFile = mod.StringParam("api.rest.certificate"); err != nil {
199 return err
200 } else if mod.certFile, err = fs.Expand(mod.certFile); err != nil {
201 return err
202 } else if err, mod.keyFile = mod.StringParam("api.rest.key"); err != nil {
203 return err
204 } else if mod.keyFile, err = fs.Expand(mod.keyFile); err != nil {
205 return err
206 } else if err, mod.username = mod.StringParam("api.rest.username"); err != nil {
207 return err
208 } else if err, mod.password = mod.StringParam("api.rest.password"); err != nil {
209 return err
210 } else if err, mod.useWebsocket = mod.BoolParam("api.rest.websocket"); err != nil {
211 return err
212 }
213
214 if mod.isTLS() {
215 if !fs.Exists(mod.certFile) || !fs.Exists(mod.keyFile) {
216 cfg, err := tls.CertConfigFromModule("api.rest", mod.SessionModule)
217 if err != nil {
218 return err
219 }
220
221 mod.Debug("%+v", cfg)
222 mod.Info("generating TLS key to %s", mod.keyFile)
223 mod.Info("generating TLS certificate to %s", mod.certFile)
224 if err := tls.Generate(cfg, mod.certFile, mod.keyFile, false); err != nil {
225 return err
226 }
227 } else {
228 mod.Info("loading TLS key from %s", mod.keyFile)
229 mod.Info("loading TLS certificate from %s", mod.certFile)
230 }
231 }
232
233 mod.server = &http.Server{}
234 mod.server.Addr = fmt.Sprintf("%s:%d", ip, port)
235
236 router := mux.NewRouter()
237
238 router.Methods("OPTIONS").HandlerFunc(mod.corsRoute)
239
240 router.HandleFunc("/api/file", mod.fileRoute)
241
242 router.HandleFunc("/api/events", mod.eventsRoute)

Callers 7

StartMethod · 0.95
TestConfigureErrorsFunction · 0.95
TestServerConfigurationFunction · 0.95
TestServerAddrFunction · 0.95
TestTLSConfigurationFunction · 0.95
BenchmarkConfigureFunction · 0.95

Calls 12

NameMethod · 0.95
isTLSMethod · 0.95
ErrAlreadyStartedFunction · 0.92
CertConfigFromModuleFunction · 0.92
GenerateFunction · 0.92
StringParamMethod · 0.80
IntParamMethod · 0.80
BoolParamMethod · 0.80
RunningMethod · 0.65
DebugMethod · 0.45
InfoMethod · 0.45
WarningMethod · 0.45

Tested by 6

TestConfigureErrorsFunction · 0.76
TestServerConfigurationFunction · 0.76
TestServerAddrFunction · 0.76
TestTLSConfigurationFunction · 0.76
BenchmarkConfigureFunction · 0.76