1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 21:29:02 +08:00
nali/pkg/common/httpclient.go

60 lines
1.2 KiB
Go
Raw Normal View History

package common
import (
2023-08-24 11:10:52 +08:00
"io"
"log"
"net/http"
"time"
)
const UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
type HttpClient struct {
*http.Client
}
var httpClient *HttpClient
func init() {
httpClient = &HttpClient{http.DefaultClient}
httpClient.Timeout = time.Second * 60
httpClient.Transport = &http.Transport{
TLSHandshakeTimeout: time.Second * 5,
IdleConnTimeout: time.Second * 10,
ResponseHeaderTimeout: time.Second * 10,
ExpectContinueTimeout: time.Second * 20,
2022-07-29 05:54:00 +08:00
Proxy: http.ProxyFromEnvironment,
}
}
func GetHttpClient() *HttpClient {
c := *httpClient
return &c
}
func (c *HttpClient) Get(urls ...string) (body []byte, err error) {
var req *http.Request
var resp *http.Response
for _, url := range urls {
req, err = http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Println(err)
continue
}
req.Header.Set("User-Agent", UserAgent)
resp, err = c.Do(req)
if err == nil && resp != nil && resp.StatusCode == 200 {
2023-08-24 11:10:52 +08:00
body, err = io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
continue
}
return
}
}
return nil, err
}