-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (46 loc) · 1.32 KB
/
main.go
File metadata and controls
52 lines (46 loc) · 1.32 KB
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
package main
import (
"crypto/tls"
"flag"
"log"
"net/http"
"strings"
)
func main() {
var pemPath, keyPath, proto, listen, users string
flag.StringVar(&pemPath, "pem", "server.pem", "path to pem file")
flag.StringVar(&keyPath, "key", "server.key", "path to key file")
flag.StringVar(&proto, "proto", "http", "Proxy protocol (http or https)")
flag.StringVar(&listen, "listen", ":8080", "listen address, default :8080")
flag.StringVar(&users, "users", "", "user:password list")
flag.Parse()
if proto != "http" && proto != "https" {
log.Fatal("Protocol must be either http or https")
}
var userList []User
for _, up := range strings.Split(users, ";") {
if ms := strings.Split(up, ":"); len(ms) == 2 && len(ms[0]) > 0 {
userList = append(userList, User{ms[0], ms[1]})
}
}
server := &http.Server{
Addr: listen,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(userList) > 0 && !basicAuth(w, r, userList) {
return
}
if r.Method == http.MethodConnect {
handleTunneling(w, r)
} else {
handleHTTP(w, r)
}
}),
// TLSNextProto not-nil to disable HTTP/2.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
if proto == "http" {
log.Fatal(server.ListenAndServe())
} else {
log.Fatal(server.ListenAndServeTLS(pemPath, keyPath))
}
}