diff --git a/internal/db/models/http_access_log_dao.go b/internal/db/models/http_access_log_dao.go index d15472b6..bf198262 100644 --- a/internal/db/models/http_access_log_dao.go +++ b/internal/db/models/http_access_log_dao.go @@ -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 diff --git a/internal/db/models/ip_item_dao.go b/internal/db/models/ip_item_dao.go index 7c9ba85f..1ba0ef35 100644 --- a/internal/db/models/ip_item_dao.go +++ b/internal/db/models/ip_item_dao.go @@ -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 diff --git a/internal/rpc/services/service_dns_domain.go b/internal/rpc/services/service_dns_domain.go index 416b8084..274382f0 100644 --- a/internal/rpc/services/service_dns_domain.go +++ b/internal/rpc/services/service_dns_domain.go @@ -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 } diff --git a/internal/rpc/services/service_http_firewall_policy.go b/internal/rpc/services/service_http_firewall_policy.go index 4af66af0..ec67ce4b 100644 --- a/internal/rpc/services/service_http_firewall_policy.go +++ b/internal/rpc/services/service_http_firewall_policy.go @@ -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) diff --git a/internal/rpc/services/service_ip_item.go b/internal/rpc/services/service_ip_item.go index e1e2f03e..a077a7b8 100644 --- a/internal/rpc/services/service_ip_item.go +++ b/internal/rpc/services/service_ip_item.go @@ -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() diff --git a/internal/setup/sql_upgrade.go b/internal/setup/sql_upgrade.go index 5d9e9721..f9a4f8f0 100644 --- a/internal/setup/sql_upgrade.go +++ b/internal/setup/sql_upgrade.go @@ -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 diff --git a/internal/tasks/dns_task_executor.go b/internal/tasks/dns_task_executor.go index a04f884d..fed1f052 100644 --- a/internal/tasks/dns_task_executor.go +++ b/internal/tasks/dns_task_executor.go @@ -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 } diff --git a/internal/tasks/health_check_executor.go b/internal/tasks/health_check_executor.go index 53b740a1..aaae4a0f 100644 --- a/internal/tasks/health_check_executor.go +++ b/internal/tasks/health_check_executor.go @@ -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) } diff --git a/internal/utils/ip.go b/internal/utils/ip.go deleted file mode 100644 index b4fa31db..00000000 --- a/internal/utils/ip.go +++ /dev/null @@ -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, ":") -} diff --git a/internal/utils/version.go b/internal/utils/version.go index 5742711d..aaafa084 100644 --- a/internal/utils/version.go +++ b/internal/utils/version.go @@ -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()) }