-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
200 lines (193 loc) · 5.55 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
package main
import (
. "./TiebaSign"
"bytes"
"container/list"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
func getCookie(cookieFileName string, silence bool) (cookieJar *cookiejar.Jar, hasError bool) {
needLogin := true
cookieJar, _ = cookiejar.New(nil)
cookies := make([]*http.Cookie, 0)
if _, err := os.Stat(cookieFileName); err == nil {
rawCookie, _ := ioutil.ReadFile(cookieFileName)
rawCookie = bytes.Trim(rawCookie, "\xef\xbb\xbf")
rawCookieList := strings.Split(strings.Replace(string(rawCookie), "\r\n", "\n", -1), "\n")
for _, rawCookieLine := range rawCookieList {
rawCookieInfo := strings.SplitN(rawCookieLine, "=", 2)
if len(rawCookieInfo) < 2 {
continue
}
cookies = append(cookies, &http.Cookie{
Name: rawCookieInfo[0],
Value: rawCookieInfo[1],
Domain: ".baidu.com",
})
}
fmt.Printf("Verifying imported cookies from %s...", cookieFileName)
url, _ := url.Parse("http://baidu.com")
cookieJar.SetCookies(url, cookies)
if GetLoginStatus(cookieJar) {
needLogin = false
fmt.Println("OK")
} else {
fmt.Println("Failed")
}
}
if needLogin && !silence {
fmt.Println("Cannot login, since baidu has switched to RSA login, and I have no time to make a new login program. You have to make a cookie.txt, and paste your cookie in the file.")
fmt.Println("Cookie Format: BDUSS=xxxxxxx")
return nil, true
} else if needLogin && silence {
return nil, true
}
return cookieJar, false
}
type SignTask struct {
cookie *cookiejar.Jar
tieba LikedTieba
failedAttempts int
}
func main() {
var batchMode = flag.Bool("batch", false, "Batch Sign mode")
var maxRetryTimes = flag.Int("retry", 7, "Max retry times for a single tieba")
var cookieFileName = flag.String("cookie", "cookie.txt", "Try to load cookie from specified file")
flag.Parse()
currentDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
os.Chdir(currentDir)
fmt.Println("Tieba Sign (Go Version) beta")
fmt.Println("Author: kookxiang <[email protected]>")
fmt.Println()
if *batchMode == false {
cookie, hasError := getCookie(*cookieFileName, *cookieFileName != "cookie.txt")
if hasError {
return
}
fmt.Print("Fetching tieba list...")
likedTiebaList, err := GetLikedTiebaList(cookie)
if err != nil {
fmt.Println("Error!")
fmt.Println(err)
return
} else {
fmt.Println("OK")
}
taskList := list.New()
for _, tieba := range likedTiebaList {
taskList.PushBack(SignTask{
tieba: tieba,
cookie: cookie,
failedAttempts: 0,
})
}
for {
taskNode := taskList.Front()
if taskNode == nil {
break
}
taskList.Remove(taskNode)
task := taskNode.Value.(SignTask)
fmt.Printf("Now signing %s...", ToUtf8(task.tieba.Name))
status, _, exp := TiebaSign(task.tieba, task.cookie)
if status == 2 {
if exp > 0 {
fmt.Printf("Succeed! Exp +%d\n", exp)
} else {
fmt.Println("Succeed")
}
} else if status == 1 {
task.failedAttempts++
if task.failedAttempts <= *maxRetryTimes {
taskList.PushBack(task) // push failed task back to list
fmt.Println("Failed, retry later")
} else {
fmt.Println("Failed")
}
} else {
fmt.Println("Failed")
}
if exp > 0 || status == 1 {
time.Sleep(2e9)
}
}
} else {
fmt.Println("Loading and verifying Cookies from ./cookies/")
cookieFiles, _ := ioutil.ReadDir("cookies")
threadList := sync.WaitGroup{}
cookieList := map[string]*cookiejar.Jar{}
for _, file := range cookieFiles {
profileName := strings.Replace(file.Name(), ".txt", "", 1)
cookie, hasError := getCookie("cookies/"+file.Name(), true)
if hasError {
fmt.Errorf("Failed to load profile %s, invalid cookie!\n", profileName)
} else {
cookieList[profileName] = cookie
}
}
for profileName, cookie := range cookieList {
threadList.Add(1)
go func(profileName string, cookie *cookiejar.Jar) {
fmt.Printf("[%s] Go routine started.\n", profileName)
likedTiebaList, err := GetLikedTiebaList(cookie)
if err != nil {
fmt.Printf("[%s] Error while fetching tieba list\n", profileName)
fmt.Printf("[%s] Go routine stopped.\n", profileName)
threadList.Done()
return
} else {
fmt.Printf("[%s] Loaded tieba list.\n", profileName)
}
taskList := list.New()
for _, tieba := range likedTiebaList {
taskList.PushBack(SignTask{
tieba: tieba,
cookie: cookie,
failedAttempts: 0,
})
}
for {
taskNode := taskList.Front()
if taskNode == nil {
break
}
taskList.Remove(taskNode)
task := taskNode.Value.(SignTask)
status, _, exp := TiebaSign(task.tieba, task.cookie)
if status == 2 {
if exp > 0 {
fmt.Printf("[%s] Succeed: %s, Exp +%d\n", profileName, ToUtf8(task.tieba.Name), exp)
} else {
fmt.Printf("[%s] Succeed: %s\n", profileName, ToUtf8(task.tieba.Name))
}
} else if status == 1 {
fmt.Printf("[%s] Failed: %s\n", profileName, ToUtf8(task.tieba.Name))
task.failedAttempts++
if task.failedAttempts <= *maxRetryTimes {
taskList.PushBack(task) // push failed task back to list
}
} else {
fmt.Printf("[%s] Failed: %s\n", profileName, ToUtf8(task.tieba.Name))
}
if exp > 0 || status == 1 {
time.Sleep(2e9)
}
}
fmt.Printf("[%s] Finished!\n", profileName)
fmt.Printf("[%s] Go routine stopped.\n", profileName)
threadList.Done()
}(profileName, cookie)
}
threadList.Wait()
fmt.Println("All Task Finished! Congratulation!")
}
}