@@ -7,50 +7,65 @@ import (
77 "net/url"
88 "strings"
99 "time"
10-
11- "golang.org/x/text/encoding/simplifiedchinese"
1210)
1311
1412func loginCampus (config * Config ) error {
15- // 初始化客户端
1613 client := & http.Client {
17- Timeout : time . Duration ( 10 ) * time .Second ,
14+ Timeout : 10 * time .Second ,
1815 }
19- // 构造 POST 请求
16+
17+ urlStr := "http://10.0.0.252/cgi-bin/ace_web_auth.cgi"
18+
2019 formData := url.Values {
21- "DDDDD" : {config .Username },
22- "upass" : {config .Password },
23- "0MKKey" : {"登 录" },
20+ "web_jumpto" : {"" },
21+ "orig_referer" : {"http://www.qq.com/" },
22+ "username" : {config .Username },
23+ "userpwd" : {config .Password },
24+ "login_page" : {"" },
25+ "temp_account" : {"0" },
26+ "path" : {"-6" },
27+ "u_type" : {"1" },
2428 }
25- // 执行网络请求
26- response , err := client .PostForm ("http://10.0.0.253/0.htm" , formData )
29+
30+ response , err := client .PostForm (urlStr , formData )
2731 if err != nil {
2832 return fmt .Errorf ("网络请求失败" )
2933 }
30- // 自动关闭请求体
31- defer func (Body io.ReadCloser ) {
32- _ = Body .Close ()
33- }(response .Body )
34- // 读取请求
34+ defer response .Body .Close ()
35+
3536 bodyBytes , err := io .ReadAll (response .Body )
3637 if err != nil {
3738 return fmt .Errorf ("读取响应失败" )
3839 }
39- // 转换编码
40- utf8body , err := simplifiedchinese .GBK .NewDecoder ().Bytes (bodyBytes )
41- if err != nil {
42- return fmt .Errorf ("编码转换失败" )
43- }
44- // 转换成 UTF-8 编码
45- html := string (utf8body )
46- // 返回请求状态
47- if strings .Contains (html , "您已经成功登录" ) {
40+
41+ html := string (bodyBytes )
42+ redirect := extractRedirect (html )
43+
44+ if strings .Contains (redirect , "login_online_detail" ) {
4845 return nil
49- } else if strings .Contains (html , "userid error1" ) {
50- return fmt .Errorf ("账号错误" )
51- } else if strings .Contains (html , "userid error2" ) {
52- return fmt .Errorf ("密码错误" )
46+ } else if strings .Contains (redirect , "reason=27" ) {
47+ return fmt .Errorf ("账号或密码错误" )
5348 }
54- // 无法处理
49+
5550 return fmt .Errorf ("其他错误" )
5651}
52+
53+ func extractRedirect (html string ) string {
54+ start := strings .Index (html , "location" )
55+ if start == - 1 {
56+ return ""
57+ }
58+
59+ firstQuote := strings .IndexAny (html [start :], `"'` )
60+ if firstQuote == - 1 {
61+ return ""
62+ }
63+ firstQuote += start
64+
65+ secondQuote := strings .IndexAny (html [firstQuote + 1 :], `"'` )
66+ if secondQuote == - 1 {
67+ return ""
68+ }
69+ secondQuote += firstQuote + 1
70+ return html [firstQuote + 1 : secondQuote ]
71+ }
0 commit comments