From 7bbdc4e09736871afd4f4bf9462a3e3292dc13d8 Mon Sep 17 00:00:00 2001 From: PatrickStar <1592810043@qq.com> Date: Mon, 18 Sep 2023 22:30:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=AD=A3=E6=96=B9?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/controllerHandle.go | 5 ++++ .../zfController/controllerHandle.go | 4 +++ app/controller/zfController/zfController.go | 2 ++ app/errors/funnelErrors.go | 1 + app/errors/httpResponseErrors.go | 1 + app/model/user.go | 1 + app/service/libraryService/login.go | 2 +- app/service/serviceUniFunction.go | 6 ++-- app/service/zfService/login.go | 28 +++++++++++-------- app/service/zfService/zfService.go | 3 ++ app/utils/fetch/fetch.go | 13 +++++---- 11 files changed, 46 insertions(+), 20 deletions(-) diff --git a/app/controller/controllerHandle.go b/app/controller/controllerHandle.go index e571bcd..36a3082 100644 --- a/app/controller/controllerHandle.go +++ b/app/controller/controllerHandle.go @@ -29,6 +29,11 @@ func ErrorHandle(context *gin.Context, err error) { exp = errors.SessionExpired break } + case errors.ERR_OAUTH_ERROR: + { + exp = errors.OauthError + break + } } println(err.Error()) diff --git a/app/controller/zfController/controllerHandle.go b/app/controller/zfController/controllerHandle.go index 4a209f4..08d61eb 100644 --- a/app/controller/zfController/controllerHandle.go +++ b/app/controller/zfController/controllerHandle.go @@ -19,6 +19,10 @@ func ZFTermInfoHandle(context *gin.Context, cb func(*model.User, string, string) if err == errors.ERR_Session_Expired { user, err = controller.LoginHandle(context, zfService.GetUser, false) + if err != nil { + controller.ErrorHandle(context, err) + return nil, err + } result, err = cb(user, context.PostForm("year"), context.PostForm("term")) } diff --git a/app/controller/zfController/zfController.go b/app/controller/zfController/zfController.go index f3617ed..d23b8ce 100644 --- a/app/controller/zfController/zfController.go +++ b/app/controller/zfController/zfController.go @@ -121,12 +121,14 @@ func GetProgInfo(context *gin.Context) { user, err := controller.LoginHandle(context, zfService.GetUser, false) if err != nil { + controller.ErrorHandle(context, err) return } result, err := zfService.GetTrainingPrograms(user) if err == errors.ERR_SESSION_EXPIRES { user, err = controller.LoginHandle(context, zfService.GetUser, false) if err != nil { + controller.ErrorHandle(context, err) return } result, err = zfService.GetTrainingPrograms(user) diff --git a/app/errors/funnelErrors.go b/app/errors/funnelErrors.go index 77b1784..279a002 100644 --- a/app/errors/funnelErrors.go +++ b/app/errors/funnelErrors.go @@ -11,3 +11,4 @@ var ERR_WRONG_Captcha = errors.New("ERR_WRONG_Captcha") var ERR_INVALID_ARGS = errors.New("invalid args") var ERR_SESSION_NOT_EXIST = errors.New("ERR_SESSION_NOT_EXIST") var ERR_SESSION_EXPIRES = errors.New("ERR_SESSION_EXPIRES") +var ERR_OAUTH_ERROR = errors.New("ERR_OAUTH_ERROR") diff --git a/app/errors/httpResponseErrors.go b/app/errors/httpResponseErrors.go index 6fc1c6c..3a077ce 100644 --- a/app/errors/httpResponseErrors.go +++ b/app/errors/httpResponseErrors.go @@ -13,3 +13,4 @@ var InvalidArgs = HttpResponseError{410, "参数错误"} var WrongPassword = HttpResponseError{412, "密码错误"} var CaptchaFailed = HttpResponseError{413, "验证码错误"} var SessionExpired = HttpResponseError{414, "缓存过期"} +var OauthError = HttpResponseError{415, "缓存过期"} diff --git a/app/model/user.go b/app/model/user.go index 05fde78..4016450 100644 --- a/app/model/user.go +++ b/app/model/user.go @@ -6,4 +6,5 @@ type User struct { Username string Password string Session http.Cookie + Route http.Cookie } diff --git a/app/service/libraryService/login.go b/app/service/libraryService/login.go index fd17615..ade4841 100644 --- a/app/service/libraryService/login.go +++ b/app/service/libraryService/login.go @@ -37,5 +37,5 @@ func login(username string, password string) (*model.User, error) { if len(f.Cookie) == 0 || err != nil { return nil, errors.ERR_WRONG_PASSWORD } - return service.SetUser(service.LibraryPrefix, username, password, f.Cookie[0]) + return service.SetUser(service.LibraryPrefix, username, password, f.Cookie[0], nil) } diff --git a/app/service/serviceUniFunction.go b/app/service/serviceUniFunction.go index a9b7f3f..cdcc144 100644 --- a/app/service/serviceUniFunction.go +++ b/app/service/serviceUniFunction.go @@ -34,10 +34,10 @@ func GetUser(prefix string, username string, password string) (*model.User, erro return user, nil } -func SetUser(prefix string, username string, password string, cookie *http.Cookie) (*model.User, error) { - user := model.User{Username: username, Password: password, Session: *cookie} +func SetUser(prefix string, username string, password string, sessionCookie *http.Cookie, routeCookie *http.Cookie) (*model.User, error) { + user := model.User{Username: username, Password: password, Session: *sessionCookie, Route: *routeCookie} userJson, _ := json.Marshal(user) - config.Redis.Set(getRediskey(prefix, username, password), string(userJson), cookie.Expires.Sub(time.Now().Add(time.Minute*5))) + config.Redis.Set(getRediskey(prefix, username, password), string(userJson), sessionCookie.Expires.Sub(time.Now().Add(time.Minute*5))) return &user, nil } diff --git a/app/service/zfService/login.go b/app/service/zfService/login.go index 5b1fcca..77bc1a4 100644 --- a/app/service/zfService/login.go +++ b/app/service/zfService/login.go @@ -37,15 +37,13 @@ func login(username string, password string) (*model.User, error) { if len(f.Cookie) < 1 { return nil, errors.ERR_UNKNOWN_LOGIN_ERROR } - var URL string if strings.Compare(config.Redis.Get("zf_url").String(), "new") == 0 { URL = apis.CAPTCHA_NEW_BREAKER_URL } else { URL = apis.CAPTCHA_BREAKER_URL } - - captcha, err := f.Get(URL + f.Cookie[0].Value) + captcha, err := f.Get(URL + "?session=" + f.Cookie[0].Value + "&route=" + f.Cookie[1].Value) if err != nil { return nil, err } @@ -67,16 +65,20 @@ func login(username string, password string) (*model.User, error) { if strings.Contains(string(s), "用户名或密码不正确") { return nil, errors.ERR_WRONG_PASSWORD } - var cookie *http.Cookie + var sessionCookie *http.Cookie + var routeCookie *http.Cookie for _, v := range f.Cookie { if v.Name == "JSESSIONID" { - cookie = v + sessionCookie = v + } + if v.Name == "route" { + routeCookie = v } } - if cookie == nil { + if sessionCookie == nil { return nil, errors.ERR_UNKNOWN_LOGIN_ERROR } - return service.SetUser(service.ZFPrefix, username, password, cookie) + return service.SetUser(service.ZFPrefix, username, password, sessionCookie, routeCookie) } func loginByOauth(username string, password string) (*model.User, error) { @@ -120,14 +122,18 @@ func loginByOauth(username string, password string) (*model.User, error) { if err != nil { return nil, err } - var cookie *http.Cookie + var sessionCookie *http.Cookie + var routeCookie *http.Cookie for _, v := range f.Cookie { if v.Name == "JSESSIONID" { - cookie = v + sessionCookie = v + } + if v.Name == "route" { + routeCookie = v } } - if cookie == nil { + if sessionCookie == nil { return nil, errors.ERR_UNKNOWN_LOGIN_ERROR } - return service.SetUser(service.ZFPrefix, username, password, cookie) + return service.SetUser(service.ZFPrefix, username, password, sessionCookie, routeCookie) } diff --git a/app/service/zfService/zfService.go b/app/service/zfService/zfService.go index ceb1468..1a19e32 100644 --- a/app/service/zfService/zfService.go +++ b/app/service/zfService/zfService.go @@ -82,6 +82,7 @@ func fetchTermRelatedInfo(stu *model.User, requestUrl, year, term string, examIn f := fetch.Fetch{} f.Init() f.Cookie = append(f.Cookie, &stu.Session) + f.Cookie = append(f.Cookie, &stu.Route) if term == "上" { term = "3" } else if term == "下" { @@ -112,6 +113,7 @@ func GetTrainingPrograms(stu *model.User) ([]byte, error) { f := fetch.Fetch{} f.Init() f.Cookie = append(f.Cookie, &stu.Session) + f.Cookie = append(f.Cookie, &stu.Route) response, err := f.GetRaw(zf.ZfUserInfo()) if err != nil { @@ -135,6 +137,7 @@ func GetEmptyRoomInfo(stu *model.User, year string, term string, campus string, f := fetch.Fetch{} f.Init() f.Cookie = append(f.Cookie, &stu.Session) + f.Cookie = append(f.Cookie, &stu.Route) if term == "上" { term = "3" } else if term == "下" { diff --git a/app/utils/fetch/fetch.go b/app/utils/fetch/fetch.go index 4abdcc7..a732c61 100644 --- a/app/utils/fetch/fetch.go +++ b/app/utils/fetch/fetch.go @@ -2,7 +2,8 @@ package fetch import ( "crypto/tls" - "errors" + "fmt" + errors2 "funnel/app/errors" "io" "net/http" "net/url" @@ -18,7 +19,7 @@ type Fetch struct { func (f *Fetch) InitUnSafe() { f.client = &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, - Timeout: time.Second * 2, + Timeout: time.Second * 20, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }, @@ -28,7 +29,7 @@ func (f *Fetch) InitUnSafe() { func (f *Fetch) Init() { f.client = &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, - Timeout: time.Second * 10, + Timeout: time.Second * 20, } } func (f *Fetch) SkipTlsCheck() { @@ -55,7 +56,7 @@ func (f *Fetch) GetRedirect(url string) (*url.URL, error) { return nil, err } if response.StatusCode != 302 { - return nil, errors.New("network_error") + return nil, errors2.ERR_OAUTH_ERROR } location, err := response.Location() if err != nil { @@ -106,7 +107,9 @@ func (f *Fetch) PostFormRedirect(url string, requestData url.Values) (*url.URL, return nil, err } if response.StatusCode != 302 { - return nil, errors.New("network_error") + fmt.Println(url) + fmt.Println(response.StatusCode) + return nil, errors2.ERR_WRONG_PASSWORD } f.Cookie = cookieMerge(f.Cookie, response.Cookies()) return response.Location()