-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm3u8.go
208 lines (186 loc) · 4.13 KB
/
m3u8.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
package main
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
)
type configuration struct {
Url string
Outf string
M3u8f string
Keystr string
Iv string
}
func main() {
//copy /b F:\f\*.ts E:\f\new.ts
file, err := os.Open("config.json")
if err != nil {
panic("can't find config.json !")
}
defer file.Close()
size := false
if len(os.Args) > 1 {
size = true
}
decoder := json.NewDecoder(file)
conf := configuration{}
err = decoder.Decode(&conf)
if err != nil {
panic(err)
}
url := conf.Url
outf := conf.Outf
keystr := conf.Keystr
m3u8f := conf.M3u8f
//if iv set to 0, create []byte{0,0...}
ivstr := conf.Iv
pad := "00000"
f, err := os.Open(m3u8f)
if err != nil {
panic(err)
}
defer f.Close()
rd := bufio.NewReader(f)
i := 1
if size {
bytenum := 0
for {
line, err := rd.ReadString('\n') //以'\n'为结束符读入一行
if err != nil || io.EOF == err {
break
}
str := strings.Replace(line, "\n", "", -1)
if strings.HasSuffix(str, "ts") {
if i == 1 {
res, err := http.Get(url + str)
if err != nil {
panic(err)
}
key := []byte(keystr)
iv := []byte{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
if ivstr !="0"{
iv =[]byte(ivstr)
}
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
if keystr != "" {
result, err := Aes128Decrypt(body, key, iv)
if err != nil {
fmt.Println(err.Error())
return
}
bytenum = len(result)
} else {
bytenum = len(body)
}
res.Body.Close()
}
i++
}
}
fmt.Println(bytenum*i/1024/1024, "M")
os.Exit(0)
}
for {
line, err := rd.ReadString('\n') //以'\n'为结束符读入一行
if err != nil || io.EOF == err {
break
}
str := strings.Replace(line, "\n", "", -1)
if strings.HasSuffix(str, "ts") {
fmt.Println(url + str)
// download ts file
res, err := http.Get(url + str)
if err != nil {
panic(err)
}
// set filename
s := pad + strconv.Itoa(i)
fname := s[len(s)-5:]
key := []byte(keystr)
iv := []byte{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
if ivstr !="0"{
iv =[]byte(ivstr)
}
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
if keystr != "" {
result, err := Aes128Decrypt(body, key, iv)
if err != nil {
fmt.Println(err.Error())
return
}
os.WriteFile(outf+fname+".ts", result, 0666)
} else {
os.WriteFile(outf+fname+".ts", body, 0666)
}
fmt.Println(fname)
//io.Copy(f, res.Body)
res.Body.Close()
i++
}
}
fmt.Println(`copy /b F:\f\*.ts E:\f\new.ts`)
}
func Aes128Encrypt(origData, key []byte, IV []byte) ([]byte, error) {
if key == nil || len(key) != 16 {
return nil, nil
}
if IV != nil && len(IV) != 16 {
return nil, nil
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData = PKCS5Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, IV[:blockSize])
crypted := make([]byte, len(origData))
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
func Aes128Decrypt(crypted, key []byte, IV []byte) ([]byte, error) {
if key == nil || len(key) != 16 {
return nil, nil
}
if IV != nil && len(IV) != 16 {
return nil, nil
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, IV[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return origData, nil
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
// 去掉最后一个字节 unpadding 次
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}