frp/pkg/nathole/classify.go

128 lines
2.8 KiB
Go
Raw Normal View History

2023-03-30 20:28:15 +08:00
// Copyright 2023 The frp Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nathole
import (
"fmt"
"net"
"strconv"
"github.com/samber/lo"
2023-03-30 20:28:15 +08:00
)
const (
EasyNAT = "EasyNAT"
HardNAT = "HardNAT"
BehaviorNoChange = "BehaviorNoChange"
BehaviorIPChanged = "BehaviorIPChanged"
BehaviorPortChanged = "BehaviorPortChanged"
BehaviorBothChanged = "BehaviorBothChanged"
)
type NatFeature struct {
NatType string
Behavior string
PortsDifference int
RegularPortsChange bool
PublicNetwork bool
}
func ClassifyNATFeature(addresses []string, localIPs []string) (*NatFeature, error) {
2023-03-30 20:28:15 +08:00
if len(addresses) <= 1 {
return nil, fmt.Errorf("not enough addresses")
2023-03-30 20:28:15 +08:00
}
natFeature := &NatFeature{}
2023-03-30 20:28:15 +08:00
ipChanged := false
portChanged := false
var baseIP, basePort string
var portMax, portMin int
2023-03-30 20:28:15 +08:00
for _, addr := range addresses {
ip, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
portNum, err := strconv.Atoi(port)
if err != nil {
return nil, err
2023-03-30 20:28:15 +08:00
}
if lo.Contains(localIPs, ip) {
natFeature.PublicNetwork = true
}
2023-03-30 20:28:15 +08:00
if baseIP == "" {
baseIP = ip
basePort = port
portMax = portNum
portMin = portNum
2023-03-30 20:28:15 +08:00
continue
}
if portNum > portMax {
portMax = portNum
}
if portNum < portMin {
portMin = portNum
}
2023-03-30 20:28:15 +08:00
if baseIP != ip {
ipChanged = true
}
if basePort != port {
portChanged = true
}
}
2023-03-30 20:28:15 +08:00
natFeature.PortsDifference = portMax - portMin
if natFeature.PortsDifference <= 10 && natFeature.PortsDifference >= 1 {
natFeature.RegularPortsChange = true
2023-03-30 20:28:15 +08:00
}
switch {
case ipChanged && portChanged:
natFeature.NatType = HardNAT
natFeature.Behavior = BehaviorBothChanged
2023-03-30 20:28:15 +08:00
case ipChanged:
natFeature.NatType = HardNAT
natFeature.Behavior = BehaviorIPChanged
2023-03-30 20:28:15 +08:00
case portChanged:
natFeature.NatType = HardNAT
natFeature.Behavior = BehaviorPortChanged
2023-03-30 20:28:15 +08:00
default:
natFeature.NatType = EasyNAT
natFeature.Behavior = BehaviorNoChange
}
return natFeature, nil
}
func ClassifyFeatureCount(features []*NatFeature) (int, int, int) {
easyCount := 0
hardCount := 0
// for HardNAT
portsChangedRegularCount := 0
for _, feature := range features {
if feature.NatType == EasyNAT {
easyCount++
continue
}
hardCount++
if feature.RegularPortsChange {
portsChangedRegularCount++
}
2023-03-30 20:28:15 +08:00
}
return easyCount, hardCount, portsChangedRegularCount
2023-03-30 20:28:15 +08:00
}