generated from songquanpeng/go-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c012e80
commit 8d67740
Showing
5 changed files
with
60 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package handler | ||
|
||
import ( | ||
"go-public/common" | ||
"sync" | ||
) | ||
|
||
var ipStore = struct { | ||
sync.RWMutex | ||
ips map[string]bool | ||
}{ | ||
ips: make(map[string]bool), | ||
} | ||
|
||
var whitelistCacheThreshold = 4 | ||
|
||
func isInWhitelist(ip string) bool { | ||
// If whitelist is empty, just return true | ||
if len(common.ServerConfig.Whitelist) == 0 { | ||
return true | ||
} | ||
// If ip is localhost, just return true | ||
if ip == "::1" { | ||
return true | ||
} | ||
// If whitelist is too short, we don't cache it | ||
if len(common.ServerConfig.Whitelist) < whitelistCacheThreshold { | ||
for _, v := range common.ServerConfig.Whitelist { | ||
if v == ip { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
// Check the cache | ||
ipStore.RLock() | ||
if _, ok := ipStore.ips[ip]; ok { | ||
ipStore.RUnlock() | ||
return true | ||
} | ||
ipStore.RUnlock() | ||
// Check the whitelist | ||
for _, v := range common.ServerConfig.Whitelist { | ||
if v == ip { | ||
ipStore.Lock() | ||
ipStore.ips[ip] = true | ||
ipStore.Unlock() | ||
return true | ||
} | ||
} | ||
return false | ||
} |