classify.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2023 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. package nathole
  15. import (
  16. "fmt"
  17. "net"
  18. "slices"
  19. "strconv"
  20. )
  21. const (
  22. EasyNAT = "EasyNAT"
  23. HardNAT = "HardNAT"
  24. BehaviorNoChange = "BehaviorNoChange"
  25. BehaviorIPChanged = "BehaviorIPChanged"
  26. BehaviorPortChanged = "BehaviorPortChanged"
  27. BehaviorBothChanged = "BehaviorBothChanged"
  28. )
  29. type NatFeature struct {
  30. NatType string
  31. Behavior string
  32. PortsDifference int
  33. RegularPortsChange bool
  34. PublicNetwork bool
  35. }
  36. func ClassifyNATFeature(addresses []string, localIPs []string) (*NatFeature, error) {
  37. if len(addresses) <= 1 {
  38. return nil, fmt.Errorf("not enough addresses")
  39. }
  40. natFeature := &NatFeature{}
  41. ipChanged := false
  42. portChanged := false
  43. var baseIP, basePort string
  44. var portMax, portMin int
  45. for _, addr := range addresses {
  46. ip, port, err := net.SplitHostPort(addr)
  47. if err != nil {
  48. return nil, err
  49. }
  50. portNum, err := strconv.Atoi(port)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if slices.Contains(localIPs, ip) {
  55. natFeature.PublicNetwork = true
  56. }
  57. if baseIP == "" {
  58. baseIP = ip
  59. basePort = port
  60. portMax = portNum
  61. portMin = portNum
  62. continue
  63. }
  64. if portNum > portMax {
  65. portMax = portNum
  66. }
  67. if portNum < portMin {
  68. portMin = portNum
  69. }
  70. if baseIP != ip {
  71. ipChanged = true
  72. }
  73. if basePort != port {
  74. portChanged = true
  75. }
  76. }
  77. switch {
  78. case ipChanged && portChanged:
  79. natFeature.NatType = HardNAT
  80. natFeature.Behavior = BehaviorBothChanged
  81. case ipChanged:
  82. natFeature.NatType = HardNAT
  83. natFeature.Behavior = BehaviorIPChanged
  84. case portChanged:
  85. natFeature.NatType = HardNAT
  86. natFeature.Behavior = BehaviorPortChanged
  87. default:
  88. natFeature.NatType = EasyNAT
  89. natFeature.Behavior = BehaviorNoChange
  90. }
  91. if natFeature.Behavior == BehaviorPortChanged {
  92. natFeature.PortsDifference = portMax - portMin
  93. if natFeature.PortsDifference <= 5 && natFeature.PortsDifference >= 1 {
  94. natFeature.RegularPortsChange = true
  95. }
  96. }
  97. return natFeature, nil
  98. }
  99. func ClassifyFeatureCount(features []*NatFeature) (int, int, int) {
  100. easyCount := 0
  101. hardCount := 0
  102. // for HardNAT
  103. portsChangedRegularCount := 0
  104. for _, feature := range features {
  105. if feature.NatType == EasyNAT {
  106. easyCount++
  107. continue
  108. }
  109. hardCount++
  110. if feature.RegularPortsChange {
  111. portsChangedRegularCount++
  112. }
  113. }
  114. return easyCount, hardCount, portsChangedRegularCount
  115. }