Skip to content

Commit

Permalink
Merge pull request #46 from xingcxb/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
xingcxb authored Nov 16, 2024
2 parents d5eae5a + 547a47f commit a1bd9ac
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 68 deletions.
1 change: 1 addition & 0 deletions _examples/core/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ func TestGetCurrentAbPath(t *testing.T) {
}

func TestUnzip(t *testing.T) {
// 如果要解压到当前目录下可以使用 ./
fmt.Println(fileKit.Unzip("/Users/symbol/Downloads/qts_linux_amd64.zip", "/Users/symbol/Downloads"))
}
55 changes: 25 additions & 30 deletions core/httpKit/httpClientKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpKit
import (
"bufio"
"errors"
"github.com/valyala/fasthttp"
"github.com/xingcxb/goKit/core/strKit"
"io"
"net/http"
Expand Down Expand Up @@ -81,12 +82,13 @@ func HttpDownload(urlString, savePath, fileName string, isCover bool) (string, e
}

// HttpGet 发送get请求
/**
/*
* @param urlString 网址
* @return string 网页内容,error
*/
func HttpGet(urlString string) (string, error) {
return HttpGetFull(urlString, nil, nil, "", -1)
stream, err := HttpBasic(urlString, http.MethodGet, nil, nil, "", -1)
return string(stream), err
}

// HttpGetFull 发送get请求[完整版]
Expand All @@ -99,7 +101,8 @@ func HttpGet(urlString string) (string, error) {
* @return string 网页内容,error
*/
func HttpGetFull(urlString string, headers, paramMap map[string]string, body string, timeout int) (string, error) {
return HttpBasic(urlString, http.MethodGet, headers, paramMap, body, timeout)
stream, err := HttpBasic(urlString, http.MethodGet, headers, paramMap, body, timeout)
return string(stream), err
}

// HttpPost 发送post基础请求
Expand All @@ -109,7 +112,8 @@ func HttpGetFull(urlString string, headers, paramMap map[string]string, body str
* @return string 网页内容,error
*/
func HttpPost(urlString string, params map[string]string) (string, error) {
return HttpBasic(urlString, http.MethodPost, nil, params, "", -1)
stream, err := HttpBasic(urlString, http.MethodPost, nil, params, "", -1)
return string(stream), err
}

// HttpPostFull 发送post请求[完整版]
Expand All @@ -122,7 +126,8 @@ func HttpPost(urlString string, params map[string]string) (string, error) {
* @return string 网页内容,error
*/
func HttpPostFull(urlString string, headers, paramMap map[string]string, body string, timeout int) (string, error) {
return HttpBasic(urlString, http.MethodPost, headers, paramMap, body, timeout)
stream, err := HttpBasic(urlString, http.MethodPost, headers, paramMap, body, timeout)
return string(stream), err
}

// HttpBasic 发送http请求[基础]
Expand All @@ -135,34 +140,24 @@ func HttpPostFull(urlString string, headers, paramMap map[string]string, body st
* @param timeout 超时时长,-1表示默认超时,单位毫秒
* @return string 网页内容,error
*/
func HttpBasic(urlString, httpMethod string, headers, paramMap map[string]string, body string, timeout int) (string, error) {
urlParam := strKit.MapParamsToUrlParams(paramMap)
if urlParam != "" {
urlString = strKit.Splicing(urlString, "?", urlParam)
}
bodyReader := strings.NewReader(body)
req, _ := http.NewRequest(httpMethod, urlString, bodyReader)
if headers == nil {
// 如果headers为空,初始化
headers = make(map[string]string, 0)
func HttpBasic(urlString, httpMethod string, headers, paramMap map[string]string, body string, timeout int) (stream []byte, err error) {
client := &fasthttp.Client{
MaxConnWaitTimeout: time.Duration(timeout) * time.Millisecond,
}
req := &fasthttp.Request{}
queryStr := strKit.MapParamsToUrlParams(paramMap)
req.SetRequestURI(urlString)
req.URI().SetQueryString(queryStr)
for k, v := range headers {
req.Header.Add(k, v)
}
if timeout != -1 {
http.DefaultClient.Timeout = time.Duration(timeout) * time.Millisecond
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
req.Header.Set(k, v)
}
defer res.Body.Close()
respByte, err := io.ReadAll(res.Body)
if err != nil {
return "", err
req.Header.SetMethod(httpMethod)
resp := &fasthttp.Response{}
// 发起请求
if err = client.Do(req, resp); err != nil {
return nil, err
}
result := string(respByte)
return result, nil
return resp.Body(), nil
}

// HttpProxyGet 发送get代理请求
Expand Down Expand Up @@ -248,7 +243,7 @@ func HttpProxyBasic(urlStr, httpMethod string, headers, paramMap map[string]stri
if proxyIpPort == "" {
// 如果不需要代理,直接调用httpBasic
result, err := HttpBasic(urlStr, httpMethod, headers, paramMap, body, timeout)
return headers, result, err
return headers, string(result), err
}
urlParam := strKit.MapParamsToUrlParams(paramMap)
if urlParam != "" {
Expand Down
22 changes: 22 additions & 0 deletions core/ipKit/ipKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ipKit
import (
"encoding/json"
"github.com/xingcxb/goKit/core/ipKit/ipService"
"net"
)

// GetIpAddressInfo 获取ip物理地址信息
Expand All @@ -21,3 +22,24 @@ func GetIpAddressInfo(ip string) (string, error) {
}
return string(ipInfoJson), nil
}

// IsPrivateIP 判断是否为局域网 IP
/*
* @param ip 需要判断的ip地址
* @return bool true 内网 false 局域网
*/
func IsPrivateIP(ip net.IP) bool {
privateIPBlocks := []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
}

for _, cidr := range privateIPBlocks {
_, subnet, _ := net.ParseCIDR(cidr)
if subnet.Contains(ip) {
return true
}
}
return false
}
4 changes: 2 additions & 2 deletions core/osKit/osKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package osKit
import (
"encoding/json"
"fmt"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/host"
"github.com/xingcxb/goKit/core/dateKit"
"net"
)
Expand Down
8 changes: 4 additions & 4 deletions core/osKit/os_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"errors"
"fmt"
"github.com/dkorunic/iSMC/smc"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/mem"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion core/regKit/regexKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func FindAll(regex, content string) []string {
if err != nil {
return []string{}
}
return compile.FindAllString(content, -1)
return compile.FindStringSubmatch(content)
}

// Index 返回第一个匹配的字符串的起始位置,如果没有匹配则返回-1(下标从0开始)
Expand Down
16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@ require (
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pdfcpu/pdfcpu v0.8.0
github.com/redis/go-redis/v9 v9.6.0
github.com/shirou/gopsutil/v3 v3.24.3
github.com/shirou/gopsutil/v4 v4.24.10
github.com/tidwall/gjson v1.17.1
github.com/valyala/fasthttp v1.57.0
github.com/xuri/excelize/v2 v2.8.1
github.com/yeqown/go-qrcode/v2 v2.2.3
github.com/yeqown/go-qrcode/writer/standard v1.2.3
go.uber.org/zap v1.27.0
golang.org/x/net v0.27.0
golang.org/x/net v0.30.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/andybalholm/brotli v1.1.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/ebitengine/purego v0.8.1 // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/hhrutter/lzw v1.0.0 // indirect
github.com/hhrutter/tiff v1.0.1 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
Expand All @@ -42,20 +46,20 @@ require (
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 // indirect
github.com/yeqown/reedsolomon v1.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/image v0.15.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit a1bd9ac

Please sign in to comment.