feat: Add Return Pages for Unauthorized Domains and IPs (#7306)

pull/7308/head
zhengkunwang 2024-12-10 11:58:32 +08:00 committed by GitHub
parent dbe62082ff
commit db7180a761
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 137 additions and 178 deletions

View File

@ -3,6 +3,7 @@ package helper
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/cmd/server/res"
"net/http" "net/http"
"strconv" "strconv"
@ -138,3 +139,23 @@ func ErrResponse(ctx *gin.Context, code int) {
ctx.JSON(code, nil) ctx.JSON(code, nil)
ctx.Abort() ctx.Abort()
} }
func ErrWithHtml(ctx *gin.Context, code int, scope string) {
if code == 444 {
ctx.String(444, "")
ctx.Abort()
return
}
file := fmt.Sprintf("html/%d.html", code)
if code == 200 && scope != "" {
file = fmt.Sprintf("html/200_%s.html", scope)
}
data, err := res.ErrorMsg.ReadFile(file)
if err != nil {
ctx.String(http.StatusInternalServerError, "Internal Server Error")
ctx.Abort()
return
}
ctx.Data(code, "text/html; charset=utf-8", data)
ctx.Abort()
}

View File

@ -156,6 +156,9 @@ func Routers() *gin.Engine {
Router.Use(middleware.DemoHandle()) Router.Use(middleware.DemoHandle())
} }
Router.Use(middleware.WhiteAllow())
Router.Use(middleware.BindDomain())
Router.NoRoute(func(c *gin.Context) { Router.NoRoute(func(c *gin.Context) {
if checkFrontendPath(c) { if checkFrontendPath(c) {
toIndexHtml(c) toIndexHtml(c)
@ -182,8 +185,7 @@ func Routers() *gin.Engine {
setWebStatic(PublicGroup) setWebStatic(PublicGroup)
} }
PrivateGroup := Router.Group("/api/v1") PrivateGroup := Router.Group("/api/v1")
PrivateGroup.Use(middleware.WhiteAllow())
PrivateGroup.Use(middleware.BindDomain())
PrivateGroup.Use(middleware.GlobalLoading()) PrivateGroup.Use(middleware.GlobalLoading())
for _, router := range rou.RouterGroupApp { for _, router := range rou.RouterGroupApp {
router.InitRouter(PrivateGroup) router.InitRouter(PrivateGroup)

View File

@ -1,7 +1,6 @@
package middleware package middleware
import ( import (
"errors"
"strings" "strings"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
@ -30,11 +29,7 @@ func BindDomain() gin.HandlerFunc {
if domains != status.Value { if domains != status.Value {
code := LoadErrCode() code := LoadErrCode()
if code != 200 { helper.ErrWithHtml(c, code, "err_domain")
helper.ErrResponse(c, code)
return
}
helper.ErrorWithDetail(c, constant.CodeErrDomain, constant.ErrTypeInternalServer, errors.New("domain not allowed"))
return return
} }
c.Next() c.Next()

View File

@ -1,7 +1,6 @@
package middleware package middleware
import ( import (
"errors"
"net" "net"
"strings" "strings"
@ -36,11 +35,7 @@ func WhiteAllow() gin.HandlerFunc {
} }
} }
code := LoadErrCode() code := LoadErrCode()
if code != 200 { helper.ErrWithHtml(c, code, "ip_limit")
helper.ErrResponse(c, code)
return
}
helper.ErrorWithDetail(c, constant.CodeErrIP, constant.ErrTypeInternalServer, errors.New("IP address not allowed"))
} }
} }

View File

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Access Temporarily Unavailable</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f9f9f9;
margin: 0;
padding: 0;
color: #333;
}
.container {
max-width: 600px;
margin: 50px auto;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
.icon img {
width: 100px;
height: auto;
}
h1 {
font-size: 24px;
color: #555;
}
p {
font-size: 16px;
color: #666;
line-height: 1.5;
}
.command {
font-family: monospace;
background: #f0f0f0;
padding: 5px 10px;
border-radius: 4px;
display: inline-block;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Access Temporarily Unavailable</h1>
<p>The current environment has enabled domain name binding.</p>
<p>You can enter the following command in the SSH terminal to reset the binding information:</p>
<p class="command">1pctl rest domain</p>
</div>
</body>
</html>

View File

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Access Temporarily Unavailable</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f9f9f9;
margin: 0;
padding: 0;
color: #333;
}
.container {
max-width: 600px;
margin: 50px auto;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
.icon img {
width: 100px;
height: auto;
}
h1 {
font-size: 24px;
color: #555;
}
p {
font-size: 16px;
color: #666;
line-height: 1.5;
}
.command {
font-family: monospace;
background: #f0f0f0;
padding: 5px 10px;
border-radius: 4px;
display: inline-block;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Access Temporarily Unavailable</h1>
<p>The current environment has enabled authorized IP access.</p>
<p>You can enter the following command in the SSH terminal to reset the binding information:</p>
<p class="command">1pctl reset ips</p>
</div>
</body>
</html>

View File

@ -1,55 +0,0 @@
<template>
<div class="not-container">
<img src="@/assets/images/unsafe.svg" class="not-img" alt="404" />
<div class="not-detail">
<h2>{{ $t('commons.login.notSafe') }}</h2>
<h4>{{ $t('commons.login.errDomain1') }}</h4>
<div>
<h4>{{ $t('commons.login.errHelper') }} 1pctl reset domain</h4>
<div class="float-left cursor-pointer">
<el-icon color="#409EFC" class="ml-1.5 mt-8" :size="18" @click="copyText('1pctl reset domain')">
<DocumentCopy />
</el-icon>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts" name="404">
import { copyText } from '@/utils/util';
</script>
<style scoped lang="scss">
.not-container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
.not-img {
margin-top: 300px;
}
.not-detail {
margin-top: 300px;
display: flex;
flex-direction: column;
h2,
h4 {
padding: 0;
margin: 0;
}
h2 {
font-size: 60px;
color: #434e59;
}
h4 {
margin: 30px 0 20px;
float: left;
font-size: 19px;
font-weight: normal;
color: #848587;
}
}
}
</style>

View File

@ -1,54 +0,0 @@
<template>
<div class="not-container">
<img src="@/assets/images/unsafe.svg" class="not-img" alt="404" />
<div class="not-detail">
<h2>{{ $t('commons.login.notSafe') }}</h2>
<h4>{{ $t('commons.login.errIP1') }}</h4>
<div>
<h4>{{ $t('commons.login.errHelper') }} 1pctl reset ips</h4>
<div class="float-left cursor-pointer">
<el-icon color="#409EFC" class="ml-1.5 mt-8" :size="18" @click="copyText('1pctl reset ips')">
<DocumentCopy />
</el-icon>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts" name="404">
import { copyText } from '@/utils/util';
</script>
<style scoped lang="scss">
.not-container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
.not-img {
margin-top: 300px;
}
.not-detail {
margin-top: 300px;
display: flex;
flex-direction: column;
h2,
h4 {
padding: 0;
margin: 0;
}
h2 {
font-size: 60px;
color: #434e59;
}
h4 {
margin: 30px 0 20px;
float: left;
font-size: 19px;
font-weight: normal;
color: #848587;
}
}
}
</style>

View File

@ -1,55 +0,0 @@
<template>
<div class="not-container">
<img src="@/assets/images/unsafe.svg" class="not-img" alt="404" />
<div class="not-detail">
<h2>{{ $t('commons.login.notSafe') }}</h2>
<h4>{{ $t('commons.login.safeEntrance1') }}</h4>
<div>
<h4>{{ $t('commons.login.safeEntrance2') }}</h4>
<div class="float-left cursor-pointer">
<el-icon color="#409EFC" class="ml-1.5 mt-8" :size="18" @click="copyText('1pctl user-info')">
<DocumentCopy />
</el-icon>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts" name="404">
import { copyText } from '@/utils/util';
</script>
<style scoped lang="scss">
.not-container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
.not-img {
margin-top: 300px;
}
.not-detail {
margin-top: 300px;
display: flex;
flex-direction: column;
h2,
h4 {
padding: 0;
margin: 0;
}
h2 {
font-size: 60px;
color: #434e59;
}
h4 {
margin: 30px 0 20px;
float: left;
font-size: 19px;
font-weight: normal;
color: #848587;
}
}
}
</style>