-
Notifications
You must be signed in to change notification settings - Fork 18
/
flags.go
196 lines (162 loc) · 5.27 KB
/
flags.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package fingerproxy
import (
"flag"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
)
var (
// basic
flagListenAddr, flagForwardURL *string
// tls
flagCertFilename, flagKeyFilename *string
// metrics
flagMetricsListenAddr, flagDurationMetricBuckets *string
// functionality
flagPreserveHost *bool
flagEnableKubernetesProbe *bool
flagReverseProxyFlushInterval *string
// timeouts
flagTimeoutHTTPIdle, flagTimeoutHTTPRead, flagTimeoutHTTPWrite, flagTimeoutTLSHandshake *string
// misc
flagVerboseLogs *bool
flagVersion *bool
)
func initFlags() {
flagListenAddr = flag.String(
"listen-addr",
envWithDefault("LISTEN_ADDR", ":443"),
"Listening address, equivalent to $LISTEN_ADDR",
)
flagForwardURL = flag.String(
"forward-url",
envWithDefault("FORWARD_URL", "http://localhost:80"),
"Backend URL that the requests will be forwarded to, equivalent to $FORWARD_URL",
)
flagCertFilename = flag.String(
"cert-filename",
envWithDefault("CERT_FILENAME", "tls.crt"),
"TLS certificate filename, equivalent to $CERT_FILENAME",
)
flagKeyFilename = flag.String(
"certkey-filename",
envWithDefault("CERTKEY_FILENAME", "tls.key"),
"TLS certificate key file name, equivalent to $CERTKEY_FILENAME",
)
flagMetricsListenAddr = flag.String(
"metrics-listen-addr",
envWithDefault("METRICS_LISTEN_ADDR", ":9035"),
"Listening address of Prometheus metrics, equivalent to $METRICS_LISTEN_ADDR",
)
flagDurationMetricBuckets = flag.String(
"duration-metric-buckets",
envWithDefault("DURATION_METRIC_BUCKETS", ".00001, .00002, .00005, .0001, .0002, .0005, .001, .005, .01"),
"The histogram buckets of duration metric, equivalent to $DURATION_METRIC_BUCKETS",
)
flagPreserveHost = flag.Bool(
"preserve-host",
envWithDefaultBool("PRESERVE_HOST", false),
"Forward HTTP Host header from incoming requests to the backend, equivalent to $PRESERVE_HOST",
)
flagEnableKubernetesProbe = flag.Bool(
"enable-kubernetes-probe",
envWithDefaultBool("ENABLE_KUBERNETES_PROBE", true),
"Enable kubernetes liveness/readiness probe support, equivalent to $ENABLE_KUBERNETES_PROBE",
)
flagReverseProxyFlushInterval = flag.String(
"reverse-proxy-flush-interval",
envWithDefault("REVERSE_PROXY_FLUSH_INTERVAL", "100ms"),
"See https://pkg.go.dev/net/http/httputil#ReverseProxy.FlushInterval, equivalent to $REVERSE_PROXY_FLUSH_INTERVAL",
)
flagTimeoutHTTPIdle = flag.String(
"timeout-http-idle",
envWithDefault("TIMEOUT_HTTP_IDLE", "180s"),
"See https://pkg.go.dev/net/http#Server.IdleTimeout, equivalent to $TIMEOUT_HTTP_IDLE",
)
flagTimeoutHTTPRead = flag.String(
"timeout-http-read",
envWithDefault("TIMEOUT_HTTP_READ", "60s"),
"See https://pkg.go.dev/net/http#Server.ReadTimeout, equivalent to $TIMEOUT_HTTP_READ",
)
flagTimeoutHTTPWrite = flag.String(
"timeout-http-write",
envWithDefault("TIMEOUT_HTTP_WRITE", "60s"),
"See https://pkg.go.dev/net/http#Server.WriteTimeout, equivalent to $TIMEOUT_HTTP_WRITE",
)
flagTimeoutTLSHandshake = flag.String(
"timeout-tls-handshake",
envWithDefault("TIMEOUT_TLS_HANDSHAKE", "10s"),
"Timeout for TLS handshakes, equivalent to $TIMEOUT_TLS_HANDSHAKE",
)
flagVerboseLogs = flag.Bool(
"verbose",
envWithDefaultBool("VERBOSE", false),
"Enable verbose logs, equivalent to $VERBOSE",
)
flagVersion = flag.Bool("version", false, "Print version and exit")
}
func parseFlags() {
flag.Parse()
if *flagVersion {
fmt.Fprintln(os.Stderr, "Fingerproxy - https://github.com/wi1dcard/fingerproxy")
fmt.Fprintf(os.Stderr, "Version: %s (%s)\n", BuildTag, BuildCommit)
os.Exit(0)
}
}
func parseForwardURL() *url.URL {
forwardURL, err := url.Parse(*flagForwardURL)
if err != nil {
DefaultLog.Fatalf(`invalid forward url "%s": %s`, *flagForwardURL, err)
}
return forwardURL
}
func parseDurationMetricBuckets() []float64 {
bucketStrings := strings.Split(*flagDurationMetricBuckets, ",")
buckets := []float64{}
for _, bucket := range bucketStrings {
parsedBucket, err := strconv.ParseFloat(strings.Trim(bucket, " "), 64)
if err != nil {
DefaultLog.Fatalf(`invalid duration metric bucket "%s": %s`, bucket, err)
}
buckets = append(buckets, parsedBucket)
}
return buckets
}
func parseReverseProxyFlushInterval() time.Duration {
dur, err := time.ParseDuration(*flagReverseProxyFlushInterval)
if err != nil {
DefaultLog.Fatalf(`invalid tls handshake timeout "%s": %s`, *flagReverseProxyFlushInterval, dur)
}
return dur
}
func parseHTTPIdleTimeout() time.Duration {
dur, err := time.ParseDuration(*flagTimeoutHTTPIdle)
if err != nil {
DefaultLog.Fatalf(`invalid http idle timeout "%s": %s`, *flagTimeoutHTTPIdle, err)
}
return dur
}
func parseHTTPReadTimeout() time.Duration {
dur, err := time.ParseDuration(*flagTimeoutHTTPRead)
if err != nil {
DefaultLog.Fatalf(`invalid http read timeout "%s": %s`, *flagTimeoutHTTPRead, err)
}
return dur
}
func parseHTTPWriteTimeout() time.Duration {
dur, err := time.ParseDuration(*flagTimeoutHTTPWrite)
if err != nil {
DefaultLog.Fatalf(`invalid http write timeout "%s": %s`, *flagTimeoutHTTPWrite, err)
}
return dur
}
func parseTLSHandshakeTimeout() time.Duration {
dur, err := time.ParseDuration(*flagTimeoutTLSHandshake)
if err != nil {
DefaultLog.Fatalf(`invalid tls handshake timeout "%s": %s`, *flagTimeoutTLSHandshake, err)
}
return dur
}