-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
66 lines (60 loc) · 2.22 KB
/
Copy pathconfig.go
File metadata and controls
66 lines (60 loc) · 2.22 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
package main
import (
"flag"
"fmt"
"time"
)
// Config holds all configuration options for the proxy switcher
type Config struct {
BindAddr string
CheckInterval time.Duration
ScrapeInterval time.Duration
TestTimeout time.Duration
TestURL string
ProxyFile string
ProxyURL string
MaxFailures int
MaxLatency time.Duration
}
// LoadConfig parses command-line flags and returns the configuration
func LoadConfig() (*Config, error) {
bindAddr := flag.String("addr", "127.0.0.1:8080", "Local address to listen on")
checkInterval := flag.Duration("check-interval", 1*time.Minute, "Interval between background proxy health checks")
scrapeInterval := flag.Duration("scrape-interval", 10*time.Minute, "Interval between scraping/reloading proxy lists")
testTimeout := flag.Duration("timeout", 5*time.Second, "Timeout for testing a single proxy")
testURL := flag.String("test-url", "https://clients3.google.com/generate_204", "URL used to test proxy speed and health")
proxyFile := flag.String("file", "", "Path to a local file containing proxies (ip:port, one per line)")
proxyURL := flag.String("url", "", "Custom HTTP URL containing a list of proxies (ip:port, one per line)")
maxFailures := flag.Int("max-failures", 3, "Maximum consecutive check failures before removing a proxy")
maxLatency := flag.Duration("max-latency", 2*time.Second, "Maximum latency for a proxy to be considered fast")
flag.Parse()
if *checkInterval <= 0 {
return nil, fmt.Errorf("check-interval must be positive")
}
if *scrapeInterval <= 0 {
return nil, fmt.Errorf("scrape-interval must be positive")
}
if *testTimeout <= 0 {
return nil, fmt.Errorf("timeout must be positive")
}
if *testURL == "" {
return nil, fmt.Errorf("test-url cannot be empty")
}
if *maxFailures <= 0 {
return nil, fmt.Errorf("max-failures must be positive")
}
if *maxLatency <= 0 {
return nil, fmt.Errorf("max-latency must be positive")
}
return &Config{
BindAddr: *bindAddr,
CheckInterval: *checkInterval,
ScrapeInterval: *scrapeInterval,
TestTimeout: *testTimeout,
TestURL: *testURL,
ProxyFile: *proxyFile,
ProxyURL: *proxyURL,
MaxFailures: *maxFailures,
MaxLatency: *maxLatency,
}, nil
}