-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
109 lines (91 loc) · 2.34 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"context"
"crypto/tls"
"flag"
"log"
"net/http"
"os/signal"
"syscall"
"github.com/wi1dcard/fingerproxy/pkg/debug"
"github.com/wi1dcard/fingerproxy/pkg/proxyserver"
)
var (
flagListenAddr, flagCertFilename, flagKeyFilename *string
flagBenchmarkControlGroup, flagVerbose, flagQuiet *bool
tlsConf *tls.Config
ctx, _ = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
)
func main() {
parseFlags()
setupTLSConfig()
if *flagBenchmarkControlGroup {
runAsControlGroup()
} else {
run()
}
}
func parseFlags() {
flagListenAddr = flag.String(
"listen-addr",
"localhost:8443",
"Listening address",
)
flagCertFilename = flag.String(
"cert-filename",
"tls.crt",
"TLS certificate filename",
)
flagKeyFilename = flag.String(
"certkey-filename",
"tls.key",
"TLS certificate key file name",
)
flagBenchmarkControlGroup = flag.Bool(
"benchmark-control-group",
false,
"Start a golang default TLS server as the control group for benchmarking",
)
flagVerbose = flag.Bool("verbose", false, "Print fingerprint detail in logs, conflict with -quiet")
flagQuiet = flag.Bool("quiet", false, "Do not print fingerprints in logs, conflict with -verbose")
flag.Parse()
if *flagVerbose && *flagQuiet {
log.Fatal("-verbose and -quiet cannot be specified at the same time")
}
}
func setupTLSConfig() {
tlsConf = &tls.Config{
NextProtos: []string{"h2", "http/1.1"},
}
if tlsCert, err := tls.LoadX509KeyPair(*flagCertFilename, *flagKeyFilename); err != nil {
log.Fatal(err)
} else {
tlsConf.Certificates = []tls.Certificate{tlsCert}
}
}
func runAsControlGroup() {
// create golang default https server
server := &http.Server{
Addr: *flagListenAddr,
Handler: http.HandlerFunc(echoServer),
TLSConfig: tlsConf,
}
go func() {
<-ctx.Done()
server.Shutdown(context.Background())
}()
// listen and serve
log.Printf("server (benchmark control group) listening on %s", *flagListenAddr)
err := server.ListenAndServeTLS("", "")
log.Fatal(err)
}
func run() {
// create proxyserver
server := proxyserver.NewServer(ctx, http.HandlerFunc(echoServer), tlsConf)
// start debug server if build tag `debug` is specified
debug.StartDebugServer()
// listen and serve
log.Printf("server listening on %s", *flagListenAddr)
err := server.ListenAndServe(*flagListenAddr)
log.Fatal(err)
}