|
6 | 6 | "log" |
7 | 7 | "maps" |
8 | 8 | "net" |
| 9 | + "net/http" |
| 10 | + "net/url" |
9 | 11 | "sync" |
10 | 12 |
|
11 | 13 | "github.com/anacrolix/publicip" |
@@ -136,6 +138,17 @@ func (bt *BTServer) configure(ctx context.Context) { |
136 | 138 | } |
137 | 139 | } |
138 | 140 |
|
| 141 | + // Configure proxy if enabled |
| 142 | + if err := bt.configureProxy(); err != nil { |
| 143 | + log.Println("Proxy configuration error:", err) |
| 144 | + } |
| 145 | + |
| 146 | + // Update BTsets with current proxy settings for display in Client config |
| 147 | + if settings.ProxyURL != "" && settings.BTsets != nil { |
| 148 | + settings.BTsets.ProxyURL = settings.ProxyURL |
| 149 | + settings.BTsets.ProxyMode = settings.ProxyMode |
| 150 | + } |
| 151 | + |
139 | 152 | log.Println("Client config:", settings.BTsets) |
140 | 153 |
|
141 | 154 | var err error |
@@ -179,6 +192,73 @@ func (bt *BTServer) configure(ctx context.Context) { |
179 | 192 | } |
180 | 193 | } |
181 | 194 |
|
| 195 | +func (bt *BTServer) configureProxy() error { |
| 196 | + proxyURL := settings.ProxyURL |
| 197 | + if proxyURL == "" && settings.BTsets != nil && settings.BTsets.ProxyURL != "" { |
| 198 | + proxyURL = settings.BTsets.ProxyURL |
| 199 | + } |
| 200 | + |
| 201 | + if proxyURL == "" { |
| 202 | + return nil // No proxy configured |
| 203 | + } |
| 204 | + |
| 205 | + proxyMode := settings.ProxyMode |
| 206 | + if proxyMode == "" && settings.BTsets != nil && settings.BTsets.ProxyMode != "" { |
| 207 | + proxyMode = settings.BTsets.ProxyMode |
| 208 | + } |
| 209 | + if proxyMode == "" { |
| 210 | + proxyMode = "tracker" // default |
| 211 | + } |
| 212 | + |
| 213 | + // Parse and validate proxy URL |
| 214 | + parsedURL, err := url.Parse(proxyURL) |
| 215 | + if err != nil { |
| 216 | + return fmt.Errorf("invalid proxy URL: %w", err) |
| 217 | + } |
| 218 | + |
| 219 | + scheme := parsedURL.Scheme |
| 220 | + // Validate proxy protocol |
| 221 | + switch scheme { |
| 222 | + case "socks5", "socks5h", "socks4", "socks4a", "http", "https": |
| 223 | + // Supported protocols |
| 224 | + default: |
| 225 | + return fmt.Errorf("unsupported proxy protocol: %s (supported: http, https, socks4, socks4a, socks5, socks5h)", scheme) |
| 226 | + } |
| 227 | + |
| 228 | + if proxyMode == "full" { |
| 229 | + log.Printf("Configuring proxy for all BitTorrent traffic: %s://%s", scheme, parsedURL.Host) |
| 230 | + |
| 231 | + // Set ProxyURL - this will be used by anacrolix/torrent for all BitTorrent traffic |
| 232 | + bt.config.ProxyURL = proxyURL |
| 233 | + |
| 234 | + // Also set HTTPProxy explicitly for HTTP tracker requests |
| 235 | + bt.config.HTTPProxy = func(req *http.Request) (*url.URL, error) { |
| 236 | + return parsedURL, nil |
| 237 | + } |
| 238 | + |
| 239 | + log.Println("Proxy configured successfully for all BitTorrent connections (tracker, DHT, peers)") |
| 240 | + } else if proxyMode == "peers" { |
| 241 | + log.Printf("Configuring proxy for peer connections only: %s://%s", scheme, parsedURL.Host) |
| 242 | + |
| 243 | + // Set ProxyURL for peer connections, but don't set HTTPProxy |
| 244 | + // This routes DHT and peer connections through proxy, but not HTTP tracker requests |
| 245 | + bt.config.ProxyURL = proxyURL |
| 246 | + |
| 247 | + log.Println("Proxy configured successfully for peer and DHT connections only") |
| 248 | + } else { |
| 249 | + log.Printf("Configuring proxy for HTTP tracker requests only: %s://%s", scheme, parsedURL.Host) |
| 250 | + |
| 251 | + // Only set HTTPProxy for tracker requests, don't set ProxyURL |
| 252 | + bt.config.HTTPProxy = func(req *http.Request) (*url.URL, error) { |
| 253 | + return parsedURL, nil |
| 254 | + } |
| 255 | + |
| 256 | + log.Println("Proxy configured successfully for HTTP tracker connections only") |
| 257 | + } |
| 258 | + |
| 259 | + return nil |
| 260 | +} |
| 261 | + |
182 | 262 | func (bt *BTServer) GetTorrent(hash torrent.InfoHash) *Torrent { |
183 | 263 | if torr, ok := bt.torrents[hash]; ok { |
184 | 264 | return torr |
|
0 commit comments