| 1 | package http_server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/bettercap/bettercap/session" |
| 11 | |
| 12 | "github.com/evilsocket/islazy/tui" |
| 13 | ) |
| 14 | |
| 15 | type HttpServer struct { |
| 16 | session.SessionModule |
| 17 | server *http.Server |
| 18 | } |
| 19 | |
| 20 | func NewHttpServer(s *session.Session) *HttpServer { |
| 21 | mod := &HttpServer{ |
| 22 | SessionModule: session.NewSessionModule("http.server", s), |
| 23 | server: &http.Server{}, |
| 24 | } |
| 25 | |
| 26 | mod.AddParam(session.NewStringParameter("http.server.path", |
| 27 | ".", |
| 28 | "", |
| 29 | "Server folder.")) |
| 30 | |
| 31 | mod.AddParam(session.NewStringParameter("http.server.address", |
| 32 | session.ParamIfaceAddress, |
| 33 | session.IPv4Validator, |
| 34 | "Address to bind the http server to.")) |
| 35 | |
| 36 | mod.AddParam(session.NewIntParameter("http.server.port", |
| 37 | "80", |
| 38 | "Port to bind the http server to.")) |
| 39 | |
| 40 | mod.AddHandler(session.NewModuleHandler("http.server on", "", |
| 41 | "Start httpd server.", |
| 42 | func(args []string) error { |
| 43 | return mod.Start() |
| 44 | })) |
| 45 | |
| 46 | mod.AddHandler(session.NewModuleHandler("http.server off", "", |
| 47 | "Stop httpd server.", |
| 48 | func(args []string) error { |
| 49 | return mod.Stop() |
| 50 | })) |
| 51 | |
| 52 | return mod |
| 53 | } |
| 54 | |
| 55 | func (mod *HttpServer) Name() string { |
| 56 | return "http.server" |
| 57 | } |
| 58 | |
| 59 | func (mod *HttpServer) Description() string { |
| 60 | return "A simple HTTP server, to be used to serve files and scripts across the network." |
| 61 | } |
| 62 | |
| 63 | func (mod *HttpServer) Author() string { |
| 64 | return "Simone Margaritelli <[email protected]>" |
| 65 | } |
| 66 | |
| 67 | func (mod *HttpServer) Configure() error { |
| 68 | var err error |
| 69 | var path string |
| 70 | var address string |
| 71 | var port int |
| 72 | |
| 73 | if mod.Running() { |
| 74 | return session.ErrAlreadyStarted(mod.Name()) |
| 75 | } |
| 76 | |
| 77 | if err, path = mod.StringParam("http.server.path"); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | router := http.NewServeMux() |
| 82 | fileServer := http.FileServer(http.Dir(path)) |
| 83 | |
| 84 | router.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 85 | mod.Debug("%s %s %s%s", tui.Bold(strings.Split(r.RemoteAddr, ":")[0]), r.Method, r.Host, r.URL.Path) |
| 86 | if r.URL.Path == "/proxy.pac" || r.URL.Path == "/wpad.dat" { |
| 87 | w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig") |
| 88 | } |
| 89 | fileServer.ServeHTTP(w, r) |
| 90 | })) |
| 91 | |
| 92 | mod.server.Handler = router |
| 93 | |
| 94 | if err, address = mod.StringParam("http.server.address"); err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | if err, port = mod.IntParam("http.server.port"); err != nil { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | mod.server.Addr = fmt.Sprintf("%s:%d", address, port) |
| 103 | |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | func (mod *HttpServer) Start() error { |
| 108 | if err := mod.Configure(); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | return mod.SetRunning(true, func() { |
| 113 | var err error |
| 114 | mod.Info("starting on http://%s", mod.server.Addr) |
| 115 | if err = mod.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 116 | mod.Error("%v", err) |
| 117 | mod.Stop() |
| 118 | } |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | func (mod *HttpServer) Stop() error { |
| 123 | return mod.SetRunning(false, func() { |
| 124 | ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 125 | defer cancel() |
| 126 | mod.server.Shutdown(ctx) |
| 127 | }) |
| 128 | } |