ipsource

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ipsource 获取本机公网 IP,支持查询 URL、读取网卡和执行命令三种方式。

Index

Constants

View Source
const (
	TypeURL       = "url"
	TypeInterface = "netInterface"
	TypeCommand   = "command"
)

获取方式

Variables

View Source
var DefaultIPv4URLs = []string{
	"https://ipv4.ddnspod.com",
	"https://api.ipify.org",
	"https://ipv4.icanhazip.com",
	"https://myip.ipip.net",
}

DefaultIPv4URLs 是默认的 IPv4 查询地址,均为第三方公共服务,用户可在界面修改。

View Source
var DefaultIPv6URLs = []string{
	"https://ipv6.ddnspod.com",
	"https://api6.ipify.org",
	"https://ipv6.icanhazip.com",
	"https://v6.ident.me",
}

DefaultIPv6URLs 是默认的 IPv6 查询地址。

Functions

func Extract

func Extract(text string, fam Family) []netip.Addr

Extract 从任意文本中提取指定地址族的地址,保持出现顺序并去重。

func Get

func Get(ctx context.Context, s Source, fam Family) (netip.Addr, error)

Get 按配置获取一个地址。

func IsPublic

func IsPublic(a netip.Addr) bool

IsPublic 判断地址是否可用于公网解析(排除回环、链路本地、私有、ULA 等)。

func ReplaceRetiredURLs added in v0.6.5

func ReplaceRetiredURLs(urls []string) ([]string, bool)

ReplaceRetiredURLs 把已停止服务的查询地址换成替代地址,替代地址已在列表中时直接去掉旧地址。 返回新列表以及是否有改动。

Types

type Family

type Family int

Family 表示地址族。

const (
	IPv4 Family = 4
	IPv6 Family = 6
)

地址族取值

type InterfaceInfo

type InterfaceInfo struct {
	Name  string   `json:"name"`
	Addrs []string `json:"addrs"`
}

InterfaceInfo 是网卡及其地址,供界面选择。

func DemoInterfaces added in v0.6.8

func DemoInterfaces() []InterfaceInfo

DemoInterfaces 返回演示模式下展示的示例网卡,地址全部取自文档保留地址段。

func ListInterfaces

func ListInterfaces() ([]InterfaceInfo, error)

ListInterfaces 列出处于启用状态的网卡。

type Source

type Source struct {
	Enable bool   `json:"enable"`
	Type   string `json:"type"`
	// URLs 按顺序尝试,直到成功
	URLs []string `json:"urls"`
	// Interface 为网卡名,Type 为 netInterface 时使用
	Interface string `json:"interface"`
	// Regex 用于从多个候选地址中筛选,留空取第一个合适的地址
	Regex string `json:"regex"`
	// Command 通过 sh -c(Windows 为 cmd /C)执行,从输出中提取 IP
	Command string `json:"command"`
}

Source 描述一种获取方式。

func (*Source) Validate

func (s *Source) Validate() error

Validate 校验配置。

Jump to

Keyboard shortcuts

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