-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghproxy.go
174 lines (157 loc) · 4.13 KB
/
ghproxy.go
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
package selfupdate
import (
"errors"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/starudream/go-lib/core/v2/gh"
"github.com/starudream/go-lib/core/v2/slog"
"github.com/starudream/go-lib/core/v2/utils/maputil"
"github.com/starudream/go-lib/core/v2/utils/osutil"
"github.com/starudream/go-lib/resty/v2"
"github.com/starudream/go-lib/resty/v2/ping"
)
const (
githubDefault = "https://github.com/"
githubReleaseFile = "starudream/go-lib/releases/download/v2.0.0-rc.1/ping"
)
var githubProxies = []string{
githubDefault,
"https://ghproxy.net/https://github.com/",
"https://gh-proxy.com/https://github.com/",
"https://mirror.ghproxy.com/https://github.com/",
"https://ghproxy.starudream.cn/https://github.com/",
}
type GithubProxy struct {
proxies []string
timeout time.Duration
client *resty.Client
stats maputil.SyncMap[string, *ping.Statistics]
costs maputil.SyncMap[string, time.Duration]
}
func (t *GithubProxy) Add(proxies ...string) *GithubProxy {
for i := 0; i < len(proxies); i++ {
proxy := proxies[i]
u, err := url.Parse(proxy)
if err != nil {
slog.Warn("invalid github proxy: %v", err, slog.String("proxy", proxy))
}
u.ForceQuery = false
u.RawQuery = ""
u.Fragment = ""
u.RawFragment = ""
t.proxies = append(t.proxies, u.String())
}
return t
}
func (t *GithubProxy) SetTimeout(timeout time.Duration) *GithubProxy {
t.timeout = timeout
return t
}
func (t *GithubProxy) Check() *GithubProxy {
if len(t.proxies) == 0 {
t.proxies = make([]string, len(githubProxies))
copy(t.proxies, githubProxies)
}
if t.timeout <= 0 {
t.timeout = 3 * time.Second
}
t.client = resty.New().SetTimeout(t.timeout)
slog.Debug("github proxy check, maybe take a few minutes", slog.Int("total", len(t.proxies)))
wg := sync.WaitGroup{}
wg.Add(2 * len(t.proxies))
for _, proxy := range t.proxies {
go func(proxy string) {
defer wg.Done()
t.stats.Store(proxy, t.ping(proxy))
}(proxy)
go func(proxy string) {
defer wg.Done()
t.costs.Store(proxy, t.down(proxy))
}(proxy)
}
wg.Wait()
return t
}
func (t *GithubProxy) ping(proxy string) *ping.Statistics {
stat, err := ping.Ping(ping.WithAddr(osutil.Must1(url.Parse(proxy)).Host), ping.WithCount(1), ping.WithTimeout(3*time.Second))
if err != nil {
slog.Warn("github proxy ping error: %v", err, slog.String("proxy", proxy))
}
return stat
}
func (t *GithubProxy) down(proxy string) time.Duration {
resp, err := t.client.R().SetDoNotParseResponse(true).Get(proxy + githubReleaseFile)
defer gh.Close(resp.RawBody())
if err != nil || resp.StatusCode() != 200 {
if err == nil {
err = errors.New(resp.Status())
}
slog.Warn("github proxy download error: %v", err, slog.String("proxy", proxy))
return -1
}
return resp.Time()
}
func (t *GithubProxy) Items() []string {
t.sort()
items := make([]string, len(t.proxies))
for i, proxy := range t.proxies {
stat, _ := t.stats.Load(proxy)
cost, _ := t.costs.Load(proxy)
if proxy != githubDefault {
proxy = strings.TrimSuffix(proxy, githubDefault)
}
if stat == nil || cost < 0 {
items[i] = fmt.Sprintf("%-40s ***", proxy)
} else {
items[i] = fmt.Sprintf("%-40s %s/%s/%s", proxy, strconv.Itoa(int(stat.PacketLoss))+"%", stat.AvgRtt.Truncate(time.Microsecond), cost.Truncate(time.Millisecond))
}
}
return items
}
func (t *GithubProxy) Fast() string {
t.sort()
if len(t.proxies) == 0 {
return githubDefault
}
proxy := t.proxies[0]
stat, _ := t.stats.Load(proxy)
if stat == nil || stat.PacketLoss == 100 {
return githubDefault
}
return proxy
}
func (t *GithubProxy) sort() {
if len(t.proxies) == 0 {
return
}
// cost > loss > rtt
sort.Slice(t.proxies, func(i, j int) bool {
pi, pj := t.proxies[i], t.proxies[j]
ci, _ := t.costs.Load(pi)
cj, _ := t.costs.Load(pj)
if ci < 0 && cj < 0 {
} else if ci < 0 {
return false
} else if cj < 0 {
return true
}
si, _ := t.stats.Load(pi)
sj, _ := t.stats.Load(pj)
if si == nil && sj == nil {
return pi < pj
} else if si == nil {
return false
} else if sj == nil {
return true
}
if si.PacketLoss == sj.PacketLoss {
return si.AvgRtt < sj.AvgRtt
}
return si.PacketLoss < sj.PacketLoss
})
}