diff --git a/go.mod b/go.mod index 6fedfc2..4f698d6 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/knadh/koanf/providers/file v1.1.2 github.com/knadh/koanf/providers/posflag v0.1.0 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/prometheus/client_golang v1.11.1 github.com/redis/go-redis/v9 v9.7.3 diff --git a/go.sum b/go.sum index 9b4fa85..7018243 100644 --- a/go.sum +++ b/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/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/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/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= diff --git a/myhttp/httpc/const.go b/myhttp/httpc/const.go index 49ece2f..493141b 100644 --- a/myhttp/httpc/const.go +++ b/myhttp/httpc/const.go @@ -18,10 +18,11 @@ const ( ContentTypeJSON = "application/json; charset=utf-8" ) const ( - MobileUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/124.0.6367.68 MobileRequest/15E148 Safari/604.1" - 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" - AcceptCNLanguage = "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7" + MobileUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/124.0.6367.68 MobileRequest/15E148 Safari/604.1" + 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" + 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 { diff --git a/myhttp/httpc/httpclient.go b/myhttp/httpc/httpclient.go index 4998422..05b66a1 100644 --- a/myhttp/httpc/httpclient.go +++ b/myhttp/httpc/httpclient.go @@ -9,6 +9,7 @@ import ( "net/http" "net/http/cookiejar" "net/url" + "slices" "strings" "time" @@ -18,25 +19,35 @@ import ( ) var ( - defaultClient *HttpClient + defaultClient *HttpClient + defaultNoRedirectClient *HttpClient ) func init() { defaultClient = New() + defaultNoRedirectClient = New(WithNoRedirect()) } func ReInitDefault(timeout time.Duration) { defaultClient = New(WithTimout(timeout)) + defaultNoRedirectClient = New(WithNoRedirect(), WithTimout(timeout)) + } func ReInitDefaultOpt(opts ...ConfigOpt) { defaultClient = New(opts...) + defaultNoRedirectClient = New(slices.Concat([]ConfigOpt{WithNoRedirect()}, opts)...) + } func Client() *HttpClient { return defaultClient } +func NoRedirectClient() *HttpClient { + return defaultNoRedirectClient +} + func NewTransport(maxConn int, idleTimeout time.Duration) *http.Transport { if maxConn <= 0 { panic("max connection <= 0") diff --git a/myip/ip2region.go b/myip/ip2region.go new file mode 100644 index 0000000..8ebe6c4 --- /dev/null +++ b/myip/ip2region.go @@ -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 +} diff --git a/myip/ip2region.xdb b/myip/ip2region.xdb new file mode 100644 index 0000000..7052c05 Binary files /dev/null and b/myip/ip2region.xdb differ