Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of simple ip security verification in Go language RPC Authorization

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about the Go language RPC Authorization for simple ip security verification method is how, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

When writing network services, we should always consider the security mechanism. Judging ip and network segment is the simplest verification mechanism. After that, I want to do a security verification mechanism similar to registration, which can not only reduce the trouble of configuration files, but also manage security well.

Go directly to the code:

The code is as follows:

Package main

Import ("net", "fmt", "time", "strings") func main () {IP_ARRAY: = "192.168.1.234192.168.1.47192.168.2.0 if err 28" servPort:= ": 7272" ltemery err: = net.Listen ("tcp", servPort) if err! = nil {fmt.Printf ("Listen is error") return} allowList: = strings.Split (IP_ARRAY, " ") for {conn,err:=l.Accept () if err! = nil {fmt.Printf (" start connect is error ") return} ipAddr:=conn.RemoteAddr () Addr:= strings.Split (ipAddr.String () ":") rAddr: = net.ParseIP (Addr [0]) var authorized bool = false for v: = range allowList {_ ipNet Err: = net.ParseCIDR (allowList [v]) if err! = nil {fmt.Printf ("parse ip net error") ipHost: = net.ParseIP (allowList [v]) if ipHost! = nil {if ipHost.Equal (rAddr) {authorized = true } else {fmt.Printf ("ip list error")}} else {fmt.Printf ("Contains ip") if ipNet.Contains (rAddr) {authorized = true}} } if authorized = = true {curTime:=time.Now () fmt.Printf (curTime.Format ("2006-01-02 15:04:05")) conn.Write ([] byte (curTime.Format ("2006-01-02 15:04:05") time.Sleep (10)} else {conn.Close ()} these are the methods of simple ip security verification in Go language RPC Authorization. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report