-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddlewares.go
More file actions
108 lines (89 loc) · 3.54 KB
/
middlewares.go
File metadata and controls
108 lines (89 loc) · 3.54 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
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
package ja4plus
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"sync"
)
// JA4Middleware is a helper to plug the JA4 fingerprinting into your HTTP server.
// It only exists because there is no direct way to pass information from the TLS handshake to the HTTP handler.
type JA4Middleware struct {
connectionFingerprints sync.Map
tlsConfig *tls.Config
}
type ja4FingerprintCtxKey struct{}
// NewJ4AMiddleware returns a new initialized middleware wrapper
func NewJ4AMiddleware() *JA4Middleware {
return &JA4Middleware{
connectionFingerprints: sync.Map{},
}
}
// NewHandlerWrapper takes a middleware, a tls config, and a http.handler and returns a modified handler that injects
// fingerprints into the context of the a connection so they can be consumed later.
func (m *JA4Middleware) NewHandlerWrapper(middleware *JA4Middleware, tlsConfig *tls.Config, next http.Handler) http.Handler {
tlsConfig.GetConfigForClient = func(chi *tls.ClientHelloInfo) (*tls.Config, error) {
// Protects against panics when generating the JA4
if chi != nil {
m.connectionFingerprints.Store(chi.Conn.RemoteAddr().String(), JA4(chi))
return nil, nil
}
return nil, fmt.Errorf("Failed to extract client tls hello")
}
middleware.tlsConfig = tlsConfig
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if cacheEntry, _ := m.connectionFingerprints.Load(r.RemoteAddr); cacheEntry != nil {
ctx = context.WithValue(ctx, ja4FingerprintCtxKey{}, cacheEntry.(string))
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// NewListenerWrapper takes a middleware, a tls config and a address and returns a fully wrapped net.Listener.
// You will still need to manually clear fingerprints from memory as connections close with ListenerCallback.
func NewListenerWrapper(middleware *JA4Middleware, tlsConfig *tls.Config, addr string) (net.Listener, error) {
tlsConfig.GetConfigForClient = func(chi *tls.ClientHelloInfo) (*tls.Config, error) {
// Protects against panics when generating the JA4
if chi != nil {
middleware.connectionFingerprints.Store(chi.Conn.RemoteAddr().String(), JA4(chi))
return nil, nil
}
return nil, fmt.Errorf("Failed to extract client tls hello")
}
middleware.tlsConfig = tlsConfig
listen, err := tls.Listen("tcp", addr, tlsConfig)
if err != nil {
return nil, err
}
return listen, nil
}
// HTTPCallback is a callback that should be set as the [http.Server]'s ConnState to clean up the fingerprint cache.
func (m *JA4Middleware) HTTPCallback(conn net.Conn, state http.ConnState) {
switch state {
case http.StateClosed, http.StateHijacked:
m.connectionFingerprints.Delete(conn.RemoteAddr().String())
}
}
// ListenerCallback is a manually called deletion method for clearing fingerprint state after a connection is closed
func (m *JA4Middleware) ListenerCallback(conn net.Conn) {
m.connectionFingerprints.Delete(conn.RemoteAddr().String())
}
// Returns the modified TLS config
func (m *JA4Middleware) ReturnTLSConfig() *tls.Config {
return m.tlsConfig
}
// JA4FromContext extracts the JA4 fingerprint from the provided [http.Request.Context].
func JA4FromContext(ctx context.Context) (string, bool) {
fingerprint, ok := ctx.Value(ja4FingerprintCtxKey{}).(string)
return fingerprint, ok
}
// JA4FromContext extracts the JA4 fingerprint from the middleware using the a connection.
func (m *JA4Middleware) JA4FromConn(conn net.Conn) (string, bool) {
fingerprint, ok := m.connectionFingerprints.Load(conn.RemoteAddr().String())
if !ok {
return "", ok
}
f, ok := fingerprint.(string)
return f, ok
}