Skip to content

Commit 3521f9b

Browse files
committed
鉴权错误时不再返回200状态码,而是返回4xx
1 parent ca8cdd4 commit 3521f9b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,30 +128,35 @@ func check(response http.ResponseWriter, req *http.Request) {
128128

129129
// 获取传入域名
130130
if len(req.Form.Get("domain")) == 0 {
131+
response.WriteHeader(400)
131132
fmt.Fprintf(response, "No domain specified.")
132133
return
133134
}
134135
domain = req.Form.Get("domain")
135136
// 获取传入文件名
136137
if len(req.Form.Get("file")) == 0 {
138+
response.WriteHeader(400)
137139
fmt.Fprintf(response, "No file specified.")
138140
return
139141
}
140142
file = req.Form.Get("file")
141143
// 获取传入签名
142144
if len(req.Form.Get("sign")) == 0 {
145+
response.WriteHeader(400)
143146
fmt.Fprintf(response, "No sign specified.")
144147
return
145148
}
146149
sign = req.Form.Get("sign")
147150
// 获取传入验证码
148151
if len(req.Form.Get("checksum")) == 0 {
152+
response.WriteHeader(400)
149153
fmt.Fprintf(response, "No checksum specified.")
150154
return
151155
}
152156
checksum = req.Form.Get("checksum")
153157
// 获取传入时间戳
154158
if len(req.Form.Get("t")) == 0 {
159+
response.WriteHeader(400)
155160
fmt.Fprintf(response, "No timestamp specified.")
156161
return
157162
}
@@ -162,6 +167,7 @@ func check(response http.ResponseWriter, req *http.Request) {
162167
if err != nil {
163168
fmt.Println("Access from IP:", ip)
164169
fmt.Println("Incoming illegal timestamp:", t)
170+
response.WriteHeader(403)
165171
fmt.Fprintf(response, "Timestamp not allowed.")
166172
return
167173
}
@@ -170,13 +176,15 @@ func check(response http.ResponseWriter, req *http.Request) {
170176
if expireTime < -timeRange {
171177
fmt.Println("Access from IP:", ip)
172178
fmt.Println("Incoming illegal timestamp:", expireTime)
179+
response.WriteHeader(403)
173180
fmt.Fprintf(response, "Timestamp not allowed.")
174181
return
175182
}
176183
// 校验时间戳是否过期
177184
if expireTime > timeRange {
178185
fmt.Println("Access from IP:", ip)
179186
fmt.Println("Incoming expired access:", expireTime)
187+
response.WriteHeader(403)
180188
fmt.Fprintf(response, "Timestamp expired.")
181189
return
182190
}
@@ -200,6 +208,7 @@ func check(response http.ResponseWriter, req *http.Request) {
200208
// 检测到重放请求
201209
fmt.Println("Access from IP:", ip)
202210
fmt.Println("Incoming repeat access:", checksum)
211+
response.WriteHeader(403)
203212
fmt.Fprintf(response, "Repeat access.")
204213
return
205214
}
@@ -227,13 +236,15 @@ func check(response http.ResponseWriter, req *http.Request) {
227236
// 获取的域名不存在
228237
fmt.Println("Access from IP:", ip)
229238
fmt.Println("Incoming illegal domain:", domain)
239+
response.WriteHeader(404)
230240
fmt.Fprintf(response, "Domain not exist.")
231241
return
232242
}
233243
if !checkFile {
234244
// 获取的文件不存在
235245
fmt.Println("Access from IP:", ip)
236246
fmt.Println("Incoming illegal filename:", file)
247+
response.WriteHeader(404)
237248
fmt.Fprintf(response, "File not exist.")
238249
return
239250
}
@@ -246,6 +257,7 @@ func check(response http.ResponseWriter, req *http.Request) {
246257
// 签名错误
247258
fmt.Println("Access from IP:", ip)
248259
fmt.Println("Incoming unauthorized access:", sign)
260+
response.WriteHeader(401)
249261
fmt.Fprintf(response, "Unauthorized access.")
250262
}
251263
}

0 commit comments

Comments
 (0)