-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
164 lines (139 loc) · 3.72 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
package main
import (
"errors"
"fmt"
"github.com/deadblue/elevengo"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-homedir"
gim "github.com/ozankasikci/go-image-merge"
"image/jpeg"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"syscall"
)
var client *elevengo.Client
func init() {
uid, cid, seid := getCredentials()
client = elevengo.Default()
client.ImportCredentials(&elevengo.Credentials{
UID: uid,
CID: cid,
SEID: seid,
})
}
func main() {
http.HandleFunc("/add-magnet-to-115", addUrl)
if err := http.ListenAndServe(":3333", nil); err != nil {
log.Fatalf("服务启动失败: %v", err)
}
}
func addUrl(writer http.ResponseWriter, request *http.Request) {
if err := request.ParseForm(); err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}
url := request.FormValue("param1")
hash, err := client.OfflineAddUrl(url)
if err != nil {
if err.Error() == "请验证账号" {
var captchaSession *elevengo.CaptchaSession
captchaSession, err = client.CaptchaStart()
expectImg, _ := homedir.Expand("~/115_captcha_expect.png")
choicesImg, _ := homedir.Expand("~/115_captcha_choices.jpg")
mergeImg, _ := homedir.Expand("~/115_captcha.jpg")
defer os.Remove(expectImg)
defer os.Remove(choicesImg)
defer os.Remove(mergeImg)
ioutil.WriteFile(expectImg, captchaSession.CodeValue, 0644)
ioutil.WriteFile(choicesImg, captchaSession.CodeKeys, 0644)
grids := []*gim.Grid{
{ImageFilePath: expectImg},
{ImageFilePath: choicesImg},
}
rgba, _ := gim.New(grids, 1, 2, gim.OptGridSize(255, 100)).Merge()
file, _ := os.Create(mergeImg)
err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: 80})
file.Close()
openUrl(mergeImg)
for {
captcha, err := askCaptcha()
if err != nil {
fmt.Println(err)
}
if err := client.CaptchaSubmit(captcha, captchaSession); err != nil {
fmt.Println(err)
if err.Error() != "captcha code incorrect" {
break
}
}
break
}
}
if err != nil {
fmt.Println(err.Error())
}
http.Error(writer, "离线任务添加失败,请打开终端查看详细错误信息", http.StatusBadRequest)
return
}
fmt.Fprintf(writer, "离线任务添加成功:%s", hash)
fmt.Printf("离线任务添加成功:%s\n", hash)
}
func openUrl(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
c := exec.Command(cmd, args...)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return c.Start()
}
func askCaptcha() (string, error) {
validate := func(input string) error {
_, err := strconv.ParseInt(input, 10, 16)
if err != nil {
return errors.New("输入 0-9 范围内的 4 个数字")
}
return nil
}
prompt := promptui.Prompt{
Label: "验证码(4位数,第一行0-4, 第二行5-9)",
Validate: validate,
}
return prompt.Run()
}
func getCredentials() (uid, cid, seid string) {
configFile, err := homedir.Expand("~/.115.cookies")
if err != nil {
panic(err)
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
panic("请在 home 目录下创建 .115.cookies 文件")
}
if data, err := ioutil.ReadFile(configFile); err != nil {
panic(err)
} else {
cookies := string(data)
uidReg := regexp.MustCompile(`UID=(\w+);`)
cidReg := regexp.MustCompile(`CID=(\w+);`)
seidReg := regexp.MustCompile(`SEID=(\w+);`)
uid = uidReg.FindAllStringSubmatch(cookies, -1)[0][1]
cid = cidReg.FindAllStringSubmatch(cookies, -1)[0][1]
seid = seidReg.FindAllStringSubmatch(cookies, -1)[0][1]
return
}
return "", "", ""
}