File size: 17,995 Bytes
e48a2d8 | 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | // Package proxypool manages persistent account routes and independently probed proxies.
package proxypool
import (
"context"
cryptorand "crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/rand/v2"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/misakano7545/workbuddy2api-panel/internal/safefile"
)
var (
ErrNotFound = errors.New("proxy not found")
ErrBound = errors.New("proxy is bound to accounts; remove bindings first")
)
// Error identifies a route failure without exposing proxy credentials.
type Error struct {
ProxyID string
Reason string
cause error
}
func (e *Error) Error() string { return "proxy route: " + e.Reason }
func (e *Error) Unwrap() error { return e.cause }
func IsError(err error) bool {
var e *Error
return errors.As(err, &e)
}
type Settings struct {
URL string `json:"url"`
IntervalSeconds int `json:"interval_seconds"`
TimeoutSeconds int `json:"timeout_seconds"`
FailureThreshold int `json:"failure_threshold"`
SuccessThreshold int `json:"success_threshold"`
}
func DefaultSettings() Settings {
return Settings{"https://www.workbuddy.cn/", 60, 10, 3, 2}
}
func (s Settings) validate() error {
u, err := url.Parse(s.URL)
if err != nil || u.Hostname() == "" || (u.Scheme != "http" && u.Scheme != "https") || u.User != nil || u.Fragment != "" {
return errors.New("health URL must be an HTTP(S) URL without credentials or fragment")
}
if s.IntervalSeconds < 1 || s.IntervalSeconds > 86400 || s.TimeoutSeconds < 1 || s.TimeoutSeconds > 300 ||
s.FailureThreshold < 1 || s.FailureThreshold > 100 || s.SuccessThreshold < 1 || s.SuccessThreshold > 100 {
return errors.New("health interval must be 1-86400s, timeout 1-300s, thresholds 1-100")
}
return nil
}
type Binding struct {
Mode string `json:"mode"`
ProxyID string `json:"proxy_id,omitempty"`
}
// realmKey 把 realm 归一化成配置键;只有 cn / global 合法,其余返回空(= 不匹配)。
func realmKey(realm string) string {
if realm == "cn" || realm == "global" {
return realm
}
return ""
}
type node struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
}
type disk struct {
Nodes []node `json:"proxies"`
Bindings map[string]Binding `json:"bindings"`
// Realms realm 级默认出口(issue #39 P3):cn/global → 绑定,账号没有显式
// 绑定时取它。cn/global 的上游域名与网络环境不同,运维通常要两个出口。
Realms map[string]Binding `json:"realms,omitempty"`
Settings Settings `json:"health"`
}
type health struct {
status string
failures int
successes int
checkedAt time.Time
attemptedAt time.Time
latencyMS int64
lastError string
}
type View struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Protocol string `json:"protocol"`
PasswordSet bool `json:"password_set"`
Enabled bool `json:"enabled"`
Status string `json:"status"`
Checking bool `json:"checking"`
LatencyMS int64 `json:"latency_ms"`
CheckedAt time.Time `json:"checked_at"`
LastError string `json:"last_error"`
Failures int `json:"failures"`
Successes int `json:"successes"`
BoundCount int `json:"bound_count"`
// Exit 出口画像(国家 / 住宅或机房 / ISP / ASN)。空字段 = 不知道,不是「没问题」。
Exit ExitInfo `json:"exit"`
}
type Snapshot struct {
Proxies []View `json:"proxies"`
Bindings map[string]Binding `json:"bindings"`
Realms map[string]Binding `json:"realms"`
Health Settings `json:"health"`
}
type Input struct {
Name string `json:"name"`
URL string `json:"url"`
Password string `json:"password"`
Enabled *bool `json:"enabled"`
}
type ImportResult struct {
Line int `json:"line"`
Status string `json:"status"`
ID string `json:"id,omitempty"`
Error string `json:"error,omitempty"`
// Name 订阅里给这个节点的名字(Clash 的 name / URI 的 #fragment)。仅作展示:
// 面板据此告诉用户「这条被跳过的是哪个节点」,比只报行号有用得多。
Name string `json:"name,omitempty"`
}
type Manager struct {
mu sync.Mutex
path string
data disk
health map[string]health
generation map[string]uint64
checking map[string]bool
requested map[string]bool
transports map[string]*http.Transport
base *http.Transport
dialer *net.Dialer
tlsOver TLSOverFunc // 经代理的 HTTPS 用它在隧道上握手(SetTLSOver);nil = net/http 原生握手
closed bool
wake chan struct{}
// Shared by periodic and manual probes, including concurrent HTTP handlers.
slots chan struct{}
// exit 每个节点的出口画像(ip / 国家 / 住宅或机房 / ISP),见 intel.go。
// 持久化不必要:重启后第一次探测会重新取。
exit map[string]ExitInfo
// lookupExitFn 出口画像的查询函数;nil = 关闭(默认,见 EnableExitLookup)。
lookupExitFn func(context.Context) ExitInfo
}
func New(path string, base *http.Transport, dialer *net.Dialer) (*Manager, error) {
if base == nil {
base = http.DefaultTransport.(*http.Transport)
}
if dialer == nil {
dialer = &net.Dialer{Timeout: 10 * time.Second, KeepAlive: 15 * time.Second}
}
m := &Manager{
path: path, base: base.Clone(), dialer: dialer,
data: disk{Nodes: []node{}, Bindings: map[string]Binding{}, Realms: map[string]Binding{}, Settings: DefaultSettings()},
health: map[string]health{}, generation: map[string]uint64{}, checking: map[string]bool{}, requested: map[string]bool{},
transports: map[string]*http.Transport{}, wake: make(chan struct{}, 1), slots: make(chan struct{}, 8),
exit: map[string]ExitInfo{},
}
if path != "" {
raw, err := safefile.Read(path, nil)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if err == nil {
if err := json.Unmarshal(raw, &m.data); err != nil {
return nil, fmt.Errorf("parse proxy pool: %w", err)
}
}
}
if err := m.data.Settings.validate(); err != nil {
return nil, err
}
seen := map[string]bool{}
for i := range m.data.Nodes {
n := &m.data.Nodes[i]
u, err := parseURL(n.URL)
if err != nil || n.ID == "" || m.generation[n.ID] != 0 || seen[uString(u)] {
return nil, errors.New("invalid or duplicate proxy in stored pool")
}
n.URL = u.String()
seen[n.URL] = true
m.generation[n.ID] = 1
}
if m.data.Bindings == nil {
m.data.Bindings = map[string]Binding{}
}
for _, b := range m.data.Bindings {
if err := m.validateBindingLocked(b); err != nil {
return nil, err
}
}
return m, nil
}
func uString(u *url.URL) string {
if u == nil {
return ""
}
return u.String()
}
func parseURL(raw string) (*url.URL, error) {
u, err := url.Parse(strings.TrimSpace(raw))
if err != nil || u == nil {
return nil, errors.New("invalid proxy URL")
}
u.Scheme = strings.ToLower(u.Scheme)
defaultPort := map[string]string{"http": "80", "https": "443", "socks5": "1080", "socks5h": "1080"}[u.Scheme]
if defaultPort == "" || u.Hostname() == "" || u.Opaque != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" ||
(u.Path != "" && u.Path != "/") || (u.User != nil && u.User.Username() == "") {
return nil, errors.New("proxy URL requires http/https/socks5/socks5h, host and optional credentials; paths and queries are unsupported")
}
port := u.Port()
if port == "" {
port = defaultPort
}
n, err := strconv.Atoi(port)
if err != nil || n < 1 || n > 65535 {
return nil, errors.New("proxy port must be 1-65535")
}
u.Host = net.JoinHostPort(strings.ToLower(u.Hostname()), strconv.Itoa(n))
u.Path, u.RawPath = "", ""
return u, nil
}
func (m *Manager) copyLocked() disk {
d := m.data
d.Nodes = append([]node{}, d.Nodes...)
d.Bindings = make(map[string]Binding, len(m.data.Bindings))
for k, v := range m.data.Bindings {
d.Bindings[k] = v
}
return d
}
func (m *Manager) commitLocked(d disk) error {
if m.closed {
return errors.New("proxy pool is closed")
}
if m.path != "" {
raw, err := json.MarshalIndent(d, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(m.path), 0o700); err != nil {
return err
}
if err := safefile.WriteKeep(m.path, raw, 0o600); err != nil {
return err
}
}
m.data = d
m.signal()
return nil
}
func (m *Manager) signal() {
select {
case m.wake <- struct{}{}:
default:
}
}
func (m *Manager) Snapshot() Snapshot {
m.mu.Lock()
defer m.mu.Unlock()
d := m.copyLocked()
s := Snapshot{Proxies: []View{}, Bindings: d.Bindings, Realms: d.Realms, Health: d.Settings}
counts := map[string]int{}
for _, b := range d.Bindings {
if b.Mode == "fixed" {
counts[b.ProxyID]++
}
}
for _, n := range d.Nodes {
h := m.health[n.ID]
status := h.status
if status == "" {
status = "pending"
}
if !n.Enabled {
status = "disabled"
}
u, _ := parseURL(n.URL)
passwordSet := false
if u.User != nil {
_, passwordSet = u.User.Password()
u.User = url.User(u.User.Username())
}
s.Proxies = append(s.Proxies, View{
ID: n.ID, Name: n.Name, URL: u.String(), Protocol: u.Scheme, PasswordSet: passwordSet,
Enabled: n.Enabled, Status: status, Checking: m.checking[n.ID], LatencyMS: h.latencyMS,
CheckedAt: h.checkedAt, LastError: h.lastError, Failures: h.failures, Successes: h.successes, BoundCount: counts[n.ID],
Exit: m.exit[n.ID],
})
}
return s
}
func (m *Manager) Import(text string) ([]ImportResult, error) {
m.mu.Lock()
defer m.mu.Unlock()
d := m.copyLocked()
seen := map[string]string{}
for _, n := range d.Nodes {
seen[n.URL] = n.ID
}
results := []ImportResult{}
var added []string
// 订阅归一化(subscription.go):明文 URI / 整段 base64 / Clash YAML 三种形态
// 都在这里变成「一行一个受支持的 URL」。认不出的条目带 Reason 回来,按
// unsupported / invalid 逐条报账——**不静默丢弃**,否则用户粘了 200 个节点
// 只进来 3 个却不知道为什么。
entries, err := ParseSubscription(text)
if err != nil {
return nil, err
}
for _, entry := range entries {
r := ImportResult{Line: entry.Line, Name: entry.Name}
if entry.URL == "" {
// 认得出来但协议不支持 → unsupported(面板不计入「无效」,也不把整段
// 订阅原样回填到输入框:那对用户没有意义)。真认不出的 → invalid。
r.Status = "invalid"
if entry.Unsupported {
r.Status = "unsupported"
}
r.Error = entry.Reason
results = append(results, r)
continue
}
r.Status = "invalid"
u, err := parseURL(entry.URL)
if err != nil {
r.Error = err.Error()
} else if id := seen[u.String()]; id != "" {
r.Status, r.ID = "duplicate", id
} else {
var b [16]byte
if _, err := cryptorand.Read(b[:]); err != nil {
return nil, err
}
id := hex.EncodeToString(b[:])
d.Nodes = append(d.Nodes, node{ID: id, Name: nodeName(u.Host, entry.Name), URL: u.String(), Enabled: true})
seen[u.String()] = id
added = append(added, id)
r.Status, r.ID = "added", id
}
results = append(results, r)
}
if len(added) > 0 {
if err := m.commitLocked(d); err != nil {
return nil, err
}
for _, id := range added {
m.generation[id] = 1
}
}
return results, nil
}
// nodeName 节点显示名:订阅给了名字就用它(最多 128 字节,与 Update 的长度上限
// 一致),否则沿用 host。截断按字节做而不是按 rune——上限本来就是字节语义
// (Update 里 len(in.Name) > 128),两个入口必须同一把尺子。
func nodeName(host, want string) string {
name := strings.TrimSpace(want)
if name == "" {
return host
}
if len(name) > 128 {
name = name[:128]
}
return name
}
func (m *Manager) Update(id string, in Input) error {
m.mu.Lock()
defer m.mu.Unlock()
d := m.copyLocked()
for i, n := range d.Nodes {
if n.ID != id {
continue
}
u, _ := parseURL(n.URL)
if in.URL != "" {
var err error
u, err = parseURL(in.URL)
if err != nil {
return err
}
if u.User != nil {
old, _ := parseURL(n.URL)
_, supplied := u.User.Password()
if !supplied && old.User != nil {
if password, ok := old.User.Password(); ok {
u.User = url.UserPassword(u.User.Username(), password)
}
}
}
}
if in.Password != "" {
if u.User == nil {
return errors.New("password requires a username in the proxy URL")
}
u.User = url.UserPassword(u.User.Username(), in.Password)
}
for _, other := range d.Nodes {
if other.ID != id && other.URL == u.String() {
return errors.New("duplicate proxy URL")
}
}
if in.Name != "" {
if len(in.Name) > 128 {
return errors.New("proxy name must not exceed 128 bytes")
}
d.Nodes[i].Name = strings.TrimSpace(in.Name)
}
d.Nodes[i].URL = u.String()
if in.Enabled != nil {
d.Nodes[i].Enabled = *in.Enabled
}
reset := d.Nodes[i].URL != n.URL || d.Nodes[i].Enabled != n.Enabled
if err := m.commitLocked(d); err != nil {
return err
}
if reset {
m.resetLocked(id)
}
return nil
}
return ErrNotFound
}
func (m *Manager) resetLocked(id string) {
m.generation[id]++
delete(m.health, id)
delete(m.exit, id) // 换了地址就是换了出口,旧画像不能留着
if tr := m.transports[id]; tr != nil {
tr.CloseIdleConnections()
delete(m.transports, id)
}
}
func (m *Manager) Delete(id string) error {
m.mu.Lock()
defer m.mu.Unlock()
for _, b := range m.data.Bindings {
if b.Mode == "fixed" && b.ProxyID == id {
return ErrBound
}
}
for _, b := range m.data.Realms {
if b.Mode == "fixed" && b.ProxyID == id {
return ErrBound
}
}
d := m.copyLocked()
for i, n := range d.Nodes {
if n.ID == id {
d.Nodes = append(d.Nodes[:i], d.Nodes[i+1:]...)
if err := m.commitLocked(d); err != nil {
return err
}
m.resetLocked(id)
return nil
}
}
return ErrNotFound
}
func (m *Manager) validateBindingLocked(b Binding) error {
switch b.Mode {
case "random", "direct":
if b.ProxyID != "" {
return errors.New("proxy_id is only valid in fixed mode")
}
case "fixed":
for _, n := range m.data.Nodes {
if n.ID == b.ProxyID {
return nil
}
}
return ErrNotFound
default:
return errors.New("proxy mode must be random, direct or fixed")
}
return nil
}
func (m *Manager) Bind(uid string, b Binding) error {
m.mu.Lock()
defer m.mu.Unlock()
if err := m.validateBindingLocked(b); err != nil {
return err
}
d := m.copyLocked()
if b.Mode == "random" {
delete(d.Bindings, uid)
} else {
d.Bindings[uid] = b
}
return m.commitLocked(d)
}
func (m *Manager) Unbind(uid string) error { return m.Bind(uid, Binding{Mode: "random"}) }
// BindRealm 设置 realm 级默认出口(issue #39 P3)。random = 恢复「随机池」,
// 等价于删掉这条默认;direct = 该域直连。账号级绑定优先于它。
func (m *Manager) BindRealm(realm string, b Binding) error {
if realmKey(realm) == "" {
return errors.New("realm must be cn or global")
}
m.mu.Lock()
defer m.mu.Unlock()
if err := m.validateBindingLocked(b); err != nil {
return err
}
d := m.copyLocked()
if d.Realms == nil {
d.Realms = map[string]Binding{}
}
if b.Mode == "random" {
delete(d.Realms, realmKey(realm))
} else {
d.Realms[realmKey(realm)] = b
}
return m.commitLocked(d)
}
// RequestCheck queues probes on the service lifecycle, independent of browser disconnects.
// An empty ID requests all nodes, including manually disabled nodes.
func (m *Manager) RequestCheck(id string) error {
m.mu.Lock()
defer m.mu.Unlock()
found := id == ""
for _, n := range m.data.Nodes {
if id == "" || n.ID == id {
m.requested[n.ID] = true
found = true
}
}
if !found {
return ErrNotFound
}
m.signal()
return nil
}
func (m *Manager) Configure(s Settings) error {
if err := s.validate(); err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
d := m.copyLocked()
if d.Settings == s {
return nil
}
d.Settings = s
if err := m.commitLocked(d); err != nil {
return err
}
for _, n := range d.Nodes {
m.resetLocked(n.ID)
}
return nil
}
func (m *Manager) selectLocked(uid, realm string) (node, bool, error) {
if m.closed {
return node{}, false, &Error{Reason: "pool closed"}
}
b := m.data.Bindings[uid]
// 账号没有显式绑定时取 realm 默认出口(issue #39 P3):查表顺序
// 账号绑定 > realm 默认 > 随机池 > 直连。显式 direct 依旧赢过 realm 默认。
if _, bound := m.data.Bindings[uid]; !bound {
if rb, ok := m.data.Realms[realmKey(realm)]; ok {
b = rb
}
}
// 随机绑定下「一条启用中的代理都没有」= 直连:原来只看节点条数,代理全部被手工
// 停用时仍进抽签、count==0,于是每个请求都 503「no healthy proxy available」
// (issue #150)。停用即关闭,与「一条代理都没配」同义。
// 只对非 fixed 生效:显式钉住的代理不可用时静默直连,等于把本机 IP 暴露给上游。
if b.Mode == "direct" {
return node{}, false, nil
}
if b.Mode != "fixed" {
anyEnabled := false
for _, n := range m.data.Nodes {
if n.Enabled {
anyEnabled = true
break
}
}
if !anyEnabled {
return node{}, false, nil
}
}
var selected node
count := 0
for _, n := range m.data.Nodes {
if b.Mode == "fixed" && b.ProxyID != n.ID {
continue
}
if n.Enabled && m.health[n.ID].status == "healthy" {
count++
if rand.IntN(count) == 0 {
selected = n
}
}
}
if count == 0 {
return node{}, false, &Error{ProxyID: b.ProxyID, Reason: "no healthy proxy available"}
}
return selected, true, nil
}
func (m *Manager) Close() {
m.mu.Lock()
defer m.mu.Unlock()
m.closed = true
for _, tr := range m.transports {
tr.CloseIdleConnections()
}
}
|