1
0

classify.go 2.8 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. "strconv"
  19. "github.com/samber/lo"
  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 lo.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. natFeature.PortsDifference = portMax - portMin
  78. if natFeature.PortsDifference <= 10 && natFeature.PortsDifference >= 1 {
  79. natFeature.RegularPortsChange = true
  80. }
  81. switch {
  82. case ipChanged && portChanged:
  83. natFeature.NatType = HardNAT
  84. natFeature.Behavior = BehaviorBothChanged
  85. case ipChanged:
  86. natFeature.NatType = HardNAT
  87. natFeature.Behavior = BehaviorIPChanged
  88. case portChanged:
  89. natFeature.NatType = HardNAT
  90. natFeature.Behavior = BehaviorPortChanged
  91. default:
  92. natFeature.NatType = EasyNAT
  93. natFeature.Behavior = BehaviorNoChange
  94. }
  95. return natFeature, nil
  96. }
  97. func ClassifyFeatureCount(features []*NatFeature) (int, int, int) {
  98. easyCount := 0
  99. hardCount := 0
  100. // for HardNAT
  101. portsChangedRegularCount := 0
  102. for _, feature := range features {
  103. if feature.NatType == EasyNAT {
  104. easyCount++
  105. continue
  106. }
  107. hardCount++
  108. if feature.RegularPortsChange {
  109. portsChangedRegularCount++
  110. }
  111. }
  112. return easyCount, hardCount, portsChangedRegularCount
  113. }