-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
334 lines (288 loc) · 9.25 KB
/
main.go
File metadata and controls
334 lines (288 loc) · 9.25 KB
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
package main
import (
"github.com/wonderivan/logger"
"github.com/devedge/imagehash"
"github.com/anthonynsimon/bild/transform"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/segment"
"encoding/hex"
"encoding/json"
"encoding/base64"
"image"
"net/http"
"net/url"
"os"
"io"
"io/ioutil"
"strings"
"strconv"
"time"
"flag"
"fmt"
)
var (
u string
p string
v bool
t bool
version string
)
func init(){
flag.StringVar(&u, "u", "", "设置校园网登录学号")
flag.StringVar(&p, "p", "", "设置校园网登录密码(身份证后8位)")
flag.BoolVar(&v, "v", false, "检查版本")
flag.BoolVar(&t, "t", false, "循环运行(按Ctrl+C结束程序)")
flag.Usage = usage
}
func usage(){
fmt.Println("Usage: zquAutoLogin-go -u [studentId] -p [password]\nOptions:")
flag.PrintDefaults()
}
func main(){
version = "v1.3.0"
flag.Parse()
if v {
versionCompare(version, getVersion())
} else if u == "" || p == "" {
flag.Usage()
return
} else {
logger.Info("version:", version)
logger.Info("帐号:", u, "密码:", p)
networkTest()
for t {
time.Sleep(time.Duration(1)*time.Minute)
networkTest()
}
}
}
func getVersion() string{
url := "https://api.github.com/repos/ooxoop/zquAutoLogin-go/releases/latest"
timeout := time.Duration(5 * time.Second)
client := &http.Client{Timeout: timeout,}
req, _ := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
if err != nil {
logger.Error("获取最新版本号失败,请检查网络" )
os.Exit(2)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var m map[string]interface{}
json.Unmarshal(body, &m)
return m["tag_name"].(string)
}
func versionCompare(v1, v2 string) {
if v1[1] == v2[1] {
if v1[3] == v2[3] {
if v1[5] == v2[5] {
fmt.Println("[1]当前版本为最新版本,无需更新")
os.Exit(1)
}
}
}
fmt.Println("[0]检查到新版本,开始更新...")
os.Exit(1)
}
func networkTest() {
url := "http://quan.suning.com/getSysTime.do"
timeout := time.Duration(5 * time.Second)
client := &http.Client{Timeout: timeout,}
req, _ := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
if err != nil {
logger.Error("获取重定向连接失败" )
os.Exit(2)
}
defer resp.Body.Close()
test_status := resp.Request.URL.Host
if test_status == "quan.suning.com" {
logger.Info("可以上网")
} else if (test_status == "10.0.1.51" || test_status == "eportal.zqu.edu.cn"){
resp.Request.ParseForm();
formStr := "wlanuserip="
formStr += resp.Request.Form.Get("wlanuserip")
formStr += "&wlanacname="
formStr += resp.Request.Form.Get("wlanacname")
formStr += "&ssid="
formStr += resp.Request.Form.Get("ssid")
formStr += "&nasip="
formStr += resp.Request.Form.Get("nasip")
formStr += "&mac="
formStr += resp.Request.Form.Get("mac")
formStr += "&t="
formStr += resp.Request.Form.Get("t")
formStr += "&url="
formStr += resp.Request.Form.Get("url")
result, message := autoLogin_1(formStr)
if result == "success" {
logger.Info("已经通过局域网验证")
time.Sleep(time.Duration(3)*time.Second)
networkTest()
} else {
logger.Error(message)
}
} else if test_status == "enet.10000.gd.cn:10001"{
if autoLogin_2(u, p) == "success" {
time.Sleep(time.Duration(3)*time.Second)
networkTest()
}
}
}
func autoLogin_1(str string) (string, string) {
userid := u
password := string([]rune(p)[2:])
login_url := "http://10.0.1.51/eportal/InterFace.do?method=login"
form := url.Values{
"userId" : {userid},
"password" : {password},
"queryString" : {str},
}
referer := "http://10.0.1.51/eportal/index.jsp?"
referer += str
client := &http.Client{}
req, _ := http.NewRequest("POST", login_url, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("Accept", "*/*")
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.Header.Set("Accept-Language", "zh-Hans-CN, zh-Hans; q=0.5")
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Connection", "Keep-Alive")
req.Header.Set("Host", "10.0.1.51")
req.Header.Set("Origin", "http://10.0.1.51")
req.Header.Set("Referer", referer)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763")
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var m map[string]interface{}
json.Unmarshal(body, &m)
return m["result"].(string), m["message"].(string)
}
func autoLogin_2 (userid, password string) string {
test_url := "http://quan.suning.com/getSysTime.do"
login_url := "http://enet.10000.gd.cn:10001/login.do"
captcha_url := "http://enet.10000.gd.cn:10001/common/image.jsp"
resp, _ := http.Head(test_url)
defer resp.Body.Close()
resp.Request.ParseForm();
formStr := "wlanacip="
formStr += resp.Request.Form.Get("wlanacip")
formStr += "&wlanuserip="
formStr += resp.Request.Form.Get("wlanuserip")
wlanacip := resp.Request.Form.Get("wlanacip")
wlanuserip := resp.Request.Form.Get("wlanuserip")
//logger.Debug("wlanacip:", wlanacip, "wlanuserip:", wlanuserip)
client := &http.Client{}
req, _ := http.NewRequest("GET", captcha_url, nil)
resp, _ = client.Do(req)
cookie := strings.Split(resp.Header.Get("Set-Cookie"), ";")[0]
req, _ = http.NewRequest("GET", captcha_url, nil)
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.Header.Set("Cookie", cookie)
resp, _ = client.Do(req)
img, _ := os.Create("image.jpg")
io.Copy(img, resp.Body)
captcha := getCaptcha()
encode := base64.StdEncoding.EncodeToString([]byte(password))
form := url.Values{
"edubas" : {wlanacip},
"eduuser" : {wlanuserip},
"userName1" : {userid},
"password1" : {encode},
"patch" : {"wifi"},
"rand" : {captcha},
}
referer := "http://enet.10000.gd.cn:10001/zq/zq251/index.jsp?"
referer += formStr
req, _ = http.NewRequest("POST", login_url, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cache-Control", "max-age=0")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Content-Length", "110")
req.Header.Set("Host", "enet.10000.gd.cn:10001")
req.Header.Set("Origin", "http://enet.10000.gd.cn:10001")
req.Header.Set("Referer", referer)
req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36")
req.Header.Set("Cookie", cookie)
logger.Info("正在登录电信网络")
resp, _ = client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
result := string(body)
if strings.Index(result, "success") > 0 {
logger.Info("登录成功")
return "success"
} else {
r := result[402 : len(result)-600]
val := strings.Index(r, "\"")
r = r[:val]
logger.Error(r)
return "fail"
}
}
func getCaptcha() string {
src,_ := imagehash.OpenImg("./image.jpg")
x := 13
h1,_ := imagehash.DhashHorizontal(segment.Threshold(effect.Grayscale(transform.Crop(src, image.Rect(5, 3, 5+x, 3+x))), 128), 8)
h2,_ := imagehash.DhashHorizontal(segment.Threshold(effect.Grayscale(transform.Crop(src, image.Rect(5+x, 3, 5+2*x, 3+x))), 128), 8)
h3,_ := imagehash.DhashHorizontal(segment.Threshold(effect.Grayscale(transform.Crop(src, image.Rect(5+2*x, 3, 5+3*x, 3+x))), 128), 8)
h4,_ := imagehash.DhashHorizontal(segment.Threshold(effect.Grayscale(transform.Crop(src, image.Rect(5+3*x, 3, 5+4*x, 3+x))), 128), 8)
var numMap map[int]string
numMap = make(map[int]string)
numMap [0] = "0e33232b2b23330e"
numMap [1] = "0d0d0d2d2d0c0f03"
numMap [2] = "0f6202264d983606"
numMap [3] = "0f72060d0602660f"
numMap [4] = "26461696370b2706"
numMap [5] = "0626203e0602660f"
numMap [6] = "06b3312e33233306"
numMap [7] = "030b02062d4a0a9a"
numMap [8] = "8632329637233386"
numMap [9] = "8e3323338b43328f"
min := 9999
min_p1 := 0
for i := 0; i < 10; i++ {
numVal, _ := hex.DecodeString(numMap[i])
if imagehash.GetDistance(h1 , numVal) < min {
min = imagehash.GetDistance(h1, numVal)
min_p1 = i
}
}
min = 9999
min_p2 := 0
for i := 0; i < 10; i++ {
numVal, _ := hex.DecodeString(numMap[i])
if imagehash.GetDistance(h2 , numVal) < min {
min = imagehash.GetDistance(h2, numVal)
min_p2 = i
}
}
min = 9999
min_p3 := 0
for i := 0; i < 10; i++ {
numVal, _ := hex.DecodeString(numMap[i])
if imagehash.GetDistance(h3 , numVal) < min {
min = imagehash.GetDistance(h3, numVal)
min_p3 = i
}
}
min = 9999
min_p4 := 0
for i := 0; i < 10; i++ {
numVal, _ := hex.DecodeString(numMap[i])
if imagehash.GetDistance(h4 , numVal) < min {
min = imagehash.GetDistance(h4, numVal)
min_p4 = i
}
}
code := strconv.Itoa(min_p1)
code += strconv.Itoa(min_p2)
code += strconv.Itoa(min_p3)
code += strconv.Itoa(min_p4)
return code
}