-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbindings.go
More file actions
208 lines (184 loc) · 5.47 KB
/
bindings.go
File metadata and controls
208 lines (184 loc) · 5.47 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
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
197
198
199
200
201
202
203
204
205
206
207
208
package masktunnel
import (
"encoding/json"
"fmt"
"net"
"net/http"
"time"
"github.com/rs/zerolog"
)
// Python bindings: gopy-friendly API surface.
// ServerOption describes server configuration for bindings.
// Keep fields as basic types to ensure gopy can map them.
type ServerOption struct {
Addr string `json:"addr"`
Port string `json:"port"`
UserAgent string `json:"user_agent"`
Payload string `json:"payload"`
UpstreamProxy string `json:"upstream_proxy"`
Username string `json:"username"`
Password string `json:"password"`
Verbose int `json:"verbose"`
LoggerID string `json:"logger_id"`
logger *zerolog.Logger // internal, set via WithLogger
}
// DefaultServerOption returns default options.
func DefaultServerOption() *ServerOption {
return &ServerOption{
Port: "8080",
Verbose: 0,
}
}
// WithLogger sets a custom logger for the server.
func (opt *ServerOption) WithLogger(logger zerolog.Logger) {
opt.logger = &logger
}
// ServerHandle wraps *Server for Python.
// Note: gopy doesn't like interfaces or embedded complex fields; keep it simple.
type ServerHandle struct {
s *Server
}
// NewServerHandle creates a new server handle.
func NewServerHandle(opt *ServerOption) *ServerHandle {
if opt == nil {
opt = DefaultServerOption()
}
// Map bindings verbosity to zerolog global level.
// 0=warn, 1=info, 2=debug, 3+=trace.
switch {
case opt.Verbose >= 3:
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case opt.Verbose == 2:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case opt.Verbose == 1:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
default:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}
var cfgLogger *zerolog.Logger
if opt.logger != nil {
cfgLogger = opt.logger
} else if opt.LoggerID != "" {
l := NewLoggerWithID(opt.LoggerID)
cfgLogger = &l
}
cfg := &Config{
Addr: opt.Addr,
Port: opt.Port,
UserAgent: opt.UserAgent,
Payload: opt.Payload,
UpstreamProxy: opt.UpstreamProxy,
Username: opt.Username,
Password: opt.Password,
Verbose: opt.Verbose,
Logger: cfgLogger,
}
return &ServerHandle{s: NewServer(cfg)}
}
// Start starts the proxy server (blocking).
func (h *ServerHandle) Start() error {
if h == nil || h.s == nil {
return fmt.Errorf("server not initialized")
}
return h.s.Start()
}
// StartBackground starts the proxy server in a background goroutine.
// It waits until the server is ready to accept connections, then returns.
// The actual listening address can be retrieved via Addr() after this returns.
func (h *ServerHandle) StartBackground() error {
if h == nil || h.s == nil {
return fmt.Errorf("server not initialized")
}
addr := net.JoinHostPort(h.s.config.Addr, h.s.config.Port)
// Create listener first to get the actual bound address
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
h.s.listener = listener
h.s.actualAddr = listener.Addr().String()
h.s.httpServer = &http.Server{
Handler: h.s,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
}
// Start serving in background
go func() {
h.s.httpServer.Serve(listener)
}()
return nil
}
// Stop stops the proxy server.
func (h *ServerHandle) Stop() error {
if h == nil || h.s == nil {
return nil
}
return h.s.Stop()
}
// Close is an alias of Stop.
func (h *ServerHandle) Close() error {
return h.Stop()
}
// Addr returns the effective listen address (host:port).
// If the server has started, returns the actual bound address.
// Otherwise, returns the configured address.
func (h *ServerHandle) Addr() string {
if h == nil || h.s == nil || h.s.config == nil {
return ""
}
// Return actual address if server has started
if h.s.actualAddr != "" {
return h.s.actualAddr
}
// Fallback to configured address
return net.JoinHostPort(h.s.config.Addr, h.s.config.Port)
}
// SetUpstreamProxy sets the upstream proxy and resets sessions.
func (h *ServerHandle) SetUpstreamProxy(proxyURL string) error {
if h == nil || h.s == nil || h.s.config == nil {
return fmt.Errorf("server not initialized")
}
h.s.config.UpstreamProxy = proxyURL
if h.s.sessionManager != nil {
h.s.sessionManager.CloseAll()
}
return nil
}
// ResetSessions closes all cached TLS sessions.
func (h *ServerHandle) ResetSessions() int {
if h == nil || h.s == nil || h.s.sessionManager == nil {
return 0
}
count := h.s.sessionManager.GetSessionCount()
h.s.sessionManager.CloseAll()
return count
}
// GetSupportedBrowsersForPython returns the list of supported browser names.
func GetSupportedBrowsersForPython() []string {
return GetSupportedBrowsers()
}
// GetSupportedVersionsForPython returns the supported major versions for a browser.
func GetSupportedVersionsForPython(browserName string) []int {
return GetSupportedVersions(browserName)
}
// GetBrowserFingerprintJSON returns a JSON string describing the fingerprint.
func GetBrowserFingerprintJSON(userAgent string) (string, error) {
fp, err := GetBrowserFingerprint(userAgent)
if err != nil {
return "", err
}
b, err := json.Marshal(fp)
if err != nil {
return "", err
}
return string(b), nil
}
// GetCAPEM returns the in-memory CA certificate in PEM format.
func (h *ServerHandle) GetCAPEM() []byte {
if h == nil || h.s == nil || h.s.certManager == nil {
return nil
}
return h.s.certManager.GetCACertPEM()
}