2018-11-21 10:17:05 +00:00
|
|
|
package curl2info
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2018-11-22 17:33:33 +00:00
|
|
|
"474420502.top/eson/requests"
|
2018-11-21 10:17:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CURL 信息结构
|
|
|
|
type CURL struct {
|
|
|
|
ParsedURL *url.URL
|
|
|
|
Method string
|
|
|
|
Header http.Header
|
|
|
|
CookieJar http.CookieJar
|
|
|
|
Cookies []*http.Cookie
|
|
|
|
Body *requests.Body
|
2018-11-22 17:33:33 +00:00
|
|
|
Auth *requests.BasicAuth
|
2018-11-27 10:49:52 +00:00
|
|
|
Timeout int // second
|
2018-11-22 17:33:33 +00:00
|
|
|
Insecure bool
|
2018-11-27 10:49:52 +00:00
|
|
|
|
2018-12-06 06:31:23 +00:00
|
|
|
ITask string
|
|
|
|
Crontab string
|
2018-12-07 10:20:00 +00:00
|
|
|
Name string
|
2018-11-21 10:17:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 09:03:17 +00:00
|
|
|
// New new 一个 curl 出来
|
|
|
|
func New() *CURL {
|
2018-11-21 10:17:05 +00:00
|
|
|
|
|
|
|
u := &CURL{}
|
2018-11-22 17:33:33 +00:00
|
|
|
u.Insecure = false
|
|
|
|
|
2018-11-21 10:17:05 +00:00
|
|
|
u.Header = make(http.Header)
|
|
|
|
u.CookieJar, _ = cookiejar.New(nil)
|
|
|
|
u.Body = requests.NewBody()
|
2018-11-27 10:49:52 +00:00
|
|
|
u.Timeout = 30
|
2018-11-21 10:17:05 +00:00
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func (curl *CURL) String() string {
|
2018-11-23 07:30:52 +00:00
|
|
|
if curl != nil {
|
|
|
|
return fmt.Sprintf("Method: %s\nParsedURL: %s\nHeader: %s\nCookie: %s",
|
|
|
|
curl.Method, curl.ParsedURL.String(), curl.Header, curl.Cookies)
|
|
|
|
}
|
|
|
|
return ""
|
2018-11-21 10:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSession 创建Session
|
|
|
|
func (curl *CURL) CreateSession() *requests.Session {
|
|
|
|
ses := requests.NewSession()
|
|
|
|
ses.SetHeader(curl.Header)
|
|
|
|
ses.SetCookies(curl.ParsedURL, curl.Cookies)
|
2018-11-27 10:49:52 +00:00
|
|
|
ses.SetConfig(requests.CRequestTimeout, curl.Timeout)
|
2018-11-22 17:33:33 +00:00
|
|
|
|
|
|
|
if curl.Auth != nil {
|
|
|
|
ses.SetConfig(requests.CBasicAuth, curl.Auth)
|
|
|
|
}
|
|
|
|
|
|
|
|
if curl.Insecure {
|
|
|
|
ses.SetConfig(requests.CInsecure, curl.Insecure)
|
|
|
|
}
|
|
|
|
|
2018-11-21 10:17:05 +00:00
|
|
|
return ses
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateWorkflow 根据Session 创建Workflow
|
|
|
|
func (curl *CURL) CreateWorkflow(ses *requests.Session) *requests.Workflow {
|
|
|
|
var wf *requests.Workflow
|
2018-11-22 17:33:33 +00:00
|
|
|
|
2018-11-27 06:05:24 +00:00
|
|
|
if ses == nil {
|
|
|
|
ses = curl.CreateSession()
|
|
|
|
}
|
|
|
|
|
2018-11-21 10:17:05 +00:00
|
|
|
switch curl.Method {
|
|
|
|
case "HEAD":
|
|
|
|
wf = ses.Head(curl.ParsedURL.String())
|
|
|
|
case "GET":
|
|
|
|
wf = ses.Get(curl.ParsedURL.String())
|
|
|
|
case "POST":
|
|
|
|
wf = ses.Post(curl.ParsedURL.String())
|
|
|
|
case "PUT":
|
|
|
|
wf = ses.Put(curl.ParsedURL.String())
|
|
|
|
case "PATCH":
|
|
|
|
wf = ses.Patch(curl.ParsedURL.String())
|
|
|
|
case "OPTIONS":
|
|
|
|
wf = ses.Options(curl.ParsedURL.String())
|
|
|
|
case "DELETE":
|
|
|
|
wf = ses.Delete(curl.ParsedURL.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
wf.SetBody(curl.Body)
|
|
|
|
return wf
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:19:40 +00:00
|
|
|
// ParseRawCURL curl_bash 可以用trie改进 没空改
|
2018-11-21 10:17:05 +00:00
|
|
|
func ParseRawCURL(scurl string) (cURL *CURL, err error) {
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if _err := recover(); _err != nil {
|
|
|
|
cURL = nil
|
|
|
|
err = _err.(error)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-11-23 07:30:52 +00:00
|
|
|
executor := newPQueueExecute()
|
2018-11-30 09:03:17 +00:00
|
|
|
curl := New()
|
2018-11-23 10:36:32 +00:00
|
|
|
|
|
|
|
if scurl[0] == '"' && scurl[len(scurl)-1] == '"' {
|
|
|
|
scurl = strings.Trim(scurl, `"`)
|
|
|
|
} else if scurl[0] == '\'' && scurl[len(scurl)-1] == '\'' {
|
|
|
|
scurl = strings.Trim(scurl, `'`)
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:19:40 +00:00
|
|
|
scurl = strings.TrimSpace(scurl)
|
2018-11-21 10:17:05 +00:00
|
|
|
scurl = strings.TrimLeft(scurl, "curl")
|
2018-11-29 08:19:40 +00:00
|
|
|
|
2018-12-02 19:18:42 +00:00
|
|
|
mathches := regexp.MustCompile(
|
|
|
|
`--[^ ]+ +'[^']+'|`+
|
|
|
|
`--[^ ]+ +[^ ]+|`+
|
|
|
|
`-[A-Za-z] +'[^']+'|`+
|
|
|
|
`-[A-Za-z] +[^ ]+|`+
|
|
|
|
` '[^']+'|`+
|
|
|
|
`--[a-z]+ {0,}`,
|
|
|
|
).FindAllString(scurl, -1)
|
2018-11-21 10:17:05 +00:00
|
|
|
for _, m := range mathches {
|
|
|
|
m = strings.TrimSpace(m)
|
|
|
|
switch v := m[0]; v {
|
|
|
|
case '\'':
|
|
|
|
purl, err := url.Parse(strings.Trim(m, "'"))
|
2018-11-21 10:20:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-11-21 10:17:05 +00:00
|
|
|
curl.ParsedURL = purl
|
|
|
|
case '-':
|
2018-11-29 08:19:40 +00:00
|
|
|
exec := judgeOptions(curl, m)
|
2018-11-22 17:33:33 +00:00
|
|
|
if exec != nil {
|
|
|
|
executor.Push(exec)
|
|
|
|
}
|
2018-11-21 10:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 17:33:33 +00:00
|
|
|
for executor.Len() > 0 {
|
|
|
|
exec := executor.Pop()
|
|
|
|
exec.Execute()
|
|
|
|
}
|
|
|
|
|
2018-11-21 10:17:05 +00:00
|
|
|
if curl.Method == "" {
|
|
|
|
curl.Method = "GET"
|
|
|
|
}
|
|
|
|
|
|
|
|
return curl, nil
|
|
|
|
}
|