-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
79 lines (67 loc) · 1.86 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
cflog "github.com/cloudflare/cfssl/log"
)
func init() {
cflog.Level = cflog.LevelWarning
}
func main() {
flag.StringVar(&Config.Cert, "cert", "cert.pem", "proxy CA cert")
flag.StringVar(&Config.Key, "key", "key.pem", "proxy CA key")
flag.StringVar(&Config.Addr, "addr", "", "proxy listen host")
flag.StringVar(&Config.Port, "port", "8080", "proxy listen port")
flag.StringVar(&Config.TLSClient, "client", "Golang", "utls client")
flag.StringVar(&Config.TLSVersion, "version", "0", "utls client version")
flag.StringVar(&Config.Upstream, "upstream", "", "upstream proxy, e.g. 127.0.0.1:1080, socks5 only")
flag.BoolVar(&Config.Debug, "debug", false, "enable debug")
flag.Parse()
if Config.Debug {
cflog.Level = cflog.LevelDebug
}
if !fileExists(Config.Cert) || !fileExists(Config.Key) {
if fileExists(Config.Cert) {
log.Println("found CA cert, but no corresponding key")
os.Exit(-1)
} else if fileExists(Config.Key) {
log.Println("found CA key, but no corresponding cert")
os.Exit(-1)
}
log.Println("CA cert and key do not exist, generating")
err := generateCA()
if err != nil {
log.Fatal("Failed generating CA", err)
}
}
loadExistingCA()
generateSessionKey()
var err error
CustomDialer, err = NewUpstreamDialer(Config.Upstream, time.Second*10)
if err != nil {
log.Fatal(err)
}
server := &http.Server{
Addr: Config.Addr + ":" + Config.Port,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect {
handleTunneling(w, r)
} else {
handleHTTP(w, r)
}
}),
}
fmt.Printf(
"HTTP Proxy Server listen at %s:%s, with tls fingerprint %s %s\n",
Config.Addr, Config.Port, Config.TLSVersion, Config.TLSClient,
)
err = server.ListenAndServe()
if err != nil {
log.Fatal(err)
os.Exit(-1)
}
}