-
Notifications
You must be signed in to change notification settings - Fork 101
/
curl.go
151 lines (142 loc) · 2.84 KB
/
curl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package m3u8d
import (
"bytes"
"net/http"
"strconv"
"strings"
"unicode"
)
type ParseCurl_Resp struct {
ErrMsg string
DownloadReq StartDownload_Req
}
func ParseCurlStr(s string) (resp ParseCurl_Resp) {
r := strings.NewReader(strings.ReplaceAll(s, "\\\n", ""))
var tmp []string
for {
b, _, err := r.ReadRune()
if err != nil {
break
}
if unicode.IsSpace(b) {
continue
}
if b == '"' || b == '\'' {
str := parseQuotedStr(r, b)
tmp = append(tmp, str)
} else {
var buf bytes.Buffer
buf.WriteRune(b)
for {
b, _, err = r.ReadRune()
if err != nil || unicode.IsSpace(b) {
break
}
buf.WriteRune(b)
}
tmp = append(tmp, buf.String())
}
}
if len(tmp) > 0 && strings.ToLower(tmp[0]) == "curl" {
tmp = tmp[1:]
}
return ParseCurl(tmp)
}
func parseQuotedStr(r *strings.Reader, b rune) string {
var buf bytes.Buffer
var prevHasQ = false
for {
nextB, _, err := r.ReadRune()
if err != nil {
break
}
if nextB == b && prevHasQ == false {
break
}
if nextB == '\\' && b == '"' && prevHasQ == false {
prevHasQ = true
continue
}
prevHasQ = false
buf.WriteRune(nextB)
}
return buf.String()
}
func ParseCurl(cmdList []string) (resp ParseCurl_Resp) {
header := http.Header{}
isHeader := false
isMethod := false
isProxy := false
for idx := 0; idx < len(cmdList); idx++ {
value := cmdList[idx]
if isHeader {
idx1 := strings.Index(value, ": ")
if idx1 >= 0 {
k := value[:idx1]
v := value[idx1+2:]
header.Set(k, v)
}
isHeader = false
continue
}
if isMethod {
if strings.ToUpper(value) != http.MethodGet {
resp.ErrMsg = "不支持的method: " + strconv.Quote(value)
return resp
}
isMethod = false
continue
}
if isProxy {
resp.DownloadReq.SetProxy = value
isProxy = false
continue
}
valueLow := strings.ToLower(value)
switch valueLow {
case "-h":
isHeader = true
case "--compressed":
case "-x":
if value == "-X" {
isMethod = true
} else {
isProxy = true
}
case "-k", "--insecure":
resp.DownloadReq.Insecure = true
default:
if strings.HasPrefix(valueLow, "-") { // 不认识的flag, 跳过
continue
}
if resp.DownloadReq.M3u8Url != "" {
resp.ErrMsg = "重复的url"
return resp
}
resp.DownloadReq.M3u8Url = value
}
}
resp.DownloadReq.HeaderMap = header
return resp
}
func RunDownload_Req_ToCurlStr(req StartDownload_Req) string {
if req.M3u8Url == "" {
return ""
}
var buf bytes.Buffer
buf.WriteString("curl " + strconv.Quote(req.M3u8Url))
if req.Insecure {
buf.WriteString(" \\\n -insecure")
}
if req.SetProxy != "" {
buf.WriteString(" \\\n -X " + req.SetProxy)
}
for key, vList := range req.HeaderMap {
if len(vList) == 0 {
continue
}
buf.WriteString(" \\\n -H ")
buf.WriteString(`'` + key + `: ` + vList[0] + `'`)
}
return buf.String()
}