update
This commit is contained in:
1
go.mod
1
go.mod
@@ -21,6 +21,7 @@ require (
|
|||||||
github.com/knadh/koanf/providers/file v1.1.2
|
github.com/knadh/koanf/providers/file v1.1.2
|
||||||
github.com/knadh/koanf/providers/posflag v0.1.0
|
github.com/knadh/koanf/providers/posflag v0.1.0
|
||||||
github.com/knadh/koanf/v2 v2.1.2
|
github.com/knadh/koanf/v2 v2.1.2
|
||||||
|
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20260226062615-1a8d01d9679e
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/prometheus/client_golang v1.11.1
|
github.com/prometheus/client_golang v1.11.1
|
||||||
github.com/redis/go-redis/v9 v9.7.3
|
github.com/redis/go-redis/v9 v9.7.3
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -207,6 +207,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
|
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20260226062615-1a8d01d9679e h1:1+rVed4OnYgQCg934wWHms6J7aEbyQ7TaR/fSYhJmLA=
|
||||||
|
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20260226062615-1a8d01d9679e/go.mod h1:+mNMTBuDMdEGhWzoQgc6kBdqeaQpWh5ba8zqmp2MxCU=
|
||||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ const (
|
|||||||
PcUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
|
PcUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
|
||||||
AcceptHtml = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
|
AcceptHtml = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
|
||||||
AcceptCNLanguage = "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
|
AcceptCNLanguage = "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
|
||||||
|
AcceptEncodingIdentity = "identity"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RedirectAllCookies(req *http.Request, via []*http.Request) error {
|
func RedirectAllCookies(req *http.Request, via []*http.Request) error {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -19,24 +20,34 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
defaultClient *HttpClient
|
defaultClient *HttpClient
|
||||||
|
defaultNoRedirectClient *HttpClient
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
defaultClient = New()
|
defaultClient = New()
|
||||||
|
defaultNoRedirectClient = New(WithNoRedirect())
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReInitDefault(timeout time.Duration) {
|
func ReInitDefault(timeout time.Duration) {
|
||||||
defaultClient = New(WithTimout(timeout))
|
defaultClient = New(WithTimout(timeout))
|
||||||
|
defaultNoRedirectClient = New(WithNoRedirect(), WithTimout(timeout))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReInitDefaultOpt(opts ...ConfigOpt) {
|
func ReInitDefaultOpt(opts ...ConfigOpt) {
|
||||||
defaultClient = New(opts...)
|
defaultClient = New(opts...)
|
||||||
|
defaultNoRedirectClient = New(slices.Concat([]ConfigOpt{WithNoRedirect()}, opts)...)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Client() *HttpClient {
|
func Client() *HttpClient {
|
||||||
return defaultClient
|
return defaultClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NoRedirectClient() *HttpClient {
|
||||||
|
return defaultNoRedirectClient
|
||||||
|
}
|
||||||
|
|
||||||
func NewTransport(maxConn int, idleTimeout time.Duration) *http.Transport {
|
func NewTransport(maxConn int, idleTimeout time.Duration) *http.Transport {
|
||||||
if maxConn <= 0 {
|
if maxConn <= 0 {
|
||||||
panic("max connection <= 0")
|
panic("max connection <= 0")
|
||||||
|
|||||||
73
myip/ip2region.go
Normal file
73
myip/ip2region.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package myip
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
searcher *xdb.Searcher
|
||||||
|
|
||||||
|
//go:embed ip2region.xdb
|
||||||
|
dbData []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
|
||||||
|
searcher, err = xdb.NewWithBuffer(xdb.IPv4, dbData)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to create searcher with content: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
type IpInfo struct {
|
||||||
|
Country string `json:"country"`
|
||||||
|
Area string `json:"area"`
|
||||||
|
Province string `json:"province"`
|
||||||
|
City string `json:"city"`
|
||||||
|
Isp string `json:"isp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindE(ipStr string) (info *IpInfo, err error) {
|
||||||
|
info = &IpInfo{}
|
||||||
|
// 国家|区域|省份|城市|ISP
|
||||||
|
res, err := searcher.SearchByStr(ipStr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//fmt.Println(res)
|
||||||
|
|
||||||
|
sp := strings.Split(res, "|")
|
||||||
|
if len(sp) < 5 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
info.Country = convertEmpty(sp[0])
|
||||||
|
info.Area = convertEmpty(sp[1])
|
||||||
|
info.Province = convertEmpty(sp[2])
|
||||||
|
info.City = convertEmpty(sp[3])
|
||||||
|
info.Isp = convertEmpty(sp[4])
|
||||||
|
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Find(ipStr string) *IpInfo {
|
||||||
|
info, _ := FindE(ipStr)
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertEmpty(s string) string {
|
||||||
|
if s == "0" || s == "" {
|
||||||
|
return "NULL"
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IpInfo) ToString() string {
|
||||||
|
return i.Country + "-" + i.Province + "-" + i.City + "-" + i.Isp
|
||||||
|
}
|
||||||
BIN
myip/ip2region.xdb
Normal file
BIN
myip/ip2region.xdb
Normal file
Binary file not shown.
Reference in New Issue
Block a user