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"
|
2023-05-28 16:50:43 +08:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
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 {
|
2023-05-28 16:50:43 +08:00
|
|
|
return nil, fmt.Errorf("not enough addresses")
|
2023-03-30 20:28:15 +08:00
|
|
|
}
|
2023-05-28 16:50:43 +08:00
|
|
|
natFeature := &NatFeature{}
|
2023-03-30 20:28:15 +08:00
|
|
|
ipChanged := false
|
|
|
|
portChanged := false
|
|
|
|
|
|
|
|
var baseIP, basePort string
|
2023-05-28 16:50:43 +08:00
|
|
|
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 {
|
2023-05-28 16:50:43 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
portNum, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-03-30 20:28:15 +08:00
|
|
|
}
|
2023-05-28 16:50:43 +08:00
|
|
|
if lo.Contains(localIPs, ip) {
|
|
|
|
natFeature.PublicNetwork = true
|
|
|
|
}
|
|
|
|
|
2023-03-30 20:28:15 +08:00
|
|
|
if baseIP == "" {
|
|
|
|
baseIP = ip
|
|
|
|
basePort = port
|
2023-05-28 16:50:43 +08:00
|
|
|
portMax = portNum
|
|
|
|
portMin = portNum
|
2023-03-30 20:28:15 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
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-05-28 16:50:43 +08:00
|
|
|
}
|
2023-03-30 20:28:15 +08:00
|
|
|
|
2023-05-28 16:50:43 +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:
|
2023-05-28 16:50:43 +08:00
|
|
|
natFeature.NatType = HardNAT
|
|
|
|
natFeature.Behavior = BehaviorBothChanged
|
2023-03-30 20:28:15 +08:00
|
|
|
case ipChanged:
|
2023-05-28 16:50:43 +08:00
|
|
|
natFeature.NatType = HardNAT
|
|
|
|
natFeature.Behavior = BehaviorIPChanged
|
2023-03-30 20:28:15 +08:00
|
|
|
case portChanged:
|
2023-05-28 16:50:43 +08:00
|
|
|
natFeature.NatType = HardNAT
|
|
|
|
natFeature.Behavior = BehaviorPortChanged
|
2023-03-30 20:28:15 +08:00
|
|
|
default:
|
2023-05-28 16:50:43 +08:00
|
|
|
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
|
|
|
}
|
2023-05-28 16:50:43 +08:00
|
|
|
return easyCount, hardCount, portsChangedRegularCount
|
2023-03-30 20:28:15 +08:00
|
|
|
}
|