前端之家收集整理的这篇文章主要介绍了
falcon-open 问题解决小扩展,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main
import (
"fmt"
"net"
"strings"
"sync"
)
type List struct {
Lock sync.Mutex
IPList map[string]bool
}
var CheckList *List = NewList()
func main() {
for _,k := range []string{"127.0.0.1","192.168.10.1","172.18.80.1"} {
CheckList.Add(k)
}
conn,_ := net.Dial("tcp","127.0.0.1:1789")
if !CheckList.IsExist(conn) {
fmt.Println("没有在白名单")
conn.Close()
return
}
}
func NewList() *List {
return &List{IPList: make(map[string]bool)}
}
func (self *List) Add(s string) {
if self.IPList[s] {
return
}
self.Lock.Lock()
self.IPList[s] = true
self.Lock.Unlock()
}
func (self *List) IsExist(conn net.Conn) bool {
ip := strings.Split(conn.RemoteAddr().String(),":")[0]
return self.IPList[ip]
}