Skip to content

Commit 6052a20

Browse files
committed
tiny bug fixed
1 parent 47d4f31 commit 6052a20

File tree

2 files changed

+23
-60
lines changed

2 files changed

+23
-60
lines changed

go-proxy-rotator

6.12 MB
Binary file not shown.

main.go

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,44 @@ import (
1515
"os"
1616
"strings"
1717
"sync"
18-
"time"
1918
)
2019

2120
type ProxyConfig struct {
2221
proxyURL *url.URL
22+
isDirect bool // Indicates if this is a direct connection
2323
}
2424

2525
type ProxyManager struct {
2626
proxies []*ProxyConfig
2727
currentIdx int
2828
mu sync.Mutex
2929
enableEdge bool
30-
lastUsed time.Time
3130
}
3231

3332
func NewProxyManager(enableEdge bool) *ProxyManager {
34-
return &ProxyManager{
33+
pm := &ProxyManager{
3534
proxies: make([]*ProxyConfig, 0),
3635
enableEdge: enableEdge,
37-
lastUsed: time.Now(),
3836
}
37+
38+
// If edge mode is enabled, add direct connection as first proxy
39+
if enableEdge {
40+
pm.proxies = append(pm.proxies, &ProxyConfig{isDirect: true})
41+
}
42+
43+
return pm
3944
}
4045

4146
func (pm *ProxyManager) LoadProxies(filename string) error {
4247
file, err := os.Open(filename)
4348
if err != nil {
4449
if pm.enableEdge {
50+
// If edge mode is enabled and no proxy file, that's okay - we still have direct
4551
return nil
4652
}
4753
return err
4854
}
49-
defer func(file *os.File) {
50-
_ = file.Close()
51-
}(file)
55+
defer file.Close()
5256

5357
scanner := bufio.NewScanner(file)
5458
for scanner.Scan() {
@@ -62,7 +66,10 @@ func (pm *ProxyManager) LoadProxies(filename string) error {
6266
return fmt.Errorf("invalid proxy URL: %s", err)
6367
}
6468

65-
pm.proxies = append(pm.proxies, &ProxyConfig{proxyURL: proxyURL})
69+
pm.proxies = append(pm.proxies, &ProxyConfig{
70+
proxyURL: proxyURL,
71+
isDirect: false,
72+
})
6673
}
6774

6875
if err := scanner.Err(); err != nil {
@@ -84,73 +91,29 @@ func (pm *ProxyManager) GetNextProxy() (*ProxyConfig, error) {
8491
return nil, fmt.Errorf("no proxies available")
8592
}
8693

87-
// Always increment the index
8894
proxy := pm.proxies[pm.currentIdx]
8995
pm.currentIdx = (pm.currentIdx + 1) % len(pm.proxies)
9096

91-
// Update last used time
92-
pm.lastUsed = time.Now()
93-
9497
return proxy, nil
9598
}
9699

97-
func (pm *ProxyManager) ShouldUseDirect() bool {
98-
pm.mu.Lock()
99-
defer pm.mu.Unlock()
100-
101-
if !pm.enableEdge {
102-
return false
103-
}
104-
105-
// If we have no proxies, always use direct
106-
if len(pm.proxies) == 0 {
107-
return true
108-
}
109-
110-
// In edge mode with proxies, use direct connection periodically
111-
// This ensures we rotate through direct connection as well
112-
return time.Since(pm.lastUsed) > 5*time.Second
113-
}
114-
115100
type ProxyDialer struct {
116101
manager *ProxyManager
117102
}
118103

119104
func (d *ProxyDialer) Dial(ctx context.Context, network, addr string) (net.Conn, error) {
120-
var lastError error
121-
122-
// Check if we should try direct connection first
123-
if d.manager.ShouldUseDirect() {
124-
conn, err := net.Dial(network, addr)
125-
if err == nil {
126-
return conn, nil
127-
}
128-
lastError = err
105+
proxy, err := d.manager.GetNextProxy()
106+
if err != nil {
107+
return nil, err
129108
}
130109

131-
// If we have proxies, try them
132-
if len(d.manager.proxies) > 0 {
133-
proxy, err := d.manager.GetNextProxy()
134-
if err != nil {
135-
if lastError != nil {
136-
return nil, fmt.Errorf("direct connection failed: %v, proxy error: %v", lastError, err)
137-
}
138-
return nil, err
139-
}
140-
141-
conn, err := d.dialWithProxy(proxy, network, addr)
142-
if err == nil {
143-
return conn, nil
144-
}
145-
lastError = err
146-
} else if !d.manager.enableEdge {
147-
return nil, fmt.Errorf("no proxies available and edge mode is disabled")
110+
// Handle direct connection
111+
if proxy.isDirect {
112+
return net.Dial(network, addr)
148113
}
149114

150-
if lastError != nil {
151-
return nil, fmt.Errorf("all connection attempts failed, last error: %v", lastError)
152-
}
153-
return nil, fmt.Errorf("no connection methods available")
115+
// Handle proxy connection
116+
return d.dialWithProxy(proxy, network, addr)
154117
}
155118

156119
func (d *ProxyDialer) dialWithProxy(proxy *ProxyConfig, network, addr string) (net.Conn, error) {

0 commit comments

Comments
 (0)