diff --git a/go.mod b/go.mod index eeab137..930f4bb 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,6 @@ module git.zhangdeman.cn/zhangdeman/httpclient go 1.20 -require github.com/ddliu/go-httpclient v0.7.1 +require ( + github.com/ddliu/go-httpclient v0.7.1 +) diff --git a/curl/define.go b/httpclient/define.go similarity index 98% rename from curl/define.go rename to httpclient/define.go index e87b130..42a5d87 100644 --- a/curl/define.go +++ b/httpclient/define.go @@ -1,4 +1,4 @@ -// Package curl 基础请求库 +// Package httpclient 基础请求库 // // Author: go_developer@163.com<白茶清欢> // @@ -9,7 +9,7 @@ // Version: 1.0.0 // // Date: 2022/05/01 19:56:48 -package curl +package httpclient import ( "github.com/ddliu/go-httpclient" diff --git a/curl/exception.go b/httpclient/exception.go similarity index 98% rename from curl/exception.go rename to httpclient/exception.go index b71d4b6..806cde7 100644 --- a/curl/exception.go +++ b/httpclient/exception.go @@ -1,4 +1,4 @@ -// Package curl ... +// Package httpclient ... // // Author: go_developer@163.com<白茶清欢> // @@ -9,7 +9,7 @@ // Version: 1.0.0 // // Date: 2022/05/01 20:40:08 -package curl +package httpclient import "strings" diff --git a/curl/request.go b/httpclient/request.go similarity index 99% rename from curl/request.go rename to httpclient/request.go index c3dac62..ac2a78f 100644 --- a/curl/request.go +++ b/httpclient/request.go @@ -1,4 +1,4 @@ -// Package curl ... +// Package httpclient ... // // Author: go_developer@163.com<白茶清欢> // @@ -9,7 +9,7 @@ // Version: 1.0.0 // // Date: 2022/05/01 21:25:03 -package curl +package httpclient import ( "bytes" diff --git a/util/browser.go b/util/browser.go new file mode 100644 index 0000000..30c22c8 --- /dev/null +++ b/util/browser.go @@ -0,0 +1,33 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-04-09 19:02 +package util + +import ( + "fmt" + "os/exec" + "runtime" +) + +type browser struct { + commandTable map[string]string +} + +// Open 使用系统默认浏览器打开链接 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:05 2023/4/9 +func (b *browser) Open(link string) error { + run, ok := b.commandTable[runtime.GOOS] + if !ok { + return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS) + } + + cmd := exec.Command(run, link) + return cmd.Start() +} diff --git a/util/init.go b/util/init.go new file mode 100644 index 0000000..9c03c68 --- /dev/null +++ b/util/init.go @@ -0,0 +1,34 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-08-10 14:34 +package util + +var ( + // UserAgent ... + UserAgent *userAgent + // Browser ... + Browser *browser + // IP ... + IP *ip + // URL ... + URL *ownURL +) + +func init() { + UserAgent = &userAgent{ + list: defaultUserAgentList, + } + Browser = &browser{ + commandTable: map[string]string{ + "windows": "start", + "darwin": "open", + "linux": "xdg-open", + }, + } + IP = &ip{} + URL = &ownURL{} +} diff --git a/util/ip.go b/util/ip.go new file mode 100644 index 0000000..4864b06 --- /dev/null +++ b/util/ip.go @@ -0,0 +1,59 @@ +// Package util... +// +// Description : util... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-03-09 5:56 下午 +package util + +import ( + "net" + "net/http" +) + +// ip ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:47 2022/5/14 +type ip struct { +} + +// GetHostIP 获取本机IP地址 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:58 下午 2021/3/9 +func (i *ip) GetHostIP() string { + hostIP := "127.0.0.1" + addrs, _ := net.InterfaceAddrs() + for _, address := range addrs { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + hostIP = ipnet.IP.String() + break + } + } + } + return hostIP +} + +// GetRemoteIP 获取远端IP +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:35 下午 2021/11/22 +func (i *ip) GetRemoteIP(req *http.Request) string { + // Try via request + remoteIP, _, err := net.SplitHostPort(req.RemoteAddr) + + if err != nil { + return "::1" + } + userIP := net.ParseIP(remoteIP) + if userIP == nil { + return "::1" + } + return userIP.String() +} diff --git a/util/url.go b/util/url.go new file mode 100644 index 0000000..b560e60 --- /dev/null +++ b/util/url.go @@ -0,0 +1,97 @@ +// Package util... +// +// Description : util... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-11-04 2:38 下午 +package util + +import ( + "net/url" + "strings" +) + +// URLParseResult url解析 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2:51 下午 2021/11/4 +type URLParseResult struct { + Scheme string `json:"scheme"` + Domain string `json:"domain"` + URI string `json:"uri"` + Parameter map[string]string `json:"parameter"` +} + +// ownURL ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:38 2022/5/14 +type ownURL struct { +} + +// Encode ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2:39 下午 2021/11/4 +func (ou *ownURL) Encode(inputURL string) string { + return url.QueryEscape(inputURL) +} + +// Decode ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2:39 下午 2021/11/4 +func (ou *ownURL) Decode(inputURL string) (string, error) { + return url.QueryUnescape(inputURL) +} + +// Parse url解析 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2:39 下午 2021/11/4 +func (ou *ownURL) Parse(inputURL string) (*URLParseResult, error) { + var ( + parseResult *url.URL + err error + ) + if parseResult, err = url.Parse(inputURL); nil != err { + return nil, err + } + detail := &URLParseResult{ + Scheme: parseResult.Scheme, + Domain: parseResult.Host, + URI: parseResult.Path, + Parameter: make(map[string]string), + } + for k, v := range parseResult.Query() { + if len(v) > 1 { + detail.Parameter[k] = "[" + strings.Join(v, ",") + "]" + } else { + detail.Parameter[k] = v[0] + } + } + return detail, nil +} + +// BuildQueryURL 构建GET链接 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2:43 下午 2021/11/4 +func (ou *ownURL) BuildQueryURL(apiURL string, parameter map[string]string) string { + u := url.Values{} + for k, v := range parameter { + u.Set(k, v) + } + apiURL = strings.Trim(apiURL, "?") + if strings.Contains(apiURL, "?") { + return apiURL + "&" + u.Encode() + } + return apiURL + "?" + u.Encode() +} diff --git a/util/user_agent.go b/util/user_agent.go new file mode 100644 index 0000000..4873f89 --- /dev/null +++ b/util/user_agent.go @@ -0,0 +1,176 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-03-31 18:51 +package util + +import ( + "math/rand" + "time" + + "github.com/mssola/user_agent" +) + +var defaultUserAgentList = []string{ + "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6", + "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", + "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)", + "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20", + "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6", + "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", + "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)", + "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", + "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.3 Mobile/14E277 Safari/603.1.30", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1", + "(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1", + "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11", + "(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6", + "(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6", + "(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", + "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1", + "(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5", + "(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", + "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5", + "(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24", + "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", + "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24", + "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", +} + +type userAgent struct { + list []string +} + +// Set 设置ua池 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:55 2023/3/31 +func (ua *userAgent) Set(agentList []string) { + if len(agentList) == 0 { + return + } + ua.list = agentList +} + +// GetRandomUA 随机获取UA +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:56 2023/3/31 +func (ua *userAgent) GetRandomUA() string { + rand.Seed(time.Now().UnixNano()) + idx := rand.Intn(len(ua.list)) + return ua.list[idx] +} + +// Parse 解析UA +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:31 2023/4/20 +func (ua *userAgent) Parse(inputUA string) *UAInfo { + uaInstance := user_agent.New(inputUA) + uaInfo := &UAInfo{ + Mozilla: uaInstance.Mozilla(), + Platform: uaInstance.Platform(), + OS: uaInstance.OS(), + Localization: uaInstance.Localization(), + Model: uaInstance.Model(), + Browser: &BrowserInfo{ + Engine: "", + EngineVersion: "", + Name: "", + Version: "", + }, + OSInfo: &OSInfo{ + FullName: uaInstance.OSInfo().FullName, + Name: uaInstance.OSInfo().Name, + Version: uaInstance.OSInfo().Version, + }, + Bot: false, + Mobile: false, + Undecided: false, + } + uaInfo.Browser.Engine, uaInfo.Browser.EngineVersion = uaInstance.Engine() + uaInfo.Browser.Name, uaInfo.Browser.Version = uaInstance.Browser() + return uaInfo +} + +// UAInfo ua解析结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type UAInfo struct { + Mozilla string `json:"mozilla"` + Platform string `json:"platform"` + OS string `json:"os"` + OSInfo *OSInfo `json:"os_info"` + Localization string `json:"localization"` + Model string `json:"model"` + Browser *BrowserInfo `json:"browser"` + Bot bool `json:"bot"` + Mobile bool `json:"mobile"` + Undecided bool `json:"undecided"` +} + +// BrowserInfo 浏览器信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type BrowserInfo struct { + Engine string `json:"engine"` // 浏览器引擎 + EngineVersion string `json:"engine_version"` // 浏览器引擎版本 + Name string `json:"name"` // 浏览器名称 + Version string `json:"version"` // 浏览器版本 +} + +// OSInfo 系统信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type OSInfo struct { + FullName string `json:"full_name"` // 操作系统全称 + Name string `json:"name"` // 操作系统名称 + Version string `json:"version"` // 操作系统版本 +}