Skip to content

Commit

Permalink
鉴权错误时不再返回200状态码,而是返回4xx
Browse files Browse the repository at this point in the history
  • Loading branch information
Raobee committed Jan 24, 2022
1 parent ca8cdd4 commit 3521f9b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,35 @@ func check(response http.ResponseWriter, req *http.Request) {

// 获取传入域名
if len(req.Form.Get("domain")) == 0 {
response.WriteHeader(400)
fmt.Fprintf(response, "No domain specified.")
return
}
domain = req.Form.Get("domain")
// 获取传入文件名
if len(req.Form.Get("file")) == 0 {
response.WriteHeader(400)
fmt.Fprintf(response, "No file specified.")
return
}
file = req.Form.Get("file")
// 获取传入签名
if len(req.Form.Get("sign")) == 0 {
response.WriteHeader(400)
fmt.Fprintf(response, "No sign specified.")
return
}
sign = req.Form.Get("sign")
// 获取传入验证码
if len(req.Form.Get("checksum")) == 0 {
response.WriteHeader(400)
fmt.Fprintf(response, "No checksum specified.")
return
}
checksum = req.Form.Get("checksum")
// 获取传入时间戳
if len(req.Form.Get("t")) == 0 {
response.WriteHeader(400)
fmt.Fprintf(response, "No timestamp specified.")
return
}
Expand All @@ -162,6 +167,7 @@ func check(response http.ResponseWriter, req *http.Request) {
if err != nil {
fmt.Println("Access from IP:", ip)
fmt.Println("Incoming illegal timestamp:", t)
response.WriteHeader(403)
fmt.Fprintf(response, "Timestamp not allowed.")
return
}
Expand All @@ -170,13 +176,15 @@ func check(response http.ResponseWriter, req *http.Request) {
if expireTime < -timeRange {
fmt.Println("Access from IP:", ip)
fmt.Println("Incoming illegal timestamp:", expireTime)
response.WriteHeader(403)
fmt.Fprintf(response, "Timestamp not allowed.")
return
}
// 校验时间戳是否过期
if expireTime > timeRange {
fmt.Println("Access from IP:", ip)
fmt.Println("Incoming expired access:", expireTime)
response.WriteHeader(403)
fmt.Fprintf(response, "Timestamp expired.")
return
}
Expand All @@ -200,6 +208,7 @@ func check(response http.ResponseWriter, req *http.Request) {
// 检测到重放请求
fmt.Println("Access from IP:", ip)
fmt.Println("Incoming repeat access:", checksum)
response.WriteHeader(403)
fmt.Fprintf(response, "Repeat access.")
return
}
Expand Down Expand Up @@ -227,13 +236,15 @@ func check(response http.ResponseWriter, req *http.Request) {
// 获取的域名不存在
fmt.Println("Access from IP:", ip)
fmt.Println("Incoming illegal domain:", domain)
response.WriteHeader(404)
fmt.Fprintf(response, "Domain not exist.")
return
}
if !checkFile {
// 获取的文件不存在
fmt.Println("Access from IP:", ip)
fmt.Println("Incoming illegal filename:", file)
response.WriteHeader(404)
fmt.Fprintf(response, "File not exist.")
return
}
Expand All @@ -246,6 +257,7 @@ func check(response http.ResponseWriter, req *http.Request) {
// 签名错误
fmt.Println("Access from IP:", ip)
fmt.Println("Incoming unauthorized access:", sign)
response.WriteHeader(401)
fmt.Fprintf(response, "Unauthorized access.")
}
}

0 comments on commit 3521f9b

Please # to comment.