myip

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2022 License: MIT Imports: 5 Imported by: 4

README

获取本机内网 / 公网 IP

使用

package main

import (
	"fmt"

	"github.com/fufuok/utils/myip"
)

var (
	// 服务器 IP
	InternalIPv4  string
	ExternalIPv4  string
	ExternalIPAny string
)

func init() {
	// 推荐方式
	go func() {
		InternalIPv4 = myip.InternalIPv4()
	}()
	go func() {
		ExternalIPv4 = myip.ExternalIPv4()
	}()
	go func() {
		ExternalIPAny = myip.ExternalIPAny(5)
	}()
}

func main() {
	fmt.Println("MyIP(可能为空, 但不阻塞)", InternalIPv4, ExternalIPv4, ExternalIPAny)

	fmt.Println("获取外网地址 (IPv4):", myip.ExternalIPv4())
	fmt.Println("获取外网地址 (IPv6):", myip.ExternalIPv6())
	fmt.Println("获取外网地址 (出口公网地址, 优先获取 IPv6):", myip.ExternalIP())
	fmt.Println("获取外网地址 (出口公网地址 IPv4):", myip.ExternalIP("ipv4"))
	fmt.Println("获取外网地址 (出口公网地址 IPv6):", myip.ExternalIP("ipv6"))

	fmt.Println("获取内网地址 (IPv4):", myip.InternalIPv4())
	fmt.Println("获取内网地址 (临时 IPv6 地址):", myip.InternalIPv6())
	fmt.Println("获取内网地址 (出口本地地址):", myip.InternalIP("", ""))
	fmt.Println("获取内网地址 (出口本地地址):", myip.InternalIP("1.1.1.1:53", "udp"))
	fmt.Println("获取内网地址 (出口本地地址):", myip.InternalIP("baidu.com:443", "tcp"))
	fmt.Println("获取内网地址 (出口本地地址):", myip.InternalIP("1.1.1.1", "ip4:icmp"))

	fmt.Println("获取本地地址 (第一个):", myip.LocalIP())
	fmt.Println("获取所有本地地址 (IPv4):", myip.LocalIPv4s())

	fmt.Println("MyIP:", InternalIPv4, ExternalIPv4, ExternalIPAny)
}

ff

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExternalIP

func ExternalIP(v ...string) string

ExternalIP 获取外网地址 (出口公网地址)

func ExternalIPAny added in v0.1.6

func ExternalIPAny(retries ...int) string

ExternalIPAny 获取外网地址

func ExternalIPv4

func ExternalIPv4() string

ExternalIPv4 获取外网地址 (IPv4)

func ExternalIPv6

func ExternalIPv6() string

ExternalIPv6 获取外网地址 (IPv6)

func InterfaceAddrs added in v0.7.2

func InterfaceAddrs(v ...string) (map[string][]net.IP, error)

InterfaceAddrs 获取所有带 IP 的接口和对应的所有 IP 排除本地链路地址和环回地址

func InternalIP

func InternalIP(dstAddr, network string) string

InternalIP 获取内网地址 (出口本地地址)

func InternalIPv4

func InternalIPv4() string

InternalIPv4 获取内网地址 (IPv4)

func InternalIPv6

func InternalIPv6() string

InternalIPv6 获取内网地址 (临时 IPv6 地址)

func LocalIP

func LocalIP() string

LocalIP 获取本地地址 (第一个)

func LocalIPv4s added in v0.1.3

func LocalIPv4s() (ips []string)

LocalIPv4s 获取所有本地地址 IPv4

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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