-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·555 lines (515 loc) · 14.3 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package main
import (
"crypto/aes"
"crypto/cipher"
"embed"
"flag"
"fmt"
"log"
"m3u8-downloader/parse"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/levigross/grequests"
)
const (
// HEAD_TIMEOUT 请求头超时时间
HEAD_TIMEOUT = 5 * time.Second
// TS_NAME_TEMPLATE ts视频片段命名规则
TS_NAME_TEMPLATE = "%05d.ts"
)
//go:embed index.html
var indexHTML embed.FS
var (
port = flag.String("port", "10000", "请输入端口号")
logger *log.Logger
ro = &grequests.RequestOptions{
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
RequestTimeout: HEAD_TIMEOUT,
Headers: map[string]string{
"Connection": "keep-alive",
"Accept": "*/*",
"Accept-Encoding": "*",
"Accept-Language": "zh-CN,zh;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
},
}
)
// 定义请求参数的结构体
type DownloadRequest struct {
URL string `json:"u" binding:"required"`
NFlag *int `json:"n"`
HTFlag string `json:"ht"`
OFlag string `json:"o" binding:"required"`
CFlag string `json:"c"`
RFlag *bool `json:"r"`
SFlag int `json:"s"`
SPFlag string `json:"sp"`
Referer string `json:"referer"`
Proxy string `json:"proxy"`
Sync bool `json:"sync"`
}
type UpdataTask struct {
ID int `json:"id" binding:"required"`
URL string `json:"url" binding:"required"`
}
// Task represents a download task
type Task struct {
ID int `json:"id"`
URL string `json:"url"`
Status string `json:"status"`
TotalTs int `json:"total_ts"`
Message string `json:"message"`
Completed float32 `json:"completed"`
TotalTime float64 `json:"totalTime"`
MasterList []*parse.MasterPlaylist `json:"master_list"`
TaskInfo DownloadRequest `json:"taskInfo"`
}
var (
tasks []Task
taskID int
taskMux sync.Mutex
)
func main() {
flag.Parse()
r := gin.Default()
r.POST("/download", func(c *gin.Context) {
var req DownloadRequest
if err := c.ShouldBindJSON(&req); err != nil {
if errs, ok := err.(validator.ValidationErrors); ok {
// 遍历错误字段, 进行判断
for _, e := range errs {
if e.Field() == "URL" && e.Tag() == "required" {
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少下载链接"})
return
}
if e.Field() == "OFlag" && e.Tag() == "required" {
c.JSON(http.StatusBadRequest, gin.H{"error": "请填写文件名"})
return
}
}
} else {
// 如果不是验证类型的错误,则原样返回
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
}
// 校验 u 参数
if !strings.HasPrefix(req.URL, "http") {
c.JSON(http.StatusBadRequest, gin.H{"error": "下载链接格式错误"})
return
}
if req.Proxy != "" {
proxyURL, err := url.Parse(req.Proxy) // Proxy URL
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "代理地址错误"})
return
}
ro.Proxies = map[string]*url.URL{proxyURL.Scheme: proxyURL}
} else if req.Proxy == "" && ro.Proxies != nil {
ro.Proxies = nil
}
if req.HTFlag == "" {
req.HTFlag = "v1"
}
var defaultRFlag bool = true
if req.RFlag == nil {
req.RFlag = &defaultRFlag
}
var defaultNFlag int = 24
if req.NFlag == nil {
req.NFlag = &defaultNFlag
}
taskMux.Lock()
taskID++
newTask := Task{
ID: taskID,
URL: req.URL,
Status: "下载中",
TotalTs: 0, // 假设总共有 0 个 ts 文件
Completed: 0,
TotalTime: 0,
TaskInfo: req,
}
tasks = append(tasks, newTask)
taskMux.Unlock()
if req.Sync {
Run(newTask.ID, newTask.TaskInfo.URL,
*(newTask.TaskInfo.NFlag), newTask.TaskInfo.HTFlag,
newTask.TaskInfo.OFlag, newTask.TaskInfo.CFlag,
*(newTask.TaskInfo.RFlag), newTask.TaskInfo.SFlag,
newTask.TaskInfo.SPFlag, newTask.TaskInfo.Referer,
)
} else {
go Run(newTask.ID, newTask.TaskInfo.URL,
*(newTask.TaskInfo.NFlag), newTask.TaskInfo.HTFlag,
newTask.TaskInfo.OFlag, newTask.TaskInfo.CFlag,
*(newTask.TaskInfo.RFlag), newTask.TaskInfo.SFlag,
newTask.TaskInfo.SPFlag, newTask.TaskInfo.Referer,
)
}
c.JSON(http.StatusOK, gin.H{"status": "success", "taskID": newTask.ID})
})
r.POST("/updata", func(c *gin.Context) {
var req UpdataTask
if err := c.ShouldBindJSON(&req); err != nil {
if errs, ok := err.(validator.ValidationErrors); ok {
// 遍历错误字段, 进行判断
for _, e := range errs {
if e.Field() == "URL" && e.Tag() == "required" {
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少下载链接"})
return
}
if e.Field() == "ID" && e.Tag() == "required" {
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少对应的taskID"})
return
}
}
} else {
// 如果不是验证类型的错误,则原样返回
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
}
// 校验 u 参数
if !strings.HasPrefix(req.URL, "http") {
c.JSON(http.StatusBadRequest, gin.H{"error": "下载链接格式错误"})
return
}
taskMux.Lock()
var newTask Task
for i := range tasks {
if tasks[i].ID == req.ID {
tasks[i].Status = "下载中"
tasks[i].URL = req.URL
tasks[i].TotalTs = 0
tasks[i].Completed = 0
tasks[i].TotalTime = 0
newTask = tasks[i]
break
}
}
taskMux.Unlock()
fmt.Println("newTask:", newTask)
go Run(newTask.ID, newTask.URL,
*(newTask.TaskInfo.NFlag), newTask.TaskInfo.HTFlag,
newTask.TaskInfo.OFlag, newTask.TaskInfo.CFlag,
*(newTask.TaskInfo.RFlag), newTask.TaskInfo.SFlag,
newTask.TaskInfo.SPFlag, newTask.TaskInfo.Referer,
)
c.JSON(http.StatusOK, gin.H{"status": "success", "taskID": newTask.ID})
})
r.GET("/tasks", func(c *gin.Context) {
c.JSON(http.StatusOK, tasks)
})
r.GET("/clearTasks", func(c *gin.Context) {
// 找到tasks中所有已经完成的任务,清除掉
taskMux.Lock()
var newTasks []Task
for i := range tasks {
if tasks[i].Status != "完成" {
newTasks = append(newTasks, tasks[i])
}
}
tasks = newTasks
taskMux.Unlock()
c.JSON(http.StatusOK, gin.H{"status": "success"})
})
r.GET("/", func(c *gin.Context) {
// 直接发送 index.html 文件
content, _ := indexHTML.ReadFile("index.html")
c.Data(http.StatusOK, "text/html; charset=utf-8", content)
})
serverPort := ":" + *port
// 启动服务器并监听指定端口
if err := r.Run(serverPort); err != nil {
panic(err)
}
}
func Run(taskID int, m3u8Url string, maxGoroutines int, hostType string, movieName string, cookie string, autoClearFlag bool, insecure int, savePath string, referer string) {
runtime.GOMAXPROCS(runtime.NumCPU())
now := time.Now()
if referer != "" {
ro.Headers["Referer"] = referer
} else {
ro.Headers["Referer"] = getHost(m3u8Url, "v2")
}
if insecure != 0 {
ro.InsecureSkipVerify = true
}
// http 自定义 cookie
if cookie != "" {
ro.Headers["Cookie"] = cookie
}
var download_dir string
pwd, _ := os.Getwd()
if savePath != "" {
pwd = savePath
}
// 初始化下载ts的目录,后面所有的ts文件会保存在这里
download_dir = filepath.Join(pwd, movieName)
if isExist, _ := pathExists(download_dir); !isExist {
os.MkdirAll(download_dir, os.ModePerm)
}
// 2、解析m3u8
m3u8Body, err := getM3u8Body(m3u8Url, hostType)
if err != nil {
errorMsg := fmt.Sprintf("获取不到m3u8内容: %v", err)
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Status = "失败"
tasks[i].Message = errorMsg
break
}
}
return
}
// 如果有masterplaylist则说明是有多层级的m3u8地址,返回让用户选择
if m3u8Body.MasterPlaylist != nil {
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Status = "暂停"
tasks[i].Message = "当前链接内有多个m3u8地址,请选择一个下载"
tasks[i].MasterList = m3u8Body.MasterPlaylist
break
}
}
return
}
// 将 keys 转换为 JSON 格式并打印出来
// 当存在keys时,则去请求加密数据
if len(m3u8Body.Keys) > 0 {
getM3u8Key(m3u8Body)
}
// 如果segmentlist为空,则表示没解析到ts,直接报错,不为空的则更新task
if len(m3u8Body.Segments) == 0 {
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Status = "失败"
tasks[i].TotalTs = 0
tasks[i].Message = "未解析到ts切片"
break
}
}
return
} else {
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Status = "下载中"
tasks[i].TotalTs = len(m3u8Body.Segments)
break
}
}
}
// 3、下载ts文件到download_dir
downloader(taskID, m3u8Body.Segments, maxGoroutines, download_dir, m3u8Body.Keys)
if ok := checkTsDownDir(download_dir); !ok {
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Status = "失败"
tasks[i].Message = "请检查url地址有效性"
break
}
}
return
}
// 4、合并ts切割文件成mp4文件
mp4Path, err := parse.MergeTs(download_dir)
if err != nil {
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Status = "失败"
tasks[i].TotalTime = time.Since(now).Seconds()
tasks[i].Message = fmt.Sprintf("合并切片失败: %s", err.Error())
break
}
}
}
if autoClearFlag {
//自动清除ts文件目录
os.RemoveAll(download_dir)
}
//5、输出下载视频信息
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Status = "完成"
tasks[i].Completed = 1
tasks[i].TotalTime = time.Since(now).Seconds()
tasks[i].Message = "下载成功:" + mp4Path
break
}
}
}
// 获取m3u8地址的host
func getHost(Url, ht string) (host string) {
u, err := url.Parse(Url)
checkErr(err)
switch ht {
case "v1":
host = u.Scheme + "://" + u.Host + filepath.Dir(u.EscapedPath())
case "v2":
host = u.Scheme + "://" + u.Host
}
return
}
// 获取m3u8地址的内容体
func getM3u8Body(Url string, ht string) (*parse.M3u8, error) {
r, err := grequests.Get(Url, ro)
if err != nil {
return nil, err
}
defer r.Close() // 确保关闭响应体
host := getHost(Url, ht)
bodyString := r.String()
lines := strings.Split(bodyString, "\n")
if lines[0] != "#EXTM3U" {
return nil, fmt.Errorf(r.String())
}
m3u8, parseErr := parse.Parse(lines, host)
return m3u8, parseErr
}
// 获取m3u8加密的密钥
func getM3u8Key(m3u8Body *parse.M3u8) {
// 循环Keys,根据里面的URI,发送请求,如果成功,则更新到KeyBody字段
for _, key := range m3u8Body.Keys {
if key.Method != "" && key.Method != parse.CryptMethodNONE {
res, err := grequests.Get(key.URI, ro) // 直接使用key.URI
checkErr(err)
if res.StatusCode == 200 {
key.KeyBody = res.String()
}
}
}
}
// downloader m3u8 下载器
func downloader(taskID int, tsList []*parse.Segment, maxGoroutines int, downloadDir string, keys map[int]*parse.Key) {
retry := 5 //单个 ts 下载重试次数
var wg sync.WaitGroup
limiter := make(chan struct{}, maxGoroutines) //chan struct 内存占用 0 bool 占用 1
tsLen := len(tsList)
downloadCount := 0
for _, ts := range tsList {
wg.Add(1)
limiter <- struct{}{}
go func(ts *parse.Segment, downloadDir string, key *parse.Key, retryies int) {
defer func() {
wg.Done()
<-limiter
}()
downloadTsFile(ts, downloadDir, key, retryies)
downloadCount++
for i := range tasks {
if tasks[i].ID == taskID {
tasks[i].Completed = float32(downloadCount) / float32(tsLen)
break
}
}
}(ts, downloadDir, keys[ts.KeyIndex], retry)
}
wg.Wait()
}
// 下载ts文件
func downloadTsFile(ts *parse.Segment, download_dir string, key *parse.Key, retries int) {
defer func() {
if r := recover(); r != nil {
downloadTsFile(ts, download_dir, key, retries-1)
}
}()
curr_path_file := fmt.Sprintf("%s/%s", download_dir, ts.Name)
if isExist, _ := pathExists(curr_path_file); isExist {
return
}
res, err := grequests.Get(ts.URI, ro)
if err != nil || !res.Ok {
if retries > 0 {
downloadTsFile(ts, download_dir, key, retries-1)
return
} else {
return
}
}
// 校验长度是否合法
var origData []byte
origData = res.Bytes()
contentLen := 0
contentLenStr := res.Header.Get("Content-Length")
if contentLenStr != "" {
contentLen, _ = strconv.Atoi(contentLenStr)
}
if len(origData) == 0 || (contentLen > 0 && len(origData) < contentLen) || res.Error != nil {
downloadTsFile(ts, download_dir, key, retries-1)
return
}
// 解密出视频 ts 源文件
if key != nil && key.Method != parse.CryptMethodNONE && key.Method != "" {
//解密 ts 文件,算法:aes 128 cbc pack5
origData, err = AesDecrypt(origData, []byte(key.KeyBody), []byte(key.IV))
if err != nil {
downloadTsFile(ts, download_dir, key, retries-1)
return
}
}
// https://en.wikipedia.org/wiki/MPEG_transport_stream
// Some TS files do not start with SyncByte 0x47, they can not be played after merging,
// Need to remove the bytes before the SyncByte 0x47(71).
syncByte := uint8(71) //0x47
bLen := len(origData)
for j := 0; j < bLen; j++ {
if origData[j] == syncByte {
origData = origData[j:]
break
}
}
os.WriteFile(curr_path_file, origData, 0666)
}
func checkTsDownDir(dir string) bool {
if isExist, _ := pathExists(filepath.Join(dir, fmt.Sprintf(TS_NAME_TEMPLATE, 0))); !isExist {
return true
}
return false
}
// ============================== shell相关 ==============================
// 判断文件是否存在
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// ============================== 加解密相关 ==============================
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func AesDecrypt(crypted, key []byte, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(iv) == 0 {
iv = key
}
blockMode := cipher.NewCBCDecrypter(block, iv[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return origData, nil
}
func checkErr(e error) {
if e != nil {
logger.Panic(e)
}
}