forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
http_server.go
187 lines (151 loc) · 4.62 KB
/
http_server.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
package modules
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/session"
"github.com/bettercap/bettercap/tls"
"github.com/evilsocket/islazy/fs"
"github.com/evilsocket/islazy/tui"
)
type HttpServer struct {
session.SessionModule
server *http.Server
certFile string
keyFile string
}
func NewHttpServer(s *session.Session) *HttpServer {
httpd := &HttpServer{
SessionModule: session.NewSessionModule("http.server", s),
server: &http.Server{},
}
httpd.AddParam(session.NewStringParameter("http.server.path",
".",
"",
"Server folder."))
httpd.AddParam(session.NewStringParameter("http.server.address",
session.ParamIfaceAddress,
session.IPv4Validator,
"Address to bind the http server to."))
httpd.AddParam(session.NewIntParameter("http.server.port",
"80",
"Port to bind the http server to."))
httpd.AddParam(session.NewStringParameter("http.server.certificate",
"",
"",
"TLS certificate file, if not empty will configure this as a HTTPS server (will be auto generated if filled but not existing)."))
httpd.AddParam(session.NewStringParameter("http.server.key",
"",
"",
"TLS key file, if not empty will configure this as a HTTPS server (will be auto generated if filled but not existing)."))
tls.CertConfigToModule("http.server", &httpd.SessionModule, tls.DefaultLegitConfig)
httpd.AddHandler(session.NewModuleHandler("http.server on", "",
"Start httpd server.",
func(args []string) error {
return httpd.Start()
}))
httpd.AddHandler(session.NewModuleHandler("http.server off", "",
"Stop httpd server.",
func(args []string) error {
return httpd.Stop()
}))
return httpd
}
func (httpd *HttpServer) Name() string {
return "http.server"
}
func (httpd *HttpServer) Description() string {
return "A simple HTTP server, to be used to serve files and scripts across the network."
}
func (httpd *HttpServer) Author() string {
return "Simone Margaritelli <evilsocket@protonmail.com>"
}
func (httpd *HttpServer) isTLS() bool {
return httpd.certFile != "" && httpd.keyFile != ""
}
func (httpd *HttpServer) Configure() error {
var err error
var path string
var address string
var port int
var certFile string
var keyFile string
if httpd.Running() {
return session.ErrAlreadyStarted
}
if err, path = httpd.StringParam("http.server.path"); err != nil {
return err
}
router := http.NewServeMux()
fileServer := http.FileServer(http.Dir(path))
router.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Info("(%s) %s %s %s%s", tui.Green("httpd"), tui.Bold(strings.Split(r.RemoteAddr, ":")[0]), r.Method, r.Host, r.URL.Path)
fileServer.ServeHTTP(w, r)
}))
httpd.server.Handler = router
if err, address = httpd.StringParam("http.server.address"); err != nil {
return err
}
if err, port = httpd.IntParam("http.server.port"); err != nil {
return err
}
httpd.server.Addr = fmt.Sprintf("%s:%d", address, port)
if err, certFile = httpd.StringParam("http.server.certificate"); err != nil {
return err
} else if certFile, err = fs.Expand(certFile); err != nil {
return err
}
if err, keyFile = httpd.StringParam("http.server.key"); err != nil {
return err
} else if keyFile, err = fs.Expand(keyFile); err != nil {
return err
}
if certFile != "" && keyFile != "" {
if !fs.Exists(certFile) || !fs.Exists(keyFile) {
err, cfg := tls.CertConfigFromModule("http.server", httpd.SessionModule)
if err != nil {
return err
}
log.Debug("%+v", cfg)
log.Info("Generating server TLS key to %s", keyFile)
log.Info("Generating server TLS certificate to %s", certFile)
if err := tls.Generate(cfg, certFile, keyFile); err != nil {
return err
}
} else {
log.Info("loading server TLS key from %s", keyFile)
log.Info("loading server TLS certificate from %s", certFile)
}
}
httpd.certFile = certFile
httpd.keyFile = keyFile
return nil
}
func (httpd *HttpServer) Start() error {
if err := httpd.Configure(); err != nil {
return err
}
return httpd.SetRunning(true, func() {
var err error
if httpd.isTLS() {
log.Info("HTTPS server starting on https://%s", httpd.server.Addr)
err = httpd.server.ListenAndServeTLS(httpd.certFile, httpd.keyFile)
} else {
log.Info("HTTP server starting on http://%s", httpd.server.Addr)
err = httpd.server.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
panic(err)
}
})
}
func (httpd *HttpServer) Stop() error {
return httpd.SetRunning(false, func() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
httpd.server.Shutdown(ctx)
})
}