0%

GoLang常用代码片段

QQ群:397745473

判断是否包含某些字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
"fmt"
"strings"
)

func main() {
fmt.Println(strings.Contains("seafood", "foo"))
fmt.Println(strings.Contains("seafood", "bar"))
fmt.Println(strings.Contains("seafood", ""))
fmt.Println(strings.Contains("", ""))
}

https://golang.org/pkg/strings/#Contains

写文件

1
2
3
4
5
6
7
8
9
10
11
12
13
func writeDate(writeContent string) {
resultFile := time.Now().Format("../result_20060102.txt")
outFd, err := os.OpenFile(resultFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
outFd, err = os.Create(resultFile)
//return
}
defer outFd.Close()
outWriter := bufio.NewWriter(outFd)
_, _ = outWriter.WriteString(writeContent + "\n")
_ = outWriter.Flush()

}

字符串分片

1
domain1 = strings.Split(domain, "//")[1]

http head请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func Head(url string) (resp *http.Response, err error) {
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // disable verify
}

Client := &http.Client{
Timeout: 10 * time.Second,
Transport: transCfg,
}
req, err := http.NewRequest("HEAD", url, nil)

if err != nil {
return nil, err
}
req.Header.Add("User-Agent", fake.UserAgent())
resp, err2 := Client.Do(req)
return resp, err2

}

http GET 请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func Get(url string) (content []byte, header http.Header, err error) {
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // disable verify
}

Client := &http.Client{
Timeout: 10 * time.Second,
Transport: transCfg,
}
req, err := http.NewRequest("GET", url, nil)

if err != nil {
return nil, nil, err
}
req.Header.Add("User-Agent", fake.UserAgent())
resp, err2 := Client.Do(req)
if err2 != nil {
return nil, nil, err
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)

return bytes, resp.Header, nil
}

代理请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
func genIpaddr() string {
rand.Seed(time.Now().Unix())
ip := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
return ip
}

// func getUrlResponse(Referer, requestURL string) string {
func getUrlResponse(url string) (content []byte, header http.Header, err error) {
//fmt.Println("requestURL:", requestURL)
//fmt.Println("Referer:", Referer)
randIp := genIpaddr()
SocksProxy, _ := cfg.GetValue("scan", "SocksProxy") //SocksProxy
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(SocksProxy)
}

httpTransport := &http.Transport{
//解决x509: certificate signed by unknown authority
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}

httpClient := &http.Client{
Timeout: time.Second * 15, //超时时间
Transport: httpTransport, //解决x509: certificate signed by unknown authority
}

// req, err := http.NewRequest("GET", requestURL, nil)
req, err := http.NewRequest("GET", url, nil)

if err != nil {
return nil, nil, err
}

req.Header.Set("Connection", "keep-alive")
req.Header.Set("User-Agent", fake.UserAgent())
req.Header.Set("X-Forward-For", randIp)
req.Header.Set("HTTP_X_FORWARD_FOR", randIp)
req.Header.Set("HTTP_CLIENT_IP", randIp)
req.Header.Set("X-Real-IP", randIp)
req.Header.Set("REMOTE_ADDR", randIp)
req.Header.Set("Referer", Referer)

resp, err2 := Client.Do(req)
if err2 != nil {
return nil, nil, err
}
defer resp.Body.Close()

bytes, _ := ioutil.ReadAll(resp.Body)
return bytes, resp.Header, nil
}

异常处理

1
2
3
4
5
6
defer func() {
if err := recover();err != nil {
//fmt.Println("panic recover",err)
}
}()

QQ群:397745473

欢迎关注我的其它发布渠道