Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 11:55:23 11月15日 Update

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.

Tags: Security verification method language code more mechanism knowledge article utility preface security management that is work meeting file article look knowledge point network segment network Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi Shulou Technology macOS Apple