diff --git a/backend/app/api/v1/auth.go b/backend/app/api/v1/auth.go index 30f1e6c9f..7dc47b8af 100644 --- a/backend/app/api/v1/auth.go +++ b/backend/app/api/v1/auth.go @@ -8,6 +8,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/middleware" "github.com/1Panel-dev/1Panel/backend/utils/captcha" "github.com/1Panel-dev/1Panel/backend/utils/qqwry" "github.com/gin-gonic/gin" @@ -113,21 +114,12 @@ func (b *BaseApi) Captcha(c *gin.Context) { // @Router /auth/issafety [get] func (b *BaseApi) CheckIsSafety(c *gin.Context) { code := c.DefaultQuery("code", "") - status, err := authService.CheckIsSafety(code) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + isSafe := authService.CheckIsSafety(code) + if !isSafe { + helper.ErrResponse(c, middleware.LoadErrCode("err-entrance")) return } - helper.SuccessWithData(c, status) -} - -func (b *BaseApi) GetResponsePage(c *gin.Context) { - pageCode, err := authService.GetResponsePage() - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - helper.SuccessWithData(c, pageCode) + helper.SuccessWithOutData(c) } // @Tags Auth diff --git a/backend/app/api/v1/helper/helper.go b/backend/app/api/v1/helper/helper.go index 69421a820..aa4afa8fa 100644 --- a/backend/app/api/v1/helper/helper.go +++ b/backend/app/api/v1/helper/helper.go @@ -133,3 +133,8 @@ func CheckBind(req interface{}, c *gin.Context) error { } return nil } + +func ErrResponse(ctx *gin.Context, code int) { + ctx.JSON(code, nil) + ctx.Abort() +} diff --git a/backend/app/service/auth.go b/backend/app/service/auth.go index 118ffd982..7be4668f3 100644 --- a/backend/app/service/auth.go +++ b/backend/app/service/auth.go @@ -19,8 +19,7 @@ import ( type AuthService struct{} type IAuthService interface { - CheckIsSafety(code string) (string, error) - GetResponsePage() (string, error) + CheckIsSafety(code string) bool VerifyCode(code string) (bool, error) Login(c *gin.Context, info dto.Login, entrance string) (*dto.UserLoginInfo, error) LogOut(c *gin.Context) error @@ -173,24 +172,16 @@ func (u *AuthService) VerifyCode(code string) (bool, error) { return hmac.Equal([]byte(setting.Value), []byte(code)), nil } -func (u *AuthService) CheckIsSafety(code string) (string, error) { +func (u *AuthService) CheckIsSafety(code string) bool { status, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance")) if err != nil { - return "", err + return true } if len(status.Value) == 0 { - return "disable", nil + return true } if status.Value == code { - return "pass", nil + return true } - return "unpass", nil -} - -func (u *AuthService) GetResponsePage() (string, error) { - pageCode, err := settingRepo.Get(settingRepo.WithByKey("NoAuthSetting")) - if err != nil { - return "", err - } - return pageCode.Value, nil + return false } diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 3660c7379..26ea7bd0b 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -8,16 +8,14 @@ const ( CodeSuccess = 200 CodeErrBadRequest = 400 CodeErrUnauthorized = 401 - CodeErrUnSafety = 402 - CodeErrForbidden = 403 - CodeErrNotFound = 404 - CodePasswordExpired = 405 CodeAuth = 406 CodeGlobalLoading = 407 - CodeErrIP = 408 - CodeErrDomain = 409 CodeErrInternalServer = 500 - CodeErrHeader = 406 + + CodeErrIP = 310 + CodeErrDomain = 311 + CodeErrEntrance = 312 + CodePasswordExpired = 313 CodeErrXpack = 410 ) diff --git a/backend/init/router/router.go b/backend/init/router/router.go index e694fa713..214cfcc6a 100644 --- a/backend/init/router/router.go +++ b/backend/init/router/router.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" - v1 "github.com/1Panel-dev/1Panel/backend/app/api/v1" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/i18n" "github.com/1Panel-dev/1Panel/backend/middleware" @@ -64,8 +63,6 @@ func Routers() *gin.Engine { PublicGroup.GET("/health", func(c *gin.Context) { c.JSON(200, "ok") }) - baseApi := v1.ApiGroupApp.BaseApi - PublicGroup.GET("/api/v1/respagecode", baseApi.GetResponsePage) PublicGroup.Use(gzip.Gzip(gzip.DefaultCompression)) setWebStatic(PublicGroup) } diff --git a/backend/middleware/bind_domain.go b/backend/middleware/bind_domain.go index bf32af046..2bc97f119 100644 --- a/backend/middleware/bind_domain.go +++ b/backend/middleware/bind_domain.go @@ -1,7 +1,6 @@ package middleware import ( - "errors" "strings" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" @@ -29,7 +28,7 @@ func BindDomain() gin.HandlerFunc { } if domains != status.Value { - helper.ErrorWithDetail(c, constant.CodeErrDomain, constant.ErrTypeInternalServer, errors.New("domain not allowed")) + helper.ErrResponse(c, LoadErrCode("err-domain")) return } c.Next() diff --git a/backend/middleware/helper.go b/backend/middleware/helper.go new file mode 100644 index 000000000..1f5a453ef --- /dev/null +++ b/backend/middleware/helper.go @@ -0,0 +1,42 @@ +package middleware + +import ( + "net/http" + + "github.com/1Panel-dev/1Panel/backend/app/repo" + "github.com/1Panel-dev/1Panel/backend/constant" +) + +func LoadErrCode(errInfo string) int { + settingRepo := repo.NewISettingRepo() + codeVal, err := settingRepo.Get(settingRepo.WithByKey("NoAuthSetting")) + if err != nil { + return 500 + } + + switch codeVal.Value { + case "400": + return http.StatusBadRequest + case "401": + return http.StatusUnauthorized + case "403": + return http.StatusForbidden + case "404": + return http.StatusFound + case "408": + return http.StatusRequestTimeout + case "416": + return http.StatusRequestedRangeNotSatisfiable + default: + if errInfo == "err-ip" { + return constant.CodeErrIP + } + if errInfo == "err-domain" { + return constant.CodeErrDomain + } + if errInfo == "err-entrance" { + return constant.CodeErrEntrance + } + return http.StatusOK + } +} diff --git a/backend/middleware/ip_limit.go b/backend/middleware/ip_limit.go index 1b45fd3f0..8f2e3536b 100644 --- a/backend/middleware/ip_limit.go +++ b/backend/middleware/ip_limit.go @@ -1,7 +1,6 @@ package middleware import ( - "errors" "net" "strings" @@ -35,7 +34,7 @@ func WhiteAllow() gin.HandlerFunc { return } } - helper.ErrorWithDetail(c, constant.CodeErrIP, constant.ErrTypeInternalServer, errors.New("IP address not allowed")) + helper.ErrResponse(c, LoadErrCode("err-ip")) } } diff --git a/backend/middleware/password_expired.go b/backend/middleware/password_expired.go index 9a6753014..389558b44 100644 --- a/backend/middleware/password_expired.go +++ b/backend/middleware/password_expired.go @@ -33,11 +33,11 @@ func PasswordExpired() gin.HandlerFunc { loc, _ := time.LoadLocation(common.LoadTimeZone()) expiredTime, err := time.ParseInLocation("2006-01-02 15:04:05", extime.Value, loc) if err != nil { - helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err) + helper.ErrResponse(c, constant.CodePasswordExpired) return } if time.Now().After(expiredTime) { - helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, nil) + helper.ErrResponse(c, constant.CodePasswordExpired) return } c.Next() diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index 98e9fa877..0f2909a39 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -41,6 +41,7 @@ class RequestHttp { this.service.interceptors.response.use( (response: AxiosResponse) => { + globalStore.errStatus = ''; const { data } = response; if (data.code == ResultEnum.OVERDUE || data.code == ResultEnum.FORBIDDEN) { globalStore.setLogStatus(false); @@ -50,26 +51,6 @@ class RequestHttp { }); return Promise.reject(data); } - if (data.code == ResultEnum.EXPIRED) { - router.push({ name: 'Expired' }); - return data; - } - if (data.code == ResultEnum.ERRIP) { - globalStore.setLogStatus(false); - router.push({ - name: 'entrance', - params: { code: 'err-ip' }, - }); - return Promise.reject(data); - } - if (data.code == ResultEnum.ERRDOMAIN) { - globalStore.setLogStatus(false); - router.push({ - name: 'entrance', - params: { code: 'err-domain' }, - }); - return Promise.reject(data); - } if (data.code == ResultEnum.ERRXPACK) { globalStore.isProductPro = false; window.location.reload(); @@ -94,13 +75,50 @@ class RequestHttp { return data; }, async (error: AxiosError) => { + globalStore.errStatus = ''; const { response } = error; if (error.message.indexOf('timeout') !== -1) MsgError('请求超时!请您稍后重试'); if (response) { - checkStatus( - response.status, - response.data && response.data['message'] ? response.data['message'] : '', - ); + switch (response.status) { + case 310: + globalStore.errStatus = 'err-ip'; + router.push({ + name: 'entrance', + params: { code: globalStore.entrance }, + }); + return; + case 311: + globalStore.errStatus = 'err-domain'; + router.push({ + name: 'entrance', + params: { code: globalStore.entrance }, + }); + return; + case 312: + globalStore.errStatus = 'err-entrance'; + router.push({ + name: 'entrance', + params: { code: globalStore.entrance }, + }); + return; + case 313: + router.push({ name: 'Expired' }); + return; + case 500: + case 400: + case 407: + checkStatus( + response.status, + response.data && response.data['message'] ? response.data['message'] : '', + ); + return; + default: + globalStore.errStatus = 'code-' + response.status; + router.push({ + name: 'entrance', + params: { code: globalStore.entrance }, + }); + } } if (!window.navigator.onLine) router.replace({ path: '/500' }); return Promise.reject(error); diff --git a/frontend/src/api/modules/auth.ts b/frontend/src/api/modules/auth.ts index b0f5e206f..937344d41 100644 --- a/frontend/src/api/modules/auth.ts +++ b/frontend/src/api/modules/auth.ts @@ -21,10 +21,6 @@ export const checkIsSafety = (code: string) => { return http.get(`/auth/issafety?code=${code}`); }; -export const getResponsePage = () => { - return http.get(`/respagecode`); -}; - export const checkIsDemo = () => { return http.get('/auth/demo'); }; diff --git a/frontend/src/components/error-message/error_code.vue b/frontend/src/components/error-message/error_code.vue index ab1e2c6e1..d5c1ea226 100644 --- a/frontend/src/components/error-message/error_code.vue +++ b/frontend/src/components/error-message/error_code.vue @@ -1,10 +1,16 @@ @@ -12,37 +18,24 @@ const props = defineProps({ code: String, }); +const loadErrInfo = () => { + switch (props.code) { + case '401': + return '401 Unauthorized'; + case '403': + return '403 Forbidden'; + case '404': + return '403 Not Found'; + case '408': + return '408 Request Timeout'; + case '416': + return '408 Requested Not Satisfiable'; + } +}; diff --git a/frontend/src/enums/http-enum.ts b/frontend/src/enums/http-enum.ts index e81dc11d4..aa9c6f640 100644 --- a/frontend/src/enums/http-enum.ts +++ b/frontend/src/enums/http-enum.ts @@ -1,14 +1,15 @@ export enum ResultEnum { SUCCESS = 200, + ERRIP = 310, + ERRDOMAIN = 311, + UNSAFETY = 312, + EXPIRED = 313, + ERROR = 500, OVERDUE = 401, - UNSAFETY = 402, FORBIDDEN = 403, - EXPIRED = 405, ERRAUTH = 406, ERRGLOBALLOADDING = 407, - ERRIP = 408, - ERRDOMAIN = 409, ERRXPACK = 410, TIMEOUT = 20000, TYPE = 'success', diff --git a/frontend/src/store/interface/index.ts b/frontend/src/store/interface/index.ts index 1328a8190..31c554261 100644 --- a/frontend/src/store/interface/index.ts +++ b/frontend/src/store/interface/index.ts @@ -34,6 +34,8 @@ export interface GlobalState { isProductPro: boolean; productProExpires: number; + + errStatus: string; } export interface MenuState { diff --git a/frontend/src/store/modules/global.ts b/frontend/src/store/modules/global.ts index dc82d7011..7874d6a1e 100644 --- a/frontend/src/store/modules/global.ts +++ b/frontend/src/store/modules/global.ts @@ -38,6 +38,8 @@ const GlobalStore = defineStore({ isProductPro: false, productProExpires: 0, + + errStatus: '', }), getters: { isDarkTheme: (state) => state.themeConfig.theme === 'dark' || state.themeConfig.theme === 'dark-gold', diff --git a/frontend/src/views/login/entrance/index.vue b/frontend/src/views/login/entrance/index.vue index 5db31c759..0bd82958f 100644 --- a/frontend/src/views/login/entrance/index.vue +++ b/frontend/src/views/login/entrance/index.vue @@ -18,22 +18,20 @@
-
-
- -
-
- -
-
- -
-
- -
+
+
-
- +
+ +
+
+ +
+
+ +
+
+
@@ -41,7 +39,7 @@