api

package module
v0.0.0-...-cbd539e Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2022 License: MIT Imports: 9 Imported by: 0

README

README

获取公网 IP

package main

import (
	publicIp "gitee.com/zhxlp/golang-study/public-ip"
	log "github.com/sirupsen/logrus"
)

func main() {
	ip, err := publicIp.GetPublicIP("V6")
	if err != nil {
		log.Error(err)
	} else {
		log.Info(ip)
	}
	ip, err = publicIp.GetPublicIP("V4")
	if err != nil {
		log.Error(err)
	} else {
		log.Info(ip)
	}

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HttpTransport http.RoundTripper = &http.Transport{
	Proxy:                 nil,
	ForceAttemptHTTP2:     true,
	MaxIdleConns:          100,
	IdleConnTimeout:       90 * time.Second,
	TLSHandshakeTimeout:   10 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
	DialContext: (&net.Dialer{
		Timeout:   30 * time.Second,
		KeepAlive: 30 * time.Second,
		Resolver: &net.Resolver{
			PreferGo: true,
			Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
				dnsResolverAddress := validDnsResolverAddress(address)
				d := net.Dialer{
					Timeout: dnsResolverTimeout,
				}
				return d.DialContext(ctx, network, dnsResolverAddress)
			},
		},
	}).DialContext,
}

自定义的Transport,内容继承 DefaultTransport,进行如下修改 1. 禁止使用代理 2. 使用指定的DNS地址进行解析(需要使用go1.19版本以上才行,应为在1.19版本修复了windows 无法使用自定义dns的问题)

View Source
var IPV4_API = []string{"http://ipv4.ddnspod.com", "http://ipv4.testipv6.cn/ip/", "http://ipv4.icanhazip.com/", "https://api4.ipify.org/", "https://ip4.seeip.org/", "http://api-ipv4.ip.sb/ip", "http://v4.ipv6-test.com/api/myip.php"}
View Source
var IPV6_API = []string{"http://ipv6.ddnspod.com", "http://ipv6.testipv6.cn/ip/", "http://ipv6.icanhazip.com/", "https://api6.ipify.org/", "https://ip6.seeip.org/", "http://api-ipv6.ip.sb/ip", "http://v6.ipv6-test.com/api/myip.php"}

Functions

func GetPublicIP

func GetPublicIP(ipType string) (string, error)

获取当前上网的公网ip地址 @param ipType: V4 或 V6

func MatchIPv4

func MatchIPv4(s string) (string, error)

查找字符串中第一个IPV4地址

func MatchIPv6

func MatchIPv6(s string) (string, error)

查找字符串中第一个IPV6地址

Types

This section is empty.

Jump to

Keyboard shortcuts

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