客户端配置,端口白名单等

This commit is contained in:
刘河
2019-02-13 03:54:00 +08:00
parent 59d789d253
commit 44d314515b
34 changed files with 1096 additions and 472 deletions

55
server/tool/utils.go Normal file
View 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
}