mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 12:20:27 +08:00
优化代码
This commit is contained in:
@@ -14,7 +14,7 @@ type NSNodeConfig struct {
|
||||
Secret string `yaml:"secret" json:"secret"`
|
||||
ClusterId int64 `yaml:"clusterId" json:"clusterId"`
|
||||
AccessLogRef *NSAccessLogRef `yaml:"accessLogRef" json:"accessLogRef"`
|
||||
RecursionConfig *RecursionConfig `yaml:"recursionConfig" json:"recursionConfig"`
|
||||
RecursionConfig *NSRecursionConfig `yaml:"recursionConfig" json:"recursionConfig"`
|
||||
DDoSProtection *ddosconfigs.ProtectionConfig `yaml:"ddosProtection" json:"ddosProtection"`
|
||||
AllowedIPs []string `yaml:"allowedIPs" json:"allowedIPs"`
|
||||
|
||||
|
||||
14
pkg/dnsconfigs/ns_plans.go
Normal file
14
pkg/dnsconfigs/ns_plans.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package dnsconfigs
|
||||
|
||||
type NSPlanConfig struct {
|
||||
SupportCountryRoutes bool `json:"supportCountryRoutes"` // 支持全球国家/地区线路
|
||||
SupportChinaProvinceRoutes bool `json:"supportChinaProvinceRoutes"` // 支持国内省份线路
|
||||
SupportISPRoutes bool `json:"supportISPRoutes"` // 支持ISP运营商线路
|
||||
CountCustomRoutes int `json:"countCustomRoutes"` // 自定义的线路数量
|
||||
CountLoadBalanceRecords bool `json:"countLoadBalanceRecords"` // 负载均衡条数
|
||||
MinTTL int32 `json:"minTTL"` // 最小TTL
|
||||
|
||||
SupportAPI bool `json:"supportAPI"` // 是否支持API操作
|
||||
}
|
||||
22
pkg/dnsconfigs/ns_recursion_config.go
Normal file
22
pkg/dnsconfigs/ns_recursion_config.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dnsconfigs
|
||||
|
||||
type NSDNSHost struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Protocol string `json:"protocol"`
|
||||
}
|
||||
|
||||
// NSRecursionConfig 递归DNS设置
|
||||
type NSRecursionConfig struct {
|
||||
IsOn bool `json:"isOn"`
|
||||
Hosts []*NSDNSHost `json:"hosts"`
|
||||
UseLocalHosts bool `json:"useLocalHosts"` // 自动从本机读取DNS
|
||||
AllowDomains []string `json:"allowDomains"`
|
||||
DenyDomains []string `json:"denyDomains"`
|
||||
}
|
||||
|
||||
func (this *NSRecursionConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
@@ -11,38 +11,38 @@ import (
|
||||
"net"
|
||||
)
|
||||
|
||||
type RouteRangeType = string
|
||||
type NSRouteRangeType = string
|
||||
|
||||
const (
|
||||
RouteRangeTypeIP RouteRangeType = "ipRange" // IP范围
|
||||
RouteRangeTypeCIDR RouteRangeType = "cidr" // CIDR
|
||||
RouteRangeTypeRegion RouteRangeType = "region" // 区域
|
||||
NSRouteRangeTypeIP NSRouteRangeType = "ipRange" // IP范围
|
||||
NSRouteRangeTypeCIDR NSRouteRangeType = "cidr" // CIDR
|
||||
NSRouteRangeTypeRegion NSRouteRangeType = "region" // 区域
|
||||
)
|
||||
|
||||
func AllRouteRangeTypes() []*shared.Definition {
|
||||
func AllNSRouteRangeTypes() []*shared.Definition {
|
||||
return []*shared.Definition{
|
||||
{
|
||||
Name: "IP范围",
|
||||
Code: RouteRangeTypeIP,
|
||||
Code: NSRouteRangeTypeIP,
|
||||
},
|
||||
{
|
||||
Name: "CIDR",
|
||||
Code: RouteRangeTypeCIDR,
|
||||
Code: NSRouteRangeTypeCIDR,
|
||||
},
|
||||
{
|
||||
Name: "区域",
|
||||
Code: RouteRangeTypeRegion,
|
||||
Code: NSRouteRangeTypeRegion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// RouteRegionResolver 解析IP接口
|
||||
type RouteRegionResolver interface {
|
||||
// NSRouteRegionResolver 解析IP接口
|
||||
type NSRouteRegionResolver interface {
|
||||
Resolve(ip net.IP) (countryId int64, provinceId int64, cityId int64, providerId int64)
|
||||
}
|
||||
|
||||
// RouteRangeInterface 线路范围接口
|
||||
type RouteRangeInterface interface {
|
||||
// NSRouteRangeInterface 线路范围接口
|
||||
type NSRouteRangeInterface interface {
|
||||
// Init 初始化
|
||||
Init() error
|
||||
|
||||
@@ -50,30 +50,30 @@ type RouteRangeInterface interface {
|
||||
Contains(ip net.IP) bool
|
||||
|
||||
// SetRegionResolver 设置IP解析接口
|
||||
SetRegionResolver(resolver RouteRegionResolver)
|
||||
SetRegionResolver(resolver NSRouteRegionResolver)
|
||||
|
||||
// IsExcluding 是否为排除
|
||||
IsExcluding() bool
|
||||
}
|
||||
|
||||
type BaseRouteRange struct {
|
||||
type NSBaseRouteRange struct {
|
||||
IsReverse bool `json:"isReverse"`
|
||||
|
||||
routeRegionResolver RouteRegionResolver
|
||||
routeRegionResolver NSRouteRegionResolver
|
||||
}
|
||||
|
||||
func (this *BaseRouteRange) SetRegionResolver(resolver RouteRegionResolver) {
|
||||
func (this *NSBaseRouteRange) SetRegionResolver(resolver NSRouteRegionResolver) {
|
||||
this.routeRegionResolver = resolver
|
||||
}
|
||||
|
||||
func (this *BaseRouteRange) IsExcluding() bool {
|
||||
func (this *NSBaseRouteRange) IsExcluding() bool {
|
||||
return this.IsReverse
|
||||
}
|
||||
|
||||
// RouteRangeIPRange IP范围配置
|
||||
// NSRouteRangeIPRange IP范围配置
|
||||
// IPv4和IPv6不能混用
|
||||
type RouteRangeIPRange struct {
|
||||
BaseRouteRange
|
||||
type NSRouteRangeIPRange struct {
|
||||
NSBaseRouteRange
|
||||
|
||||
IPFrom string `json:"ipFrom"`
|
||||
IPTo string `json:"ipTo"`
|
||||
@@ -84,7 +84,7 @@ type RouteRangeIPRange struct {
|
||||
ipVersion int // 4|6
|
||||
}
|
||||
|
||||
func (this *RouteRangeIPRange) Init() error {
|
||||
func (this *NSRouteRangeIPRange) Init() error {
|
||||
var ipFrom = net.ParseIP(this.IPFrom)
|
||||
var ipTo = net.ParseIP(this.IPTo)
|
||||
if ipFrom == nil {
|
||||
@@ -111,7 +111,7 @@ func (this *RouteRangeIPRange) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *RouteRangeIPRange) Contains(netIP net.IP) bool {
|
||||
func (this *NSRouteRangeIPRange) Contains(netIP net.IP) bool {
|
||||
if len(netIP) == 0 {
|
||||
return false
|
||||
}
|
||||
@@ -125,16 +125,16 @@ func (this *RouteRangeIPRange) Contains(netIP net.IP) bool {
|
||||
return ipLong >= this.ipFromLong && ipLong <= this.ipToLong
|
||||
}
|
||||
|
||||
// RouteRangeCIDR CIDR范围配置
|
||||
type RouteRangeCIDR struct {
|
||||
BaseRouteRange
|
||||
// NSRouteRangeCIDR CIDR范围配置
|
||||
type NSRouteRangeCIDR struct {
|
||||
NSBaseRouteRange
|
||||
|
||||
CIDR string `json:"cidr"`
|
||||
|
||||
cidr *net.IPNet
|
||||
}
|
||||
|
||||
func (this *RouteRangeCIDR) Init() error {
|
||||
func (this *NSRouteRangeCIDR) Init() error {
|
||||
_, ipNet, err := net.ParseCIDR(this.CIDR)
|
||||
if err != nil {
|
||||
return errors.New("parse cidr failed: " + err.Error())
|
||||
@@ -145,7 +145,7 @@ func (this *RouteRangeCIDR) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *RouteRangeCIDR) Contains(netIP net.IP) bool {
|
||||
func (this *NSRouteRangeCIDR) Contains(netIP net.IP) bool {
|
||||
if netIP == nil {
|
||||
return false
|
||||
}
|
||||
@@ -157,19 +157,19 @@ func (this *RouteRangeCIDR) Contains(netIP net.IP) bool {
|
||||
return this.cidr.Contains(netIP)
|
||||
}
|
||||
|
||||
// RouteRangeRegion 区域范围
|
||||
// NSRouteRangeRegion 区域范围
|
||||
// country:ID, province:ID, city:ID, isp:ID
|
||||
type RouteRangeRegion struct {
|
||||
BaseRouteRange
|
||||
type NSRouteRangeRegion struct {
|
||||
NSBaseRouteRange
|
||||
|
||||
Regions []*routeRegion `json:"regions"`
|
||||
}
|
||||
|
||||
func (this *RouteRangeRegion) Init() error {
|
||||
func (this *NSRouteRangeRegion) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *RouteRangeRegion) Contains(netIP net.IP) bool {
|
||||
func (this *NSRouteRangeRegion) Contains(netIP net.IP) bool {
|
||||
if this.routeRegionResolver == nil {
|
||||
return false
|
||||
}
|
||||
@@ -217,8 +217,8 @@ type routeRegion struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// InitRangesFromJSON 从JSON中初始化线路范围
|
||||
func InitRangesFromJSON(rangesJSON []byte) (ranges []RouteRangeInterface, err error) {
|
||||
// InitNSRangesFromJSON 从JSON中初始化线路范围
|
||||
func InitNSRangesFromJSON(rangesJSON []byte) (ranges []NSRouteRangeInterface, err error) {
|
||||
if len(rangesJSON) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -235,15 +235,15 @@ func InitRangesFromJSON(rangesJSON []byte) (ranges []RouteRangeInterface, err er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var r RouteRangeInterface
|
||||
var r NSRouteRangeInterface
|
||||
|
||||
switch rangeType {
|
||||
case RouteRangeTypeIP:
|
||||
r = &RouteRangeIPRange{}
|
||||
case RouteRangeTypeCIDR:
|
||||
r = &RouteRangeCIDR{}
|
||||
case RouteRangeTypeRegion:
|
||||
r = &RouteRangeRegion{}
|
||||
case NSRouteRangeTypeIP:
|
||||
r = &NSRouteRangeIPRange{}
|
||||
case NSRouteRangeTypeCIDR:
|
||||
r = &NSRouteRangeCIDR{}
|
||||
case NSRouteRangeTypeRegion:
|
||||
r = &NSRouteRangeRegion{}
|
||||
default:
|
||||
return nil, errors.New("invalid route line type '" + rangeType + "'")
|
||||
}
|
||||
@@ -8,12 +8,12 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRouteRangeIPRange_Contains(t *testing.T) {
|
||||
func TestNSRouteRangeIPRange_Contains(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
// ipv4
|
||||
{
|
||||
var r = &RouteRangeIPRange{
|
||||
var r = &NSRouteRangeIPRange{
|
||||
IPFrom: "192.168.1.100",
|
||||
IPTo: "192.168.3.200",
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func TestRouteRangeIPRange_Contains(t *testing.T) {
|
||||
// ipv6
|
||||
{
|
||||
var prefix = "1:2:3:4:5:6"
|
||||
var r = &RouteRangeIPRange{
|
||||
var r = &NSRouteRangeIPRange{
|
||||
IPFrom: prefix + ":1:8",
|
||||
IPTo: prefix + ":5:10",
|
||||
}
|
||||
@@ -48,7 +48,7 @@ func TestRouteRangeIPRange_Contains(t *testing.T) {
|
||||
}
|
||||
|
||||
{
|
||||
var r = &RouteRangeCIDR{
|
||||
var r = &NSRouteRangeCIDR{
|
||||
CIDR: "192.168.2.1/24",
|
||||
}
|
||||
err := r.Init()
|
||||
@@ -65,7 +65,7 @@ func TestRouteRangeIPRange_Contains(t *testing.T) {
|
||||
|
||||
// reverse ipv4
|
||||
{
|
||||
var r = &RouteRangeIPRange{
|
||||
var r = &NSRouteRangeIPRange{
|
||||
IPFrom: "192.168.1.100",
|
||||
IPTo: "192.168.3.200",
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func TestRouteRangeIPRange_Contains(t *testing.T) {
|
||||
|
||||
// reverse cidr
|
||||
{
|
||||
var r = &RouteRangeCIDR{
|
||||
var r = &NSRouteRangeCIDR{
|
||||
CIDR: "192.168.2.1/24",
|
||||
}
|
||||
err := r.Init()
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
package dnsconfigs
|
||||
|
||||
// TSIGConfig 配置
|
||||
type TSIGConfig struct {
|
||||
// NSTSIGConfig 配置
|
||||
type NSTSIGConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dnsconfigs
|
||||
|
||||
type DNSHost struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Protocol string `json:"protocol"`
|
||||
}
|
||||
|
||||
// RecursionConfig 递归DNS设置
|
||||
type RecursionConfig struct {
|
||||
IsOn bool `json:"isOn"`
|
||||
Hosts []*DNSHost `json:"hosts"`
|
||||
UseLocalHosts bool `json:"useLocalHosts"` // 自动从本机读取DNS
|
||||
AllowDomains []string `json:"allowDomains"`
|
||||
DenyDomains []string `json:"denyDomains"`
|
||||
}
|
||||
|
||||
func (this *RecursionConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user