兼容历史单端口配置

This commit is contained in:
huweishan 2024-04-09 10:29:54 +08:00
parent 4f56ea49c3
commit 24e30509e4
No known key found for this signature in database
GPG Key ID: 955238B53F458D37
2 changed files with 25 additions and 30 deletions

View File

@ -5,10 +5,9 @@ import (
"fmt" "fmt"
"net" "net"
"regexp" "regexp"
"strings"
"strconv" "strconv"
"strings"
"time" "time"
"reflect"
"github.com/bjdgyc/anylink/base" "github.com/bjdgyc/anylink/base"
"golang.org/x/text/language" "golang.org/x/text/language"
@ -28,7 +27,7 @@ type GroupLinkAcl struct {
// 自上而下匹配 默认 allow * * // 自上而下匹配 默认 allow * *
Action string `json:"action"` // allow、deny Action string `json:"action"` // allow、deny
Val string `json:"val"` Val string `json:"val"`
Port interface{} `json:"port"` Port interface{} `json:"port"` //兼容单端口历史数据类型uint16
Ports map[uint16]int8 `json:"ports"` Ports map[uint16]int8 `json:"ports"`
IpNet *net.IPNet `json:"ip_net"` IpNet *net.IPNet `json:"ip_net"`
Note string `json:"note"` Note string `json:"note"`
@ -45,13 +44,6 @@ type GroupNameId struct {
Name string `json:"name"` Name string `json:"name"`
} }
type PortData struct {
PortFrom uint16 `json:"port_from"`
PortTo uint16 `json:"port_to"`
}
// type Group struct { // type Group struct {
// Id int `json:"id" xorm:"pk autoincr not null"` // Id int `json:"id" xorm:"pk autoincr not null"`
// Name string `json:"name" xorm:"varchar(60) not null unique"` // Name string `json:"name" xorm:"varchar(60) not null unique"`
@ -172,14 +164,14 @@ func SetGroup(g *Group) error {
} }
v.IpNet = ipNet v.IpNet = ipNet
port:=""; port := ""
//base.Debug("v.port:",v.Port,v.Ports,reflect.TypeOf(v.Port).Name()) switch vp := v.Port.(type) {
switch v := v.Port.(type) {
case float64: case float64:
port = strconv.Itoa(int(v)) port = strconv.Itoa(int(vp))
case string: case string:
port = v port = vp
} }
if regexp.MustCompile(`^\d{1,5}(-\d{1,5})?(,\d{1,5}(-\d{1,5})?)*$`).MatchString(port) { if regexp.MustCompile(`^\d{1,5}(-\d{1,5})?(,\d{1,5}(-\d{1,5})?)*$`).MatchString(port) {
ports := map[uint16]int8{} ports := map[uint16]int8{}
for _, p := range strings.Split(port, ",") { for _, p := range strings.Split(port, ",") {
@ -187,14 +179,14 @@ func SetGroup(g *Group) error {
continue continue
} }
if regexp.MustCompile(`^\d{1,5}-\d{1,5}$`).MatchString(p) { if regexp.MustCompile(`^\d{1,5}-\d{1,5}$`).MatchString(p) {
rp := strings.Split(p, "-"); rp := strings.Split(p, "-")
portfrom, err := strconv.Atoi(rp[0]) portfrom, err := strconv.Atoi(rp[0])
if err != nil { if err != nil {
return errors.New("端口:"+rp[0]+" 格式错误, " + err.Error()) return errors.New("端口:" + rp[0] + " 格式错误, " + err.Error())
} }
portto, err := strconv.Atoi(rp[1]) portto, err := strconv.Atoi(rp[1])
if err != nil { if err != nil {
return errors.New("端口:"+rp[1]+" 格式错误, " + err.Error()) return errors.New("端口:" + rp[1] + " 格式错误, " + err.Error())
} }
for i := portfrom; i <= portto; i++ { for i := portfrom; i <= portto; i++ {
ports[uint16(i)] = 1 ports[uint16(i)] = 1
@ -203,7 +195,7 @@ func SetGroup(g *Group) error {
} else { } else {
port, err := strconv.Atoi(p) port, err := strconv.Atoi(p)
if err != nil { if err != nil {
return errors.New("端口:"+p+" 格式错误, " + err.Error()) return errors.New("端口:" + p + " 格式错误, " + err.Error())
} }
ports[uint16(port)] = 1 ports[uint16(port)] = 1
} }
@ -211,7 +203,7 @@ func SetGroup(g *Group) error {
v.Ports = ports v.Ports = ports
linkAcl = append(linkAcl, v) linkAcl = append(linkAcl, v)
} else { } else {
return errors.New("端口: "+port+" 格式错误,请用逗号分隔的端口,比如: 22,80,443 连续端口用-,比如:1234-5678") return errors.New("端口: " + port + " 格式错误,请用逗号分隔的端口,比如: 22,80,443 连续端口用-,比如:1234-5678")
} }
} }

View File

@ -88,9 +88,12 @@ func checkLinkAcl(group *dbdata.Group, pl *sessdata.Payload) bool {
for _, v := range group.LinkAcl { for _, v := range group.LinkAcl {
// 循环判断ip和端口 // 循环判断ip和端口
if v.IpNet.Contains(ipDst) { if v.IpNet.Contains(ipDst) {
// 放行允许ip的ping // 放行允许ip的ping
if(v.Ports==nil || len(v.Ports)==0){ if v.Ports == nil || len(v.Ports) == 0 {
if v.Port==ipPort || v.Port==0 || ipProto == waterutil.ICMP { //单端口历史数据兼容
port := uint16(v.Port.(float64))
if port == ipPort || port == 0 || ipProto == waterutil.ICMP {
if v.Action == dbdata.Allow { if v.Action == dbdata.Allow {
return true return true
} else { } else {
@ -98,7 +101,7 @@ func checkLinkAcl(group *dbdata.Group, pl *sessdata.Payload) bool {
} }
} }
} else { } else {
if dbdata.ContainsInPorts( v.Ports , ipPort) || dbdata.ContainsInPorts( v.Ports , 0) || ipProto == waterutil.ICMP { if dbdata.ContainsInPorts(v.Ports, ipPort) || dbdata.ContainsInPorts(v.Ports, 0) || ipProto == waterutil.ICMP {
if v.Action == dbdata.Allow { if v.Action == dbdata.Allow {
return true return true
} else { } else {