| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package nathole |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net" |
| 20 | "slices" |
| 21 | "strconv" |
| 22 | ) |
| 23 |
| 24 | const ( |
| 25 | EasyNAT = "EasyNAT" |
| 26 | HardNAT = "HardNAT" |
| 27 |
| 28 | BehaviorNoChange = "BehaviorNoChange" |
| 29 | BehaviorIPChanged = "BehaviorIPChanged" |
| 30 | BehaviorPortChanged = "BehaviorPortChanged" |
| 31 | BehaviorBothChanged = "BehaviorBothChanged" |
| 32 | ) |
| 33 |
| 34 | type NatFeature struct { |
| 35 | NatType string |
| 36 | Behavior string |
| 37 | PortsDifference int |
| 38 | RegularPortsChange bool |
| 39 | PublicNetwork bool |
| 40 | } |
| 41 |
| 42 | func ClassifyNATFeature(addresses []string, localIPs []string) (*NatFeature, error) { |
| 43 | if len(addresses) <= 1 { |
| 44 | return nil, fmt.Errorf("not enough addresses") |
| 45 | } |
| 46 | natFeature := &NatFeature{} |
| 47 | ipChanged := false |
| 48 | portChanged := false |
| 49 |
| 50 | var baseIP, basePort string |
| 51 | var portMax, portMin int |
| 52 | for _, addr := range addresses { |
| 53 | ip, port, err := net.SplitHostPort(addr) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | portNum, err := strconv.Atoi(port) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | if slices.Contains(localIPs, ip) { |
| 62 | natFeature.PublicNetwork = true |
| 63 | } |
| 64 |
| 65 | if baseIP == "" { |
| 66 | baseIP = ip |
| 67 | basePort = port |
| 68 | portMax = portNum |
| 69 | portMin = portNum |
| 70 | continue |
| 71 | } |
| 72 |
| 73 | portMax = max(portMax, portNum) |
| 74 | portMin = min(portMin, portNum) |
| 75 | if baseIP != ip { |
| 76 | ipChanged = true |
| 77 | } |
| 78 | if basePort != port { |
| 79 | portChanged = true |
| 80 | } |
| 81 | } |
| 82 |
| 83 | switch { |
| 84 | case ipChanged && portChanged: |
| 85 | natFeature.NatType = HardNAT |
| 86 | natFeature.Behavior = BehaviorBothChanged |
| 87 | case ipChanged: |
| 88 | natFeature.NatType = HardNAT |
| 89 | natFeature.Behavior = BehaviorIPChanged |
| 90 | case portChanged: |
| 91 | natFeature.NatType = HardNAT |
| 92 | natFeature.Behavior = BehaviorPortChanged |
| 93 | default: |
| 94 | natFeature.NatType = EasyNAT |
| 95 | natFeature.Behavior = BehaviorNoChange |
| 96 | } |
| 97 | if natFeature.Behavior == BehaviorPortChanged { |
| 98 | natFeature.PortsDifference = portMax - portMin |
| 99 | if natFeature.PortsDifference <= 5 && natFeature.PortsDifference >= 1 { |
| 100 | natFeature.RegularPortsChange = true |
| 101 | } |
| 102 | } |
| 103 | return natFeature, nil |
| 104 | } |
| 105 |
| 106 | func ClassifyFeatureCount(features []*NatFeature) (int, int, int) { |
| 107 | easyCount := 0 |
| 108 | hardCount := 0 |
| 109 | // for HardNAT |
| 110 | portsChangedRegularCount := 0 |
| 111 | for _, feature := range features { |
| 112 | if feature.NatType == EasyNAT { |
| 113 | easyCount++ |
| 114 | continue |
| 115 | } |
| 116 |
| 117 | hardCount++ |
| 118 | if feature.RegularPortsChange { |
| 119 | portsChangedRegularCount++ |
| 120 | } |
| 121 | } |
| 122 | return easyCount, hardCount, portsChangedRegularCount |
| 123 | } |