Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 319518d71d | |||
| be3df9703e | |||
| b92288bbc9 | |||
| 0805549006 | |||
| 033272f38a |
@@ -22,6 +22,7 @@ const (
|
||||
HEADER_FORM_URLENCODE = `application/x-www-form-urlencoded`
|
||||
HEADER_FORM_DATA = `multipart/form-data`
|
||||
HEADER_JSON = `application/json`
|
||||
HEADER_PLAIN = `text/plain`
|
||||
)
|
||||
|
||||
type RequestFile struct {
|
||||
@@ -32,6 +33,7 @@ type RequestFile struct {
|
||||
|
||||
type Request struct {
|
||||
Url string
|
||||
RespURL string
|
||||
Method string
|
||||
RecvData []byte
|
||||
RecvContentLength int64
|
||||
@@ -42,6 +44,8 @@ type Request struct {
|
||||
Location *url.URL
|
||||
CircleBuffer *stario.StarBuffer
|
||||
respReader io.ReadCloser
|
||||
respOrigin *http.Response
|
||||
reqOrigin *http.Request
|
||||
RequestOpts
|
||||
}
|
||||
|
||||
@@ -59,6 +63,7 @@ type RequestOpts struct {
|
||||
CustomTransport *http.Transport
|
||||
Queries map[string]string
|
||||
DisableRedirect bool
|
||||
TlsConfig *tls.Config
|
||||
}
|
||||
|
||||
type RequestOpt func(opt *RequestOpts)
|
||||
@@ -81,6 +86,12 @@ func WithHeader(key, val string) RequestOpt {
|
||||
}
|
||||
}
|
||||
|
||||
func WithTlsConfig(tlscfg *tls.Config) RequestOpt {
|
||||
return func(opt *RequestOpts) {
|
||||
opt.TlsConfig = tlscfg
|
||||
}
|
||||
}
|
||||
|
||||
func WithHeaderMap(header map[string]string) RequestOpt {
|
||||
return func(opt *RequestOpts) {
|
||||
for key, val := range header {
|
||||
@@ -205,6 +216,9 @@ func NewRequests(url string, rawdata []byte, method string, opts ...RequestOpt)
|
||||
}
|
||||
req.CustomTransport.TLSClientConfig.InsecureSkipVerify = true
|
||||
}
|
||||
if req.TlsConfig != nil {
|
||||
req.CustomTransport.TLSClientConfig = req.TlsConfig
|
||||
}
|
||||
req.CustomTransport.DialContext = func(ctx context.Context, netw, addr string) (net.Conn, error) {
|
||||
c, err := net.DialTimeout(netw, addr, req.DialTimeout)
|
||||
if err != nil {
|
||||
@@ -296,10 +310,15 @@ func Curl(curl Request) (resps Request, err error) {
|
||||
curl.CircleBuffer = fpdst
|
||||
curl.ReqHeader.Set("Content-Type", "multipart/form-data;boundary="+boundary)
|
||||
}
|
||||
resp, err := netcurl(curl)
|
||||
req, resp, err := netcurl(curl)
|
||||
if err != nil {
|
||||
return Request{}, err
|
||||
}
|
||||
if resp.Request != nil && resp.Request.URL != nil {
|
||||
curl.RespURL = resp.Request.URL.String()
|
||||
}
|
||||
curl.reqOrigin = req
|
||||
curl.respOrigin = resp
|
||||
curl.Location, _ = resp.Location()
|
||||
curl.RespHttpCode = resp.StatusCode
|
||||
curl.RespHeader = resp.Header
|
||||
@@ -357,11 +376,11 @@ func (curl *Request) RespBodyReader() io.ReadCloser {
|
||||
return curl.respReader
|
||||
}
|
||||
|
||||
func netcurl(curl Request) (*http.Response, error) {
|
||||
func netcurl(curl Request) (*http.Request, *http.Response, error) {
|
||||
var req *http.Request
|
||||
var err error
|
||||
if curl.Method == "" {
|
||||
return nil, errors.New("Error Method Not Entered")
|
||||
return nil, nil, errors.New("Error Method Not Entered")
|
||||
}
|
||||
if curl.PostBuffer != nil {
|
||||
req, err = http.NewRequest(curl.Method, curl.Url, curl.PostBuffer)
|
||||
@@ -378,7 +397,7 @@ func netcurl(curl Request) (*http.Response, error) {
|
||||
req.URL.RawQuery = sid.Encode()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
req.Header = curl.ReqHeader
|
||||
if len(curl.ReqCookies) != 0 {
|
||||
@@ -389,7 +408,7 @@ func netcurl(curl Request) (*http.Response, error) {
|
||||
if curl.Proxy != "" {
|
||||
purl, err := url.Parse(curl.Proxy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
curl.CustomTransport.Proxy = http.ProxyURL(purl)
|
||||
}
|
||||
@@ -402,7 +421,8 @@ func netcurl(curl Request) (*http.Response, error) {
|
||||
}
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
return resp, err
|
||||
|
||||
return req, resp, err
|
||||
}
|
||||
|
||||
func UrlEncodeRaw(str string) string {
|
||||
@@ -433,3 +453,11 @@ func BuildPostForm(queryMap map[string]string) []byte {
|
||||
}
|
||||
return []byte(query.Encode())
|
||||
}
|
||||
|
||||
func (r Request) Resopnse() *http.Response {
|
||||
return r.respOrigin
|
||||
}
|
||||
|
||||
func (r Request) Request() *http.Request {
|
||||
return r.reqOrigin
|
||||
}
|
||||
|
||||
@@ -2,4 +2,4 @@ module b612.me/starnet
|
||||
|
||||
go 1.16
|
||||
|
||||
require b612.me/stario v0.0.5
|
||||
require b612.me/stario v0.0.8
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
b612.me/stario v0.0.5 h1:Q1OGF+8eOoK49zMzkyh80GWaMuknhey6+PWJJL9ZuNo=
|
||||
b612.me/stario v0.0.5/go.mod h1:or4ssWcxQSjMeu+hRKEgtp0X517b3zdlEOAms8Qscvw=
|
||||
b612.me/stario v0.0.8 h1:kaA4pszAKLZJm2D9JmiuYSpgjTeE3VaO74vm+H0vBGM=
|
||||
b612.me/stario v0.0.8/go.mod h1:or4ssWcxQSjMeu+hRKEgtp0X517b3zdlEOAms8Qscvw=
|
||||
golang.org/x/crypto v0.0.0-20220313003712-b769efc7c000 h1:SL+8VVnkqyshUSz5iNnXtrBQzvFF2SkROm6t5RczFAE=
|
||||
golang.org/x/crypto v0.0.0-20220313003712-b769efc7c000/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
|
||||
@@ -33,6 +33,7 @@ func getICMP(seq uint16) ICMP {
|
||||
|
||||
func sendICMPRequest(icmp ICMP, destAddr *net.IPAddr, timeout time.Duration) (PingResult, error) {
|
||||
var res PingResult
|
||||
res.RemoteIP = destAddr.String()
|
||||
conn, err := net.DialIP("ip:icmp", nil, destAddr)
|
||||
if err != nil {
|
||||
return res, err
|
||||
@@ -84,6 +85,7 @@ func checkSum(data []byte) uint16 {
|
||||
type PingResult struct {
|
||||
Duration time.Duration
|
||||
RecvCount int
|
||||
RemoteIP string
|
||||
}
|
||||
|
||||
func Ping(ip string, seq int, timeout time.Duration) (PingResult, error) {
|
||||
@@ -95,3 +97,14 @@ func Ping(ip string, seq int, timeout time.Duration) (PingResult, error) {
|
||||
icmp := getICMP(uint16(seq))
|
||||
return sendICMPRequest(icmp, ipAddr, timeout)
|
||||
}
|
||||
|
||||
func IsIpPingable(ip string, timeout time.Duration, retryLimit int) bool {
|
||||
for i := 0; i < retryLimit; i++ {
|
||||
_, err := Ping(ip, 29, timeout)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
+5
-1
@@ -7,5 +7,9 @@ import (
|
||||
)
|
||||
|
||||
func Test_Ping(t *testing.T) {
|
||||
fmt.Println(Ping("baidu.com", 0, time.Second*2))
|
||||
fmt.Println(Ping("baidu.com", 29, time.Second*2))
|
||||
fmt.Println(Ping("www.b612.me", 29, time.Second*2))
|
||||
fmt.Println(IsIpPingable("baidu.com", time.Second*2, 3))
|
||||
fmt.Println(IsIpPingable("www.b612.me", time.Second*2, 3))
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user