/
fingerproxy.go
157 lines (130 loc) · 4.68 KB
/
fingerproxy.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
package fingerproxy
import (
"context"
"crypto/tls"
"errors"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/wi1dcard/fingerproxy/pkg/debug"
"github.com/wi1dcard/fingerproxy/pkg/fingerprint"
"github.com/wi1dcard/fingerproxy/pkg/proxyserver"
"github.com/wi1dcard/fingerproxy/pkg/reverseproxy"
)
const logFlags = log.LstdFlags | log.Lshortfile | log.Lmsgprefix
var (
// values are from CI build
BuildCommit = "GIT_COMMIT_PLACEHOLDER"
BuildTag = "GIT_TAG_PLACEHOLDER"
)
var (
// The loggers used by fingerproxy components
ProxyServerLog = log.New(os.Stderr, "[proxyserver] ", logFlags)
HTTPServerLog = log.New(os.Stderr, "[http] ", logFlags)
PrometheusLog = log.New(os.Stderr, "[metrics] ", logFlags)
ReverseProxyLog = log.New(os.Stderr, "[reverseproxy] ", logFlags)
FingerprintLog = log.New(os.Stderr, "[fingerprint] ", logFlags)
DefaultLog = log.New(os.Stderr, "[fingerproxy] ", logFlags)
// The Prometheus metric registry used by fingerproxy
PrometheusRegistry = prometheus.NewRegistry()
// The header injectors that injects fingerprint headers to forwarding requests,
// defaults to [fingerproxy.DefaultHeaderInjectors]
GetHeaderInjectors = DefaultHeaderInjectors
)
// DefaultHeaderInjectors is the default header injector set that injects JA3, JA4,
// and Akamai HTTP2 fingerprints. Override [fingerproxy.GetHeaderInjectors] to replace
// this to your own injectors.
func DefaultHeaderInjectors() []reverseproxy.HeaderInjector {
return []reverseproxy.HeaderInjector{
fingerprint.NewFingerprintHeaderInjector("X-JA3-Fingerprint", fingerprint.JA3Fingerprint),
fingerprint.NewFingerprintHeaderInjector("X-JA4-Fingerprint", fingerprint.JA4Fingerprint),
fingerprint.NewFingerprintHeaderInjector("X-HTTP2-Fingerprint", fingerprint.HTTP2Fingerprint),
}
}
func proxyErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
ReverseProxyLog.Printf("proxy %s error (from %s): %v", req.URL.String(), req.RemoteAddr, err)
if errors.Is(err, context.DeadlineExceeded) ||
errors.Is(err, context.Canceled) {
rw.WriteHeader(http.StatusGatewayTimeout)
} else {
rw.WriteHeader(http.StatusBadGateway)
}
}
func defaultReverseProxyHTTPHandler(forwardTo *url.URL, headerInjectors []reverseproxy.HeaderInjector) http.Handler {
handler := reverseproxy.NewHTTPHandler(
forwardTo,
&httputil.ReverseProxy{
ErrorLog: ReverseProxyLog,
FlushInterval: parseReverseProxyFlushInterval(),
ErrorHandler: proxyErrorHandler,
// TODO: customize transport
Transport: http.DefaultTransport.(*http.Transport).Clone(),
},
headerInjectors,
)
handler.PreserveHost = *flagPreserveHost
if *flagEnableKubernetesProbe {
handler.IsProbeRequest = reverseproxy.IsKubernetesProbeRequest
}
return handler
}
func defaultProxyServer(handler http.Handler, tlsConfig *tls.Config) *proxyserver.Server {
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
svr := proxyserver.NewServer(ctx, handler, tlsConfig)
svr.VerboseLogs = *flagVerboseLogs
svr.ErrorLog = ProxyServerLog
svr.HTTPServer.ErrorLog = HTTPServerLog
svr.MetricsRegistry = PrometheusRegistry
svr.HTTPServer.IdleTimeout = parseHTTPIdleTimeout()
svr.HTTPServer.ReadTimeout = parseHTTPReadTimeout()
svr.HTTPServer.WriteTimeout = parseHTTPWriteTimeout()
svr.TLSHandshakeTimeout = parseTLSHandshakeTimeout()
return svr
}
func initFingerprint() {
fingerprint.Logger = FingerprintLog
fingerprint.VerboseLogs = *flagVerboseLogs
fingerprint.RegisterDurationMetric(PrometheusRegistry, parseDurationMetricBuckets(), "")
}
// Run fingerproxy. To customize the fingerprinting algorithms, use "header injectors".
// See [fingerproxy.GetHeaderInjectors] for more info.
func Run() {
// CLI
initFlags()
parseFlags()
// fingerprint package
initFingerprint()
// main TLS server
server := defaultProxyServer(
defaultReverseProxyHTTPHandler(
parseForwardURL(),
GetHeaderInjectors(),
),
&tls.Config{
NextProtos: []string{"h2", "http/1.1"},
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13,
Certificates: []tls.Certificate{parseTLSCerts()},
},
)
// metrics server
PrometheusLog.Printf("server listening on %s", *flagMetricsListenAddr)
go http.ListenAndServe(
*flagMetricsListenAddr,
promhttp.HandlerFor(PrometheusRegistry, promhttp.HandlerOpts{
ErrorLog: PrometheusLog,
}),
)
// debug server if binary build with `debug` tag
debug.StartDebugServer()
// start the main TLS server
DefaultLog.Printf("server listening on %s", *flagListenAddr)
err := server.ListenAndServe(*flagListenAddr)
DefaultLog.Print(err)
}