gitea源码

hostmatcher.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package hostmatcher
  4. import (
  5. "net"
  6. "path/filepath"
  7. "slices"
  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.SplitSeq(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.SplitSeq(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. // AppendPattern appends more pattern to match
  70. func (hl *HostMatchList) AppendPattern(pattern string) {
  71. hl.patterns = append(hl.patterns, pattern)
  72. }
  73. // IsEmpty checks if the checklist is empty
  74. func (hl *HostMatchList) IsEmpty() bool {
  75. return hl == nil || (len(hl.builtins) == 0 && len(hl.patterns) == 0 && len(hl.ipNets) == 0)
  76. }
  77. func (hl *HostMatchList) checkPattern(host string) bool {
  78. host = strings.ToLower(strings.TrimSpace(host))
  79. for _, pattern := range hl.patterns {
  80. if matched, _ := filepath.Match(pattern, host); matched {
  81. return true
  82. }
  83. }
  84. return false
  85. }
  86. func (hl *HostMatchList) checkIP(ip net.IP) bool {
  87. if slices.Contains(hl.patterns, "*") {
  88. return true
  89. }
  90. for _, builtin := range hl.builtins {
  91. switch builtin {
  92. case MatchBuiltinExternal:
  93. if ip.IsGlobalUnicast() && !ip.IsPrivate() {
  94. return true
  95. }
  96. case MatchBuiltinPrivate:
  97. if ip.IsPrivate() {
  98. return true
  99. }
  100. case MatchBuiltinLoopback:
  101. if ip.IsLoopback() {
  102. return true
  103. }
  104. }
  105. }
  106. for _, ipNet := range hl.ipNets {
  107. if ipNet.Contains(ip) {
  108. return true
  109. }
  110. }
  111. return false
  112. }
  113. // MatchHostName checks if the host matches an allow/deny(block) list
  114. func (hl *HostMatchList) MatchHostName(host string) bool {
  115. if hl == nil {
  116. return false
  117. }
  118. hostname, _, err := net.SplitHostPort(host)
  119. if err != nil {
  120. hostname = host
  121. }
  122. if hl.checkPattern(hostname) {
  123. return true
  124. }
  125. if ip := net.ParseIP(hostname); ip != nil {
  126. return hl.checkIP(ip)
  127. }
  128. return false
  129. }
  130. // MatchIPAddr checks if the IP matches an allow/deny(block) list, it's safe to pass `nil` to `ip`
  131. func (hl *HostMatchList) MatchIPAddr(ip net.IP) bool {
  132. if hl == nil {
  133. return false
  134. }
  135. host := ip.String() // nil-safe, we will get "<nil>" if ip is nil
  136. return hl.checkPattern(host) || hl.checkIP(ip)
  137. }
  138. // MatchHostOrIP checks if the host or IP matches an allow/deny(block) list
  139. func (hl *HostMatchList) MatchHostOrIP(host string, ip net.IP) bool {
  140. return hl.MatchHostName(host) || hl.MatchIPAddr(ip)
  141. }