netcheck

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package netcheck 探测一个目标的可达性与「协议适配」。

它回答的问题是:这个目标该用 icmp 还是 tcp?该填什么端口?

为什么存在(源自一次真实事故):某教育网地址只响应 TCP:443、完全不响应 ICMP, 而另一个地址恰好相反。把监测任务的 ICMP 目标换成前者后,全部机器 100% 丢包, 排查很久才发现是协议不匹配。监控任务的探测类型(icmp/tcp/http)是每个任务一个, 选错了就必然全灭 —— 所以配置前应当先探测。

同一份逻辑同时驱动:

  • CLI: nekomari netcheck <host>
  • 面板: admin:netcheck (新建/编辑监测任务时的目标预检)

Index

Constants

This section is empty.

Variables

View Source
var DefaultPorts = []int{22, 80, 443}

DefaultPorts 是未指定端口时测试的常用端口。

Functions

This section is empty.

Types

type ICMPStat

type ICMPStat struct {
	Sent    int     `json:"sent"`
	Recv    int     `json:"recv"`
	LossPct float64 `json:"loss_pct"`
	MinMs   float64 `json:"min_ms"`
	AvgMs   float64 `json:"avg_ms"`
	MaxMs   float64 `json:"max_ms"`
	Err     string  `json:"err,omitempty"`
	// Denied 表示「因为没有权限而没测成」,**不是**「目标不可达」。
	// 这个区分很关键:非特权环境下的 ICMP 失败会得出完全相反的结论。
	Denied bool `json:"denied,omitempty"`
}

ICMPStat 是一次 ICMP 探测的统计。

type Options

type Options struct {
	Ports    []int         // 要测的 TCP 端口;为空则用 DefaultPorts
	Count    int           // ICMP 次数;<=0 则用 3
	Timeout  time.Duration // 单次超时;<=0 则用 3s
	SkipICMP bool          // 跳过 ICMP(例如已知环境无权限)
}

Options 控制探测行为。

type Report

type Report struct {
	Target  string    `json:"target"`
	Host    string    `json:"host"`
	IPsV4   []string  `json:"ips_v4"`
	IPsV6   []string  `json:"ips_v6"`
	DNSErr  string    `json:"dns_error,omitempty"`
	ICMP    ICMPStat  `json:"icmp"`
	TCP     []TCPStat `json:"tcp"`
	Verdict Verdict   `json:"verdict"`
	Summary string    `json:"summary"` // 一句话判定(中文)
	Advice  string    `json:"advice"`  // 给配置者的建议
}

Report 是一次完整探测的结果。

func Run

func Run(host string, opt Options) Report

Run 对 host 执行一次探测。host 可以是域名、IPv4、IPv6(可带 [..] 包裹)。 注意:探测在**调用方所在机器**上进行,因此结论反映的是该机器的可达性。

type TCPStat

type TCPStat struct {
	Port  int    `json:"port"`
	Open  bool   `json:"open"`
	RttMs int64  `json:"rtt_ms,omitempty"`
	Err   string `json:"err,omitempty"`
}

TCPStat 是单个端口的 TCP 握手结果。

type Verdict

type Verdict string

Verdict 是机器可读的判定结果,前端可据此决定提示样式。

const (
	VerdictBothOK      Verdict = "both_ok"              // ICMP 与 TCP 都可达
	VerdictICMPOnly    Verdict = "icmp_only"            // 仅 ICMP 可达
	VerdictTCPOnly     Verdict = "tcp_only"             // 仅 TCP 可达(不响应 ICMP)
	VerdictTCPUntested Verdict = "tcp_ok_icmp_untested" // TCP 通,但 ICMP 没测成(权限不足)
	VerdictICMPDenied  Verdict = "icmp_denied"          // ICMP 因权限失败
	VerdictUnreachable Verdict = "unreachable"          // 都不可达
	VerdictDNSFailure  Verdict = "dns_failure"          // 域名解析失败
)

Jump to

Keyboard shortcuts

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