更好地支持IPv6

This commit is contained in:
GoEdgeLab
2024-04-06 10:21:52 +08:00
parent 3418749f36
commit 511f248473
10 changed files with 63 additions and 57 deletions

View File

@@ -7,8 +7,8 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/zero"
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
@@ -520,14 +520,14 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
// keyword
if len(ip) > 0 {
// TODO 支持IP范围
// TODO 支持IPv6范围
if tableQuery.hasRemoteAddrField {
// IP格式
if strings.Contains(ip, ",") || strings.Contains(ip, "-") {
rangeConfig, err := shared.ParseIPRange(ip)
if err == nil {
if len(rangeConfig.IPFrom) > 0 && len(rangeConfig.IPTo) > 0 {
query.Between("INET_ATON(remoteAddr)", utils.IP2Long(rangeConfig.IPFrom), utils.IP2Long(rangeConfig.IPTo))
query.Between("INET_ATON(remoteAddr)", iputils.ToLong(rangeConfig.IPFrom), iputils.ToLong(rangeConfig.IPTo))
}
}
} else {
@@ -580,7 +580,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
if len(pieces) == 1 || len(pieces[1]) == 0 || pieces[0] == pieces[1] {
query.Attr("remoteAddr", pieces[0])
} else {
query.Between("INET_ATON(remoteAddr)", utils.IP2Long(pieces[0]), utils.IP2Long(pieces[1]))
query.Between("INET_ATON(remoteAddr)", iputils.ToLong(pieces[0]), iputils.ToLong(pieces[1]))
}
} else if statusRangeReg.MatchString(keyword) { // status:200-400
isSpecialKeyword = true

View File

@@ -5,7 +5,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
_ "github.com/go-sql-driver/mysql"
@@ -263,8 +263,15 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
op.ListId = listId
op.IpFrom = ipFrom
op.IpTo = ipTo
op.IpFromLong = utils.IP2Long(ipFrom)
op.IpToLong = utils.IP2Long(ipTo)
// TODO 支持IPv6
if iputils.IsIPv4(ipFrom) {
op.IpFromLong = iputils.ToLong(ipFrom)
}
if iputils.IsIPv4(ipTo) {
op.IpToLong = iputils.ToLong(ipTo)
}
op.Reason = reason
op.Type = itemType
op.EventLevel = eventLevel
@@ -345,8 +352,15 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
op.Id = itemId
op.IpFrom = ipFrom
op.IpTo = ipTo
op.IpFromLong = utils.IP2Long(ipFrom)
op.IpToLong = utils.IP2Long(ipTo)
// TODO 支持IPv6
if iputils.IsIPv4(ipFrom) {
op.IpFromLong = iputils.ToLong(ipFrom)
}
if iputils.IsIPv4(ipTo) {
op.IpToLong = iputils.ToLong(ipTo)
}
op.Reason = reason
op.Type = itemType
op.EventLevel = eventLevel

View File

@@ -10,9 +10,9 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/dbs"
@@ -547,7 +547,7 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
record, ok := nodeRecordMapping[key]
if !ok {
var recordType = dnstypes.RecordTypeA
if utils.IsIPv6(ip) {
if iputils.IsIPv6(ip) {
recordType = dnstypes.RecordTypeAAAA
}

View File

@@ -2,10 +2,10 @@ package services
import (
"context"
"encoding/binary"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
@@ -670,14 +670,17 @@ func (this *HTTPFirewallPolicyService) CheckHTTPFirewallPolicyIPStatus(ctx conte
}
// 校验IP
ip := net.ParseIP(req.Ip)
var ip = net.ParseIP(req.Ip)
if len(ip) == 0 {
return &pb.CheckHTTPFirewallPolicyIPStatusResponse{
IsOk: false,
Error: "请输入正确的IP",
}, nil
}
ipLong := utils.IP2Long(req.Ip)
var ipLong uint64
if ip.To4() != nil {
ipLong = uint64(binary.BigEndian.Uint32(ip.To4()))
}
var tx = this.NullTx()
firewallPolicy, err := models.SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, req.HttpFirewallPolicyId, false, nil)

View File

@@ -2,6 +2,7 @@ package services
import (
"context"
"encoding/binary"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
@@ -484,14 +485,17 @@ func (this *IPItemService) CheckIPItemStatus(ctx context.Context, req *pb.CheckI
}
// 校验IP
ip := net.ParseIP(req.Ip)
var ip = net.ParseIP(req.Ip)
if len(ip) == 0 {
return &pb.CheckIPItemStatusResponse{
IsOk: false,
Error: "请输入正确的IP",
}, nil
}
ipLong := utils.IP2Long(req.Ip)
var ipLong uint64
if ip.To4() != nil {
ipLong = uint64(binary.BigEndian.Uint32(ip.To4()))
}
var tx = this.NullTx()

View File

@@ -6,7 +6,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/stats"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
@@ -269,8 +269,18 @@ func upgradeV0_0_10(db *dbs.DB) error {
return err
}
for _, one := range ones {
ipFromLong := utils.IP2Long(one.GetString("ipFrom"))
ipToLong := utils.IP2Long(one.GetString("ipTo"))
var ipFrom = one.GetString("ipFrom")
var ipTo = one.GetString("ipTo")
var ipFromLong string
var ipToLong string
// TODO 支持IPv6
if iputils.IsIPv4(ipFrom) {
ipFromLong = iputils.ToLong(ipFrom)
}
if iputils.IsIPv4(ipTo) {
ipToLong = iputils.ToLong(ipTo)
}
_, err = db.Exec("UPDATE edgeIPItems SET ipFromLong=?, ipToLong=? WHERE id=?", ipFromLong, ipToLong, one.GetInt64("id"))
if err != nil {
return err

View File

@@ -7,8 +7,8 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
@@ -434,7 +434,7 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, taskVersion int64, clusterI
}
var recordType = dnstypes.RecordTypeA
if utils.IsIPv6(ip) {
if iputils.IsIPv6(ip) {
recordType = dnstypes.RecordTypeAAAA
}

View File

@@ -7,8 +7,8 @@ import (
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -259,7 +259,7 @@ func (this *HealthCheckExecutor) runNode(healthCheckConfig *serverconfigs.Health
// 检查单个节点
func (this *HealthCheckExecutor) runNodeOnce(healthCheckConfig *serverconfigs.HealthCheckConfig, result *HealthCheckResult) error {
// 支持IPv6
if utils.IsIPv6(result.NodeAddr) {
if iputils.IsIPv6(result.NodeAddr) {
result.NodeAddr = configutils.QuoteIP(result.NodeAddr)
}

View File

@@ -1,31 +0,0 @@
package utils
import (
"encoding/binary"
"github.com/cespare/xxhash/v2"
"math"
"net"
"strings"
)
// IP2Long 将IP转换为整型
// 注意IPv6没有顺序
func IP2Long(ip string) uint64 {
if len(ip) == 0 {
return 0
}
s := net.ParseIP(ip)
if len(s) == 0 {
return 0
}
if strings.Contains(ip, ":") {
return math.MaxUint32 + xxhash.Sum64(s)
}
return uint64(binary.BigEndian.Uint32(s.To4()))
}
// IsIPv6 判断是否为IPv6
func IsIPv6(ip string) bool {
return strings.Contains(ip, ":")
}

View File

@@ -1,12 +1,14 @@
package utils
import (
"encoding/binary"
"net"
"strings"
)
// 计算版本代号
// VersionToLong 计算版本代号
func VersionToLong(version string) uint32 {
countDots := strings.Count(version, ".")
var countDots = strings.Count(version, ".")
if countDots == 2 {
version += ".0"
} else if countDots == 1 {
@@ -14,5 +16,9 @@ func VersionToLong(version string) uint32 {
} else if countDots == 0 {
version += ".0.0.0"
}
return uint32(IP2Long(version))
var ip = net.ParseIP(version)
if ip == nil || ip.To4() == nil {
return 0
}
return binary.BigEndian.Uint32(ip.To4())
}