-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
46 lines (36 loc) · 1.05 KB
/
server.go
File metadata and controls
46 lines (36 loc) · 1.05 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
package main
import (
"crypto/tls"
"encoding/base64"
"net/http"
"os"
"strings"
)
func NewServer(port string, ips chan string, sem chan bool) *http.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := strings.SplitN(r.Header.Get("Proxy-Authorization"), " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
http.Error(w, "authorization failed", http.StatusUnauthorized)
return
}
payload, _ := base64.StdEncoding.DecodeString(auth[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || pair[0] != os.Getenv("proxy_username") && pair[1] != os.Getenv("proxy_password") {
http.Error(w, "authorization failed", http.StatusUnauthorized)
return
}
r.Header.Del("Proxy-Authorization")
sem <- true
if r.Method == http.MethodConnect {
handleTunneling(w, r, ips, sem)
} else {
handleHTTP(w, r, ips, sem)
}
})
return &http.Server{
Addr: ":" + port,
Handler: handler,
// Disable HTTP/2.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
}