/
http_server.go
64 lines (50 loc) · 1.38 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
package core
import (
"github.com/gorilla/mux"
"net/http"
"time"
"github.com/kgretzky/evilginx2/log"
)
type HttpServer struct {
srv *http.Server
acmeTokens map[string]string
}
func NewHttpServer() (*HttpServer, error) {
s := &HttpServer{}
s.acmeTokens = make(map[string]string)
r := mux.NewRouter()
s.srv = &http.Server{
Handler: r,
Addr: ":80",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
r.HandleFunc("/.well-known/acme-challenge/{token}", s.handleACMEChallenge).Methods("GET")
r.PathPrefix("/").HandlerFunc(s.handleRedirect)
return s, nil
}
func (s *HttpServer) Start() {
go s.srv.ListenAndServe()
}
func (s *HttpServer) AddACMEToken(token string, keyAuth string) {
s.acmeTokens[token] = keyAuth
}
func (s *HttpServer) ClearACMETokens() {
s.acmeTokens = make(map[string]string)
}
func (s *HttpServer) handleACMEChallenge(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
token := vars["token"]
key, ok := s.acmeTokens[token]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
log.Debug("http: found ACME verification token for URL: %s", r.URL.Path)
w.WriteHeader(http.StatusOK)
w.Header().Set("content-type", "text/plain")
w.Write([]byte(key))
}
func (s *HttpServer) handleRedirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.URL.String(), http.StatusFound)
}