detector

package
v0.0.0-...-1f6d713 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GitHub520URL = "https://raw.hellogithub.com/hosts"
	Ineo6URL     = "https://gitlab.com/ineo6/hosts/-/raw/master/hosts"
)

数据源 URL

Variables

View Source
var DefaultIPs = map[string][]string{
	"github.com": {
		"20.205.243.166",
		"140.82.112.4",
		"140.82.114.4",
	},
	"alive.github.com": {
		"140.82.113.25",
	},
	"live.github.com": {
		"140.82.113.25",
	},
	"ssh.github.com": {
		"140.82.112.35",
		"140.82.113.35",
	},
	"gist.github.com": {
		"20.205.243.166",
	},
	"raw.githubusercontent.com": {
		"185.199.110.133",
		"185.199.108.133",
	},
	"api.github.com": {
		"20.205.243.168",
		"140.82.112.5",
	},
	"codeload.github.com": {
		"20.205.243.165",
		"140.82.112.10",
	},
	"avatars.githubusercontent.com": {
		"185.199.110.133",
	},
	"github.githubassets.com": {
		"185.199.111.154",
	},
	"objects.githubusercontent.com": {
		"185.199.110.133",
	},
	"media.githubusercontent.com": {
		"185.199.110.133",
	},
	"user-images.githubusercontent.com": {
		"185.199.110.133",
	},
	"github-cloud.s3.amazonaws.com": {
		"16.15.183.87",
	},
}

DefaultIPs 内置的兜底 IP 列表 当所有数据源均不可达时使用这份列表 注意:这些 IP 可能会过时,仅作为应急兜底

View Source
var TargetDomains = []string{
	"github.com",
	"alive.github.com",
	"live.github.com",
	"ssh.github.com",
	"gist.github.com",
	"raw.githubusercontent.com",
	"api.github.com",
	"codeload.github.com",
	"avatars.githubusercontent.com",
	"github.githubassets.com",
	"objects.githubusercontent.com",
	"media.githubusercontent.com",
	"user-images.githubusercontent.com",
	"github-cloud.s3.amazonaws.com",
	"collector.github.com",
}

TargetDomains 维护的目标 GitHub 域名清单

Functions

func GetDefaultIPs

func GetDefaultIPs() map[string][]string

GetDefaultIPs 返回内置默认 IP 列表的副本

func IsPingAvailable

func IsPingAvailable() bool

IsPingAvailable 检测系统 ping 命令是否可用

func ScoreIP

func ScoreIP(entry *IPEntry) float64

ScoreIP 计算单个 IP 的质量评分(0-100) 评分维度:HTTPS 验证 + 延迟 + 丢包率 + 端口连通

func SelectBestIPs

func SelectBestIPs(entries []IPEntry, backupCount int) (best *IPEntry, backups []IPEntry)

SelectBestIPs 从排序后的 IP 列表中选择最优 IP 和备选 IP 返回最优 IP 和最多 backupCount 个备选 IP 对 github.com 域名:如果没有任何 Available 的候选,返回 nil(不选择不可用的 IP) 对其他域名:如果没有 Available 的候选,退而求其次返回排名第一的 IP

Types

type DetectResult

type DetectResult struct {
	Domains   map[string]*DomainIPs `json:"domains"`
	Timestamp int64                 `json:"timestamp"`
	ICMPUsed  bool                  `json:"icmp_used"`  // 是否使用了 ICMP 检测
	HTTPSUsed bool                  `json:"https_used"` // 是否使用了 HTTPS 验证
}

DetectResult 表示一次完整检测的结果

type Detector

type Detector struct {
	// contains filtered or unexported fields
}

Detector 是统一的 IP 检测器,协调数据源获取、多维度检测、评分筛选

func NewDetector

func NewDetector(logger zerolog.Logger) *Detector

NewDetector 创建检测器

func (*Detector) DetectAll

func (d *Detector) DetectAll(ctx context.Context) (*DetectResult, error)

