1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 21:29:02 +08:00
nali/pkg/common/httpclient.go
zu1k 2f1d16408c fix: common download save to file
Signed-off-by: zu1k <i@zu1k.com>
2022-07-04 22:01:03 +08:00

59 lines
1.1 KiB
Go

package common
import (
"io/ioutil"
"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,
}
}
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 {
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
continue
}
return
}
}
return nil, err
}