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] }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-15 07:17:49