DetectAll 执行完整的 IP 检测流程 1. 从数据源获取候选 IP(失败则使用默认列表) 2. 对每个 IP 执行多维度检测(ICMP + TCP 443 + TCP 22 + HTTPS) 3. 评分排序,选择最优和备选 IP

type DomainIPs

type DomainIPs struct {
	Domain string    `json:"domain"`
	IPs    []IPEntry `json:"ips"`
	BestIP *IPEntry  `json:"best_ip,omitempty"` // 最优 IP
}

DomainIPs 表示一个域名对应的多个候选 IP

type HTTPHostsSource

type HTTPHostsSource struct {
	// contains filtered or unexported fields
}

HTTPHostsSource 从 hosts 文本源获取候选 IP

func NewGitHub520Source

func NewGitHub520Source() *HTTPHostsSource

NewGitHub520Source 创建 GitHub520 数据源

func NewHTTPHostsSource

func NewHTTPHostsSource(name, url string) *HTTPHostsSource

NewHTTPHostsSource 创建通用 hosts 数据源

func NewIneo6Source

func NewIneo6Source() *HTTPHostsSource

NewIneo6Source 创建 ineo6/hosts 数据源

func (*HTTPHostsSource) FetchIPs

func (s *HTTPHostsSource) FetchIPs(ctx context.Context) (map[string][]string, error)

FetchIPs 获取 hosts 格式的域名-IP 映射

func (*HTTPHostsSource) Name

func (s *HTTPHostsSource) Name() string

type HTTPSCheckResult

type HTTPSCheckResult struct {
	IP             string
	Domain         string
	OK             bool    // 最终 HTTPS 验证是否成功
	BasicOK        bool    // 基础 HTTPS 是否成功(TLS 握手 + HTTP 响应)
	BrowserOK      bool    // 浏览器导向验证是否成功(仅对 github.com 严格要求)
	BrowserReason  string  // 浏览器导向验证结果说明
	Port443OK      bool    // TCP 443 是否已建立连接(即便后续 TLS/HTTP 失败也会保留)
	ConnectLatency float64 // TCP 443 建连延迟(毫秒)
	Latency        float64 // HTTPS 完整延迟(毫秒),包含 TCP 建连 + TLS 握手 + HTTP 首包往返
	StatusCode     int
	BodyBytes      int64
	Error          error
}

HTTPSCheckResult 表示一次 HTTPS 验证结果

type HTTPSChecker

type HTTPSChecker struct {
	// contains filtered or unexported fields
}

HTTPSChecker HTTPS 应用层验证器 通过真正的 HTTPS 请求(含 TLS 证书校验)验证候选 IP 的实际可用性

func NewHTTPSChecker

func NewHTTPSChecker(timeout time.Duration) *HTTPSChecker

NewHTTPSChecker 创建 HTTPS 验证器

func NewHTTPSCheckerWithConcurrency

func NewHTTPSCheckerWithConcurrency(timeout time.Duration, concurrency int) *HTTPSChecker

NewHTTPSCheckerWithConcurrency 创建带并发限制的 HTTPS 验证器

func (*HTTPSChecker) Check

func (c *HTTPSChecker) Check(ctx context.Context, ip, domain string) HTTPSCheckResult

Check 对指定 IP 执行 HTTPS 验证 流程:TCP 443 建连 → TLS 握手(验证证书匹配目标域名) → HTTP GET 请求 → 先看响应头,再按需读取少量正文校验页面特征

func (*HTTPSChecker) CheckIPs

func (c *HTTPSChecker) CheckIPs(ctx context.Context, ips []string, domain string) []HTTPSCheckResult

CheckIPs 并发对多个 IP 执行 HTTPS 验证

type IPEntry

type IPEntry struct {
	IP              string  `json:"ip"`
	Domain          string  `json:"domain"`
	Latency         float64 `json:"latency_ms"`        // 延迟(毫秒)
	LossRate        float64 `json:"loss_rate"`         // 丢包率(0.0-1.0)
	Port443         bool    `json:"port_443"`          // TCP 443 端口是否连通
	Port22          bool    `json:"port_22"`           // TCP 22 端口是否连通
	BasicHTTPS      bool    `json:"basic_https"`       // 基础 HTTPS 是否通过(TLS 握手 + HTTP 响应)
	HTTPS           bool    `json:"https"`             // 最终 HTTPS 验证是否通过(github.com 需满足浏览器导向校验)
	HTTPSLatency    float64 `json:"https_latency_ms"`  // HTTPS 验证延迟(毫秒)
	HTTPSStatusCode int     `json:"https_status_code"` // HTTPS 响应状态码
	BrowserOK       bool    `json:"browser_ok"`        // 浏览器导向验证是否通过(仅 github.com 严格要求)
	BrowserReason   string  `json:"browser_reason,omitempty"`
	Score           float64 `json:"score"`     // 质量评分(0-100)
	Available       bool    `json:"available"` // 是否可用
}

IPEntry 表示一个 IP 检测结果

func FallbackIP

func FallbackIP(domainIPs *DomainIPs) *IPEntry

FallbackIP 在主 IP 失效时切换到备选 IP 传入当前域名的检测结果,返回可用的备选 IP 对 github.com 域名:没有可用候选时返回 nil,避免写入不可用 IP 导致退化

func RankIPs

func RankIPs(entries []IPEntry) []IPEntry

RankIPs 对 IP 列表按评分排序,返回排序后的列表

type MultiSource

type MultiSource struct {
	// contains filtered or unexported fields
}

MultiSource 多数据源聚合,支持汇总多个成功结果

func NewMultiSource

func NewMultiSource(sources ...Source) *MultiSource

NewMultiSource 创建多数据源聚合器

func (*MultiSource) FetchIPs

func (m *MultiSource) FetchIPs(ctx context.Context) (map[string][]string, error)

FetchIPs 聚合所有成功数据源的结果,全部失败时返回错误

func (*MultiSource) Name

func (m *MultiSource) Name() string

type PingResult

type PingResult struct {
	IP       string
	Latency  float64 // 平均延迟(毫秒)
	LossRate float64 // 丢包率(0.0-1.0)
	OK       bool    // 是否成功
	Error    error
}

PingResult 表示一次 ping 检测结果

type Pinger

type Pinger struct {
	// contains filtered or unexported fields
}

Pinger 通过系统 ping 命令检测 IP 可达性

func NewPinger

func NewPinger(count int, timeout time.Duration) *Pinger

NewPinger 创建 Pinger 实例

func (*Pinger) Ping

func (p *Pinger) Ping(ctx context.Context, ip string) PingResult

Ping 对指定 IP 执行 ping 检测

type Source

type Source interface {
	// FetchIPs 获取域名-IP 映射列表
	FetchIPs(ctx context.Context) (map[string][]string, error)
	// Name 返回数据源名称
	Name() string
}

Source 定义数据源接口

type TCPCheckResult

type TCPCheckResult struct {
	IP      string
	Port    int
	OK      bool
	Latency float64 // 连接耗时(毫秒)
	Error   error
}

TCPCheckResult 表示一次 TCP 端口检测结果

type TCPChecker

type TCPChecker struct {
	// contains filtered or unexported fields
}

TCPChecker TCP 端口检测器

func NewTCPChecker

func NewTCPChecker(timeout time.Duration) *TCPChecker

NewTCPChecker 创建 TCP 端口检测器

func (*TCPChecker) CheckIPPorts

func (c *TCPChecker) CheckIPPorts(ctx context.Context, ips []string, ports []int) map[string][]TCPCheckResult

CheckIPPorts 对多个 IP 并发检测指定端口列表

func (*TCPChecker) CheckPort

func (c *TCPChecker) CheckPort(ctx context.Context, ip string, port int) TCPCheckResult

CheckPort 检测指定 IP 的 TCP 端口是否可连通

func (*TCPChecker) CheckPorts

func (c *TCPChecker) CheckPorts(ctx context.Context, ip string, ports []int) []TCPCheckResult

CheckPorts 并发检测指定 IP 的多个 TCP 端口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL