-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathrouter.go
More file actions
63 lines (57 loc) · 1.75 KB
/
Copy pathrouter.go
File metadata and controls
63 lines (57 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package wireproxy
import (
"log"
"net"
"regexp"
"strings"
)
// DomainRouter decides whether a connection to a given host is routed through
// the WireGuard tunnel (host matches the whitelist) or dialed directly.
//
// An empty pattern list means "tunnel everything", preserving the legacy
// behaviour where every proxied connection goes through the tunnel.
type DomainRouter struct {
patterns []*regexp.Regexp
logNames bool
}
// NewDomainRouter builds a DomainRouter from compiled regex patterns. When
// logNames is true, every routing decision is logged so users can discover
// which domains their applications reach (and thus what to whitelist).
func NewDomainRouter(patterns []*regexp.Regexp, logNames bool) *DomainRouter {
return &DomainRouter{patterns: patterns, logNames: logNames}
}
// shouldTunnel reports whether host should be routed through the tunnel.
func (r *DomainRouter) shouldTunnel(host string) bool {
if r == nil || len(r.patterns) == 0 {
return true
}
host = strings.ToLower(strings.TrimSuffix(host, "."))
for _, p := range r.patterns {
if p.MatchString(host) {
return true
}
}
return false
}
// route decides how to dial host and, when logging is enabled, records the
// decision. It returns true when the connection should use the tunnel.
func (r *DomainRouter) route(host string) bool {
tunnel := r.shouldTunnel(host)
if r != nil && r.logNames {
dest := "DIRECT"
if tunnel {
dest = "TUNNEL"
}
log.Printf("route: %s -> %s\n", host, dest)
}
return tunnel
}
// hostFromAddr extracts the host portion from a "host:port" address, falling
// back to the whole string when there is no port.
func hostFromAddr(address string) string {
host, _, err := net.SplitHostPort(address)
if err != nil {
return address
}
return host
}