4 Commits

Author SHA1 Message Date
b612 b92288bbc9 update go mod 2023-02-03 13:18:53 +08:00
b612 0805549006 add origin request/response http method 2022-09-06 15:15:33 +08:00
b612 033272f38a add tls config 2022-08-22 16:22:22 +08:00
b612 93b756d9fb add no auto redirect config 2022-06-06 11:18:42 +08:00
3 changed files with 56 additions and 9 deletions
+47 -6
View File
@@ -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
}
+7 -1
View File
@@ -2,4 +2,10 @@ module b612.me/starnet
go 1.16
require b612.me/stario v0.0.5
require b612.me/stario v0.0.7
require (
golang.org/x/crypto v0.0.0-20220313003712-b769efc7c000 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
)
+2 -2
View File
@@ -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.7 h1:QbQcsHCVLE6vRgVrPN4+9DGiSaC6IWdtm4ClL2tpMUg=
b612.me/stario v0.0.7/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=