-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathsecurity.go
More file actions
181 lines (167 loc) · 4.13 KB
/
security.go
File metadata and controls
181 lines (167 loc) · 4.13 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
package security
import (
"encoding/base64"
"fmt"
"github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/app/service"
"github.com/1Panel-dev/1Panel/core/cmd/server/res"
"github.com/1Panel-dev/1Panel/core/cmd/server/web"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global"
"github.com/1Panel-dev/1Panel/core/utils/common"
"github.com/gin-gonic/gin"
"net/http"
"regexp"
"strconv"
"strings"
)
func HandleNotRoute(c *gin.Context) bool {
if !checkBindDomain(c) {
HandleNotSecurity(c, "err_domain")
return false
}
if !checkIPLimit(c) {
HandleNotSecurity(c, "err_ip_limit")
return false
}
if checkFrontendPath(c) {
ToIndexHtml(c)
return false
}
if isEntrancePath(c) {
ToIndexHtml(c)
return false
}
return true
}
func CheckSecurity(c *gin.Context) bool {
if !checkEntrance(c) && !checkSession(c) {
HandleNotSecurity(c, "")
return false
}
if !checkBindDomain(c) {
HandleNotSecurity(c, "err_domain")
return false
}
if !checkIPLimit(c) {
HandleNotSecurity(c, "err_ip_limit")
return false
}
return true
}
func ToIndexHtml(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
c.Writer.WriteHeader(http.StatusOK)
data, err := web.IndexHtml.ReadFile("index.html")
if err != nil {
c.String(http.StatusInternalServerError, "index.html not found")
return
}
_, _ = c.Writer.Write(data)
c.Writer.Flush()
}
func isEntrancePath(c *gin.Context) bool {
entrance := service.NewIAuthService().GetSecurityEntrance()
if entrance != "" && strings.TrimSuffix(c.Request.URL.Path, "/") == "/"+entrance {
return true
}
return false
}
func checkEntrance(c *gin.Context) bool {
authService := service.NewIAuthService()
entrance := authService.GetSecurityEntrance()
if entrance == "" {
return true
}
cookieValue, err := c.Cookie("SecurityEntrance")
if err != nil {
return false
}
entranceValue, err := base64.StdEncoding.DecodeString(cookieValue)
if err != nil {
return false
}
return string(entranceValue) == entrance
}
func HandleNotSecurity(c *gin.Context, resType string) {
resPage, err := service.NewIAuthService().GetResponsePage()
if err != nil {
c.String(http.StatusInternalServerError, "Internal Server Error")
return
}
if resPage == "444" {
c.String(444, "")
return
}
file := fmt.Sprintf("html/%s.html", resPage)
if resPage == "200" && resType != "" {
file = fmt.Sprintf("html/200_%s.html", resType)
}
data, err := res.ErrorMsg.ReadFile(file)
if err != nil {
c.String(http.StatusInternalServerError, "Internal Server Error")
return
}
statusCode, err := strconv.Atoi(resPage)
if err != nil {
c.String(http.StatusInternalServerError, "Internal Server Error")
return
}
c.Data(statusCode, "text/html; charset=utf-8", data)
}
func isFrontendPath(c *gin.Context) bool {
reqUri := strings.TrimSuffix(c.Request.URL.Path, "/")
if _, ok := constant.WebUrlMap[reqUri]; ok {
return true
}
for _, route := range constant.DynamicRoutes {
if match, _ := regexp.MatchString(route, reqUri); match {
return true
}
}
return false
}
func checkFrontendPath(c *gin.Context) bool {
if !isFrontendPath(c) {
return false
}
authService := service.NewIAuthService()
if authService.GetSecurityEntrance() != "" {
return authService.IsLogin(c)
}
return true
}
func checkBindDomain(c *gin.Context) bool {
settingRepo := repo.NewISettingRepo()
status, _ := settingRepo.Get(repo.WithByKey("BindDomain"))
if len(status.Value) == 0 {
return true
}
domains := c.Request.Host
parts := strings.Split(c.Request.Host, ":")
if len(parts) > 0 {
domains = parts[0]
}
return domains == status.Value
}
func checkIPLimit(c *gin.Context) bool {
settingRepo := repo.NewISettingRepo()
status, _ := settingRepo.Get(repo.WithByKey("AllowIPs"))
if len(status.Value) == 0 {
return true
}
clientIP := c.ClientIP()
for _, ip := range strings.Split(status.Value, ",") {
if len(ip) == 0 {
continue
}
if ip == clientIP || (strings.Contains(ip, "/") && common.CheckIpInCidr(ip, clientIP)) {
return true
}
}
return false
}
func checkSession(c *gin.Context) bool {
_, err := global.SESSION.Get(c)
return err == nil
}