Skip to content

Commit

Permalink
white list subs added & modify query
Browse files Browse the repository at this point in the history
  • Loading branch information
Zenon-provalidator committed Aug 17, 2023
1 parent 0d4440f commit c88c959
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions models/DB.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ type ApiUsage struct {
Cnt int `json:"cnt"`
ChainName string `json:"chain_name"`
}

type ApiUsagesCount struct {
UqKey string `json:"uqkey"`
Counts int `json:"counts"`
}
16 changes: 13 additions & 3 deletions models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ func WriteLog(sub string, token string, ip string, method string) {
date := now.Format("2006-01-02 15:04:05")
input := ApiUsage{Sub: sub, Token: token, Ip: ip, Method: method, Date: date, ChainName: os.Getenv("CHAIN_NAME")}
DB.Create(&input)

// Usage count
ym := now.Format("0601")
uqKey := ym + "-" + sub
DB.Exec("INSERT INTO nodereal.api_usages_counts(uqkey) values (?) ON DUPLICATE KEY UPDATE uqkey = ?, counts = counts+1;", uqKey, uqKey)

}

func Count(sub string) int {
apiUsage := ApiUsage{}
DB.Raw("SELECT count(1) AS cnt FROM ( SELECT * FROM nodereal.api_usages WHERE date BETWEEN DATE_ADD(NOW(),INTERVAL -1 MONTH ) AND NOW() ) AS a WHERE a.sub= ?", sub).Scan(&apiUsage)
return apiUsage.Cnt
apiUsagesCount := ApiUsagesCount{}
now := time.Now()
// Usage count
ym := now.Format("0601")
uqKey := ym + "-" + sub
DB.Raw("SELECT counts FROM nodereal.api_usages_counts WHERE uqkey = ?", uqKey).Scan(&apiUsagesCount)
return apiUsagesCount.Counts
}
11 changes: 11 additions & 0 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func AuthorizationMiddleware(c *gin.Context) {

// Without Token
whiteListIps := strings.Split(os.Getenv("WHITE_LIST_IPS"), ",")
whiteListSubs := strings.Split(os.Getenv("WHITE_LIST_SUBS"), ",")

// Ip free pass
for _, ip := range whiteListIps {
Expand Down Expand Up @@ -73,6 +74,14 @@ func AuthorizationMiddleware(c *gin.Context) {
unixTime := now.Unix()
// claims.Exp = 1682038514 // Test

// Token Sub free pass
for _, sub := range whiteListSubs {
if claims.Sub == sub {
log.Logger.Trace.Println("whiteListSubs ", claims.Sub)
c.Next()
return
}
}
if claims.Exp < unixTime {
log.Logger.Error.Println("Token is expired")
log.Logger.Error.Println("UnixTime : ", time.Unix(unixTime, 0))
Expand Down Expand Up @@ -108,6 +117,8 @@ func AuthorizationMiddleware(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"status": http.StatusUnauthorized, "error": "You've already exceeded the limit. (100,000 Requests)"})
c.Abort()
return
} else {
log.Logger.Trace.Println("Valid request!", "sub:", claims.Sub, "(", requestsCnt, ")")
}
// 2. Write usage data Log
models.WriteLog(claims.Sub, tokenString, c.ClientIP(), c.Request.URL.Path)
Expand Down

0 comments on commit c88c959

Please sign in to comment.