|
|
|
@@ -32,6 +32,7 @@ type RequestFile struct {
|
|
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
|
Url string
|
|
|
|
|
RespURL string
|
|
|
|
|
Method string
|
|
|
|
|
RecvData []byte
|
|
|
|
|
RecvContentLength int64
|
|
|
|
@@ -39,8 +40,11 @@ type Request struct {
|
|
|
|
|
RespHeader http.Header
|
|
|
|
|
RespCookies []*http.Cookie
|
|
|
|
|
RespHttpCode int
|
|
|
|
|
Location *url.URL
|
|
|
|
|
CircleBuffer *stario.StarBuffer
|
|
|
|
|
respReader io.ReadCloser
|
|
|
|
|
respOrigin *http.Response
|
|
|
|
|
reqOrigin *http.Request
|
|
|
|
|
RequestOpts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -57,6 +61,8 @@ type RequestOpts struct {
|
|
|
|
|
SkipTLSVerify bool
|
|
|
|
|
CustomTransport *http.Transport
|
|
|
|
|
Queries map[string]string
|
|
|
|
|
DisableRedirect bool
|
|
|
|
|
TlsConfig *tls.Config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RequestOpt func(opt *RequestOpts)
|
|
|
|
@@ -79,6 +85,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 {
|
|
|
|
@@ -167,6 +179,12 @@ func WithSkipTLSVerify(skip bool) RequestOpt {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithDisableRedirect(disable bool) RequestOpt {
|
|
|
|
|
return func(opt *RequestOpts) {
|
|
|
|
|
opt.DisableRedirect = disable
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRequests(url string, rawdata []byte, method string, opts ...RequestOpt) Request {
|
|
|
|
|
req := Request{
|
|
|
|
|
RequestOpts: RequestOpts{
|
|
|
|
@@ -197,6 +215,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 {
|
|
|
|
@@ -288,10 +309,16 @@ 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
|
|
|
|
|
curl.RespCookies = resp.Cookies()
|
|
|
|
@@ -348,11 +375,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)
|
|
|
|
@@ -369,7 +396,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 {
|
|
|
|
@@ -380,15 +407,21 @@ 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)
|
|
|
|
|
}
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
Transport: curl.CustomTransport,
|
|
|
|
|
}
|
|
|
|
|
if curl.DisableRedirect {
|
|
|
|
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
|
|
return req, resp, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UrlEncodeRaw(str string) string {
|
|
|
|
@@ -419,3 +452,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
|
|
|
|
|
}
|
|
|
|
|