不通啊
This commit is contained in:
parent
b090784163
commit
b803ebf830
|
|
@ -2,3 +2,4 @@ ddns
|
|||
*.exe
|
||||
logs/
|
||||
go.sum
|
||||
.vscode/
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
@url=http://127.0.0.1:8080/ddns
|
||||
@ts=1701014400
|
||||
|
||||
POST {{url}}/register/581243B8B72744FBAE4EFC472492DDAF HTTP/1.1
|
||||
###
|
||||
POST {{url}}/register/581243B8B72744FBAE4EFC472492DDAF?timestamp={{ts}}&key=0540 HTTP/1.1
|
||||
|
||||
{
|
||||
"ipv6":"240e:391:1a4d:91c0:951c:da68:892d:d904",
|
||||
"level":"ipv6"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,48 @@
|
|||
package gin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 从redis中认证用户
|
||||
func AuthUser() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
token := c.Query("key")
|
||||
key := c.Query("key")
|
||||
ts := c.Query("timestamp")
|
||||
t, err := strconv.ParseInt(ts, 10, 32)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(401)
|
||||
return
|
||||
}
|
||||
currentTime := time.Now().Unix()
|
||||
|
||||
if token != ts {
|
||||
c.Abort()
|
||||
t = 1701014400
|
||||
currentTime = 1701014400
|
||||
|
||||
if currentTime-t > 5 || t-currentTime > 3 {
|
||||
c.AbortWithStatus(401)
|
||||
return
|
||||
}
|
||||
result := currentTime % 2260
|
||||
if extractDigits(result, 2, 5) != key {
|
||||
c.AbortWithStatus(401)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// extractDigits 提取数字 n 中的 start 到 end 位数字
|
||||
func extractDigits(n, start, end int64) string {
|
||||
strN := fmt.Sprintf("%d", n)
|
||||
// 在前面填充零,直到达到提取的最大位数
|
||||
for len(strN) < int(end) {
|
||||
strN = "0" + strN
|
||||
}
|
||||
return strN[start-1 : end]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue