You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hostmatcher.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package hostmatcher
  5. import (
  6. "net"
  7. "path/filepath"
  8. "strings"
  9. )
  10. // HostMatchList is used to check if a host or IP is in a list.
  11. type HostMatchList struct {
  12. SettingKeyHint string
  13. SettingValue string
  14. // builtins networks
  15. builtins []string
  16. // patterns for host names (with wildcard support)
  17. patterns []string
  18. // ipNets is the CIDR network list
  19. ipNets []*net.IPNet
  20. }
  21. // MatchBuiltinExternal A valid non-private unicast IP, all hosts on public internet are matched
  22. const MatchBuiltinExternal = "external"
  23. // MatchBuiltinPrivate RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) and RFC 4193 (FC00::/7). Also called LAN/Intranet.
  24. const MatchBuiltinPrivate = "private"
  25. // MatchBuiltinLoopback 127.0.0.0/8 for IPv4 and ::1/128 for IPv6, localhost is included.
  26. const MatchBuiltinLoopback = "loopback"
  27. func isBuiltin(s string) bool {
  28. return s == MatchBuiltinExternal || s == MatchBuiltinPrivate || s == MatchBuiltinLoopback
  29. }
  30. // ParseHostMatchList parses the host list HostMatchList
  31. func ParseHostMatchList(settingKeyHint, hostList string) *HostMatchList {
  32. hl := &HostMatchList{SettingKeyHint: settingKeyHint, SettingValue: hostList}
  33. for _, s := range strings.Split(hostList, ",") {
  34. s = strings.ToLower(strings.TrimSpace(s))
  35. if s == "" {
  36. continue
  37. }
  38. _, ipNet, err := net.ParseCIDR(s)
  39. if err == nil {
  40. hl.ipNets = append(hl.ipNets, ipNet)
  41. } else if isBuiltin(s) {
  42. hl.builtins = append(hl.builtins, s)
  43. } else {
  44. hl.patterns = append(hl.patterns, s)
  45. }
  46. }
  47. return hl
  48. }
  49. // ParseSimpleMatchList parse a simple matchlist (no built-in networks, no CIDR support, only wildcard pattern match)
  50. func ParseSimpleMatchList(settingKeyHint, matchList string) *HostMatchList {
  51. hl := &HostMatchList{
  52. SettingKeyHint: settingKeyHint,
  53. SettingValue: matchList,
  54. }
  55. for _, s := range strings.Split(matchList, ",") {
  56. s = strings.ToLower(strings.TrimSpace(s))
  57. if s == "" {
  58. continue
  59. }
  60. // we keep the same result as old `matchlist`, so no builtin/CIDR support here, we only match wildcard patterns
  61. hl.patterns = append(hl.patterns, s)
  62. }
  63. return hl
  64. }
  65. // AppendBuiltin appends more builtins to match
  66. func (hl *HostMatchList) AppendBuiltin(builtin string) {
  67. hl.builtins = append(hl.builtins, builtin)
  68. }
  69. // IsEmpty checks if the checklist is empty
  70. func (hl *HostMatchList) IsEmpty() bool {
  71. return hl == nil || (len(hl.builtins) == 0 && len(hl.patterns) == 0 && len(hl.ipNets) == 0)
  72. }
  73. func (hl *HostMatchList) checkPattern(host string) bool {
  74. host = strings.ToLower(strings.TrimSpace(host))
  75. for _, pattern := range hl.patterns {
  76. if matched, _ := filepath.Match(pattern, host); matched {
  77. return true
  78. }
  79. }
  80. return false
  81. }
  82. func (hl *HostMatchList) checkIP(ip net.IP) bool {
  83. for _, pattern := range hl.patterns {
  84. if pattern == "*" {
  85. return true
  86. }
  87. }
  88. for _, builtin := range hl.builtins {
  89. switch builtin {
  90. case MatchBuiltinExternal:
  91. if ip.IsGlobalUnicast() && !ip.IsPrivate() {
  92. return true
  93. }
  94. case MatchBuiltinPrivate:
  95. if ip.IsPrivate() {
  96. return true
  97. }
  98. case MatchBuiltinLoopback:
  99. if ip.IsLoopback() {
  100. return true
  101. }
  102. }
  103. }
  104. for _, ipNet := range hl.ipNets {
  105. if ipNet.Contains(ip) {
  106. return true
  107. }
  108. }
  109. return false
  110. }
  111. // MatchHostName checks if the host matches an allow/deny(block) list
  112. func (hl *HostMatchList) MatchHostName(host string) bool {
  113. if hl == nil {
  114. return false
  115. }
  116. hostname, _, err := net.SplitHostPort(host)
  117. if err != nil {
  118. hostname = host
  119. }
  120. if hl.checkPattern(hostname) {
  121. return true
  122. }
  123. if ip := net.ParseIP(hostname); ip != nil {
  124. return hl.checkIP(ip)
  125. }
  126. return false
  127. }
  128. // MatchIPAddr checks if the IP matches an allow/deny(block) list, it's safe to pass `nil` to `ip`
  129. func (hl *HostMatchList) MatchIPAddr(ip net.IP) bool {
  130. if hl == nil {
  131. return false
  132. }
  133. host := ip.String() // nil-safe, we will get "<nil>" if ip is nil
  134. return hl.checkPattern(host) || hl.checkIP(ip)
  135. }
  136. // MatchHostOrIP checks if the host or IP matches an allow/deny(block) list
  137. func (hl *HostMatchList) MatchHostOrIP(host string, ip net.IP) bool {
  138. return hl.MatchHostName(host) || hl.MatchIPAddr(ip)
  139. }