-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (165 loc) · 4.01 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"regexp"
"strings"
"github.com/iuroc/gododo/biliqr"
"github.com/iuroc/gododo/dodo"
"github.com/skip2/go-qrcode"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
PrintHeader()
userInfo := GetUserInfo()
ClearTerminal()
PrintHeader()
for {
fmt.Printf("%s\n\n", strings.Repeat("-", 40))
fmt.Print("🚩 输入文件路径或拖拽文件到此处: ")
if !scanner.Scan() {
continue
}
path := TrimPathInput(scanner.Text())
work, err := dodo.NewUploadWork(path, userInfo.Token, userInfo.UID)
if os.IsNotExist(err) {
fmt.Println("❗ 错误: 文件不存在,请检查路径是否正确。")
continue
} else if err != nil {
fmt.Println("❗ 错误:", err)
continue
}
history, err := work.History()
if err != nil {
fmt.Println("❗ 错误:", err)
continue
}
if history.HasRecord {
fmt.Println("🎉 上传成功:", history.ResourceURL)
continue
}
if err = work.Upload(); err != nil {
fmt.Println("❗ 错误:", err)
continue
}
resourceURL, err := work.Record()
if err != nil {
fmt.Println("❗ 错误:", err)
continue
}
fmt.Println("🎉 上传成功:", resourceURL)
fmt.Println()
}
}
func PrintHeader() {
fmt.Print("DoDo 文件直链获取工具 [github.com/iuroc/gododo]\n\n")
}
// TrimPathInput 去除路径两端的特殊字符。
func TrimPathInput(input string) string {
return regexp.MustCompile(`^[\s&'"]+|[\s&'"]+$|^file:///`).ReplaceAllString(input, "")
}
// GetUserInfo 获取用户信息,如果不存在,则要求用户扫码登录。
func GetUserInfo() *UserInfo {
data, err := os.ReadFile("userInfo.json")
needQR := false
encryptedUserInfo := &UserInfo{}
var userInfo *UserInfo
if os.IsNotExist(err) {
needQR = true
} else if err != nil {
log.Fatalln("[os.ReadFile] userInfo 文件读取失败", err)
} else {
err = json.Unmarshal(data, encryptedUserInfo)
if err != nil {
needQR = true
} else {
userInfo, err = encryptedUserInfo.Decrypt()
needQR = err != nil || !userInfo.Check()
}
}
if needQR {
qr, info, err := biliqr.NewLoginQR(qrcode.Low)
if err != nil {
log.Fatalln("[biliqr.NewLoginQR] 创建二维码失败", err)
}
fmt.Println("请使用哔哩哔哩 APP 扫描下方二维码:")
fmt.Println()
fmt.Println(qr.ToSmallString(false))
for {
status, err := biliqr.GetThirdQRStatus(info.OauthKey)
if err != nil {
log.Fatalln("[biliqr.GetThirdQRStatus]", err)
}
if status.Success() {
token, uid, err := dodo.GetTokenAndUID(status.Data.TmpToken)
if err != nil {
log.Fatalln("dodo.GetTokenAndUID", err)
}
userInfo = &UserInfo{
Token: token,
UID: uid,
}
encryptedUserInfo, err = userInfo.Encrypt()
if err != nil {
log.Fatalln("[userInfo.Encrypt]", err)
}
encryptedUserInfo.Save("userInfo.json")
return userInfo
}
}
}
return userInfo
}
type UserInfo struct {
Token string `json:"token"`
UID string `json:"uid"`
}
// Check 校验 Token 和 UID 的有效性。
func (info *UserInfo) Check() bool {
return dodo.CheckTokenAndUID(info.Token, info.UID)
}
// Save 以 JSON 格式保存 Token 和 UID 到文件。
func (info *UserInfo) Save(path string) {
data, err := json.Marshal(info)
if err != nil {
log.Fatalln("[json.Marshal]", err)
}
err = os.WriteFile(path, data, 0644)
if err != nil {
log.Fatalln("[os.WriteFile]", err)
}
}
func (info *UserInfo) Encrypt() (*UserInfo, error) {
aesConfig, err := NewAESConfig()
if err != nil {
return nil, err
}
return &UserInfo{
Token: aesConfig.Encrypt([]byte(info.Token)),
UID: aesConfig.Encrypt([]byte(info.UID)),
}, nil
}
func (info *UserInfo) Decrypt() (*UserInfo, error) {
aesConfig, err := NewAESConfig()
if err != nil {
return nil, err
}
token, err := aesConfig.Decrypt(info.Token)
if err != nil {
return nil, err
}
uid, err := aesConfig.Decrypt(info.UID)
if err != nil {
return nil, err
}
return &UserInfo{
Token: token,
UID: uid,
}, nil
}
func ClearTerminal() {
fmt.Print("\x1b[H\x1b[2J")
}