mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 11:56:53 +00:00
客户端配置,端口白名单等
This commit is contained in:
55
server/tool/utils.go
Normal file
55
server/tool/utils.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/beego"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ports []int
|
||||
|
||||
func init() {
|
||||
p := beego.AppConfig.String("allowPorts")
|
||||
arr := strings.Split(p, ",")
|
||||
for _, v := range arr {
|
||||
fw := strings.Split(v, "-")
|
||||
if len(fw) == 2 {
|
||||
if isPort(fw[0]) && isPort(fw[1]) {
|
||||
start, _ := strconv.Atoi(fw[0])
|
||||
end, _ := strconv.Atoi(fw[1])
|
||||
for i := start; i <= end; i++ {
|
||||
ports = append(ports, i)
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if isPort(v) {
|
||||
p, _ := strconv.Atoi(v)
|
||||
ports = append(ports, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
func isPort(p string) bool {
|
||||
pi, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if pi > 65536 || pi < 1 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func TestServerPort(p int, m string) (b bool) {
|
||||
if len(ports) != 0 {
|
||||
if !common.InIntArr(ports, p) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if m == "udpServer" {
|
||||
b = common.TestUdpPort(p)
|
||||
} else {
|
||||
b = common.TestTcpPort(p)
|
||||
}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user