|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "net/netip" |
| 6 | + "net/url" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// TargetBypassMatcher decides whether a target should skip Resin node routing |
| 13 | +// and be dialed directly from the Resin process. |
| 14 | +type TargetBypassMatcher struct { |
| 15 | + rules []targetBypassRule |
| 16 | +} |
| 17 | + |
| 18 | +type targetBypassRule struct { |
| 19 | + local bool |
| 20 | + cidr netip.Prefix |
| 21 | + exact string |
| 22 | + glob *regexp.Regexp |
| 23 | +} |
| 24 | + |
| 25 | +func NewTargetBypassMatcher(patterns []string) *TargetBypassMatcher { |
| 26 | + m := &TargetBypassMatcher{} |
| 27 | + for _, pattern := range patterns { |
| 28 | + if rule, ok := compileTargetBypassRule(pattern); ok { |
| 29 | + m.rules = append(m.rules, rule) |
| 30 | + } |
| 31 | + } |
| 32 | + if len(m.rules) == 0 { |
| 33 | + return nil |
| 34 | + } |
| 35 | + return m |
| 36 | +} |
| 37 | + |
| 38 | +func (m *TargetBypassMatcher) ShouldBypass(target string) bool { |
| 39 | + if m == nil || len(m.rules) == 0 { |
| 40 | + return false |
| 41 | + } |
| 42 | + host := targetHostForBypass(target) |
| 43 | + if host == "" { |
| 44 | + return false |
| 45 | + } |
| 46 | + host = strings.ToLower(host) |
| 47 | + addr, hasAddr := parseAddrForBypass(host) |
| 48 | + for _, rule := range m.rules { |
| 49 | + switch { |
| 50 | + case rule.local: |
| 51 | + if isLocalBypassHost(host) { |
| 52 | + return true |
| 53 | + } |
| 54 | + case rule.cidr.IsValid(): |
| 55 | + if hasAddr && rule.cidr.Contains(addr) { |
| 56 | + return true |
| 57 | + } |
| 58 | + case rule.glob != nil: |
| 59 | + if rule.glob.MatchString(host) { |
| 60 | + return true |
| 61 | + } |
| 62 | + case rule.exact != "": |
| 63 | + if host == rule.exact { |
| 64 | + return true |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return false |
| 69 | +} |
| 70 | + |
| 71 | +func compileTargetBypassRule(raw string) (targetBypassRule, bool) { |
| 72 | + pattern := strings.TrimSpace(raw) |
| 73 | + if pattern == "" { |
| 74 | + return targetBypassRule{}, false |
| 75 | + } |
| 76 | + if strings.EqualFold(pattern, "<local>") { |
| 77 | + return targetBypassRule{local: true}, true |
| 78 | + } |
| 79 | + if prefix, err := netip.ParsePrefix(stripIPv6Zone(strings.Trim(pattern, "[]"))); err == nil { |
| 80 | + return targetBypassRule{cidr: prefix}, true |
| 81 | + } |
| 82 | + |
| 83 | + pattern = strings.ToLower(targetHostForBypass(pattern)) |
| 84 | + if pattern == "" { |
| 85 | + return targetBypassRule{}, false |
| 86 | + } |
| 87 | + if strings.ContainsAny(pattern, "*?") { |
| 88 | + return targetBypassRule{glob: compileBypassGlob(pattern)}, true |
| 89 | + } |
| 90 | + return targetBypassRule{exact: pattern}, true |
| 91 | +} |
| 92 | + |
| 93 | +func compileBypassGlob(pattern string) *regexp.Regexp { |
| 94 | + quoted := regexp.QuoteMeta(pattern) |
| 95 | + quoted = strings.ReplaceAll(quoted, `\*`, ".*") |
| 96 | + quoted = strings.ReplaceAll(quoted, `\?`, ".") |
| 97 | + return regexp.MustCompile("^" + quoted + "$") |
| 98 | +} |
| 99 | + |
| 100 | +func targetHostForBypass(target string) string { |
| 101 | + target = strings.TrimSpace(target) |
| 102 | + if target == "" { |
| 103 | + return "" |
| 104 | + } |
| 105 | + if strings.Contains(target, "://") || strings.HasPrefix(target, "//") { |
| 106 | + if u, err := url.Parse(target); err == nil && u.Host != "" { |
| 107 | + target = u.Host |
| 108 | + } |
| 109 | + } |
| 110 | + if host, _, err := net.SplitHostPort(target); err == nil { |
| 111 | + return strings.Trim(host, "[]") |
| 112 | + } |
| 113 | + if strings.HasPrefix(target, "[") && strings.HasSuffix(target, "]") { |
| 114 | + return strings.Trim(target, "[]") |
| 115 | + } |
| 116 | + if i := strings.LastIndexByte(target, ':'); i > 0 && strings.Count(target, ":") == 1 { |
| 117 | + if _, err := strconv.Atoi(target[i+1:]); err == nil { |
| 118 | + return strings.Trim(target[:i], "[]") |
| 119 | + } |
| 120 | + } |
| 121 | + return strings.Trim(target, "[]") |
| 122 | +} |
| 123 | + |
| 124 | +func parseAddrForBypass(host string) (netip.Addr, bool) { |
| 125 | + addr, err := netip.ParseAddr(stripIPv6Zone(host)) |
| 126 | + return addr, err == nil |
| 127 | +} |
| 128 | + |
| 129 | +func stripIPv6Zone(host string) string { |
| 130 | + if i := strings.LastIndexByte(host, '%'); i >= 0 { |
| 131 | + return host[:i] |
| 132 | + } |
| 133 | + return host |
| 134 | +} |
| 135 | + |
| 136 | +func isLocalBypassHost(host string) bool { |
| 137 | + if host == "localhost" { |
| 138 | + return true |
| 139 | + } |
| 140 | + if _, ok := parseAddrForBypass(host); ok { |
| 141 | + return false |
| 142 | + } |
| 143 | + return !strings.Contains(host, ".") |
| 144 | +} |
0 commit comments