Skip to content

Commit

Permalink
Pull request 50: 6050-use-netip
Browse files Browse the repository at this point in the history
Merge in GO/urlfilter from 6050-use-netip to master

Updates AdguardTeam/AdGuardHome#6050.

Squashed commit of the following:

commit de1cd57
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Aug 21 20:13:21 2023 +0300

    all: imp docs, upd tools

commit 3d19556
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Aug 18 20:30:15 2023 +0300

    all: fix go.mod

commit 852bc06
Merge: 6b3749e b1001a1
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Aug 18 18:50:23 2023 +0300

    Merge branch 'master' into 6050-use-netip

commit 6b3749e
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Aug 17 17:19:43 2023 +0300

    all: upd slices again

commit 7a914a9
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Aug 8 17:33:14 2023 +0300

    rules: imp docs

commit 561b59e
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Aug 8 14:38:36 2023 +0300

    all: imp docs, tests

commit fa24781
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Aug 8 12:11:09 2023 +0300

    rules: revert changes, imp docs

commit d0b36ae
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Aug 7 19:25:49 2023 +0300

    all: migrate to go1.20 and netip
  • Loading branch information
EugeneOne1 committed Aug 22, 2023
1 parent b1001a1 commit 0ae94bb
Show file tree
Hide file tree
Showing 18 changed files with 606 additions and 437 deletions.
14 changes: 8 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/tls"
"crypto/x509"
"net"
"net/netip"
"os"
"os/signal"
"syscall"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/gomitmproxy"
"github.com/AdguardTeam/gomitmproxy/mitm"
"github.com/AdguardTeam/urlfilter/filterutil"
"github.com/AdguardTeam/urlfilter/proxy"
goFlags "github.com/jessevdk/go-flags"
)
Expand Down Expand Up @@ -108,9 +108,9 @@ func run(options Options) {
}

func createServerConfig(options Options) proxy.Config {
listenIP := filterutil.ParseIP(options.ListenAddr)
if listenIP == nil {
log.Fatalf("cannot parse %s", options.ListenAddr)
listenIP, err := netip.ParseAddr(options.ListenAddr)
if err != nil {
log.Fatalf("parsing listen addr: %s", err)
}

mitmConfig := createMITMConfig(options)
Expand All @@ -121,7 +121,8 @@ func createServerConfig(options Options) proxy.Config {
log.Fatalf("HTTPS hostname must be specified")
}

proxyCert, err := mitmConfig.GetOrCreateCert(options.HTTPSHostname)
var proxyCert *tls.Certificate
proxyCert, err = mitmConfig.GetOrCreateCert(options.HTTPSHostname)
if err != nil {
log.Fatalf("failed to generate HTTPS proxy certificate for %s: %v", options.HTTPSHostname, err)
}
Expand All @@ -141,7 +142,8 @@ func createServerConfig(options Options) proxy.Config {
config.FiltersPaths[i] = v
}

addr := &net.TCPAddr{IP: listenIP, Port: options.ListenPort}
// TODO(e.burkov): Use netip.AddrPort when gomitmproxy will support it.
addr := &net.TCPAddr{IP: listenIP.AsSlice(), Port: options.ListenPort}
config.ProxyConfig = gomitmproxy.Config{
ListenAddr: addr,
TLSConfig: tlsConfig,
Expand Down
33 changes: 20 additions & 13 deletions dnsengine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package urlfilter

import (
"net/netip"

"github.com/AdguardTeam/urlfilter/filterlist"
"github.com/AdguardTeam/urlfilter/filterutil"
"github.com/AdguardTeam/urlfilter/rules"
Expand Down Expand Up @@ -32,9 +34,10 @@ type DNSResult struct {
// rules.
NetworkRule *rules.NetworkRule

// HostRulesV4 and HostRulesV6 are the host rules with IPv4 and IPv6
// addresses respectively.
// HostRulesV4 are the host rules with IPv4 addresses.
HostRulesV4 []*rules.HostRule

// HostRulesV6 are the host rules with IPv6 addresses.
HostRulesV6 []*rules.HostRule

// NetworkRules are all matched network rules. These include unprocessed
Expand All @@ -44,22 +47,26 @@ type DNSResult struct {

// DNSRequest represents a DNS query with associated metadata.
type DNSRequest struct {
// Hostname is a hostname (or IP address).
Hostname string
// ClientIP is the address of client.
ClientIP string
// ClientName is the name of client.
// ClientIP is the IP address to match against $client modifiers. The
// default zero value won't be considered.
ClientIP netip.Addr

// ClientName is the name to match against $client modifiers. The default
// empty value won't be considered.
ClientName string

// SortedClientTags is the sorted list of client tags ($ctag).
// Hostname is the hostname to filter.
Hostname string

// SortedClientTags is the list of tags to match against $ctag modifiers.
SortedClientTags []string

// DNSType is the type of the resource record (RR) of a DNS request, for
// example "A" or "AAAA". See package github.com/miekg/dns for all
// acceptable constants.
// example "A" or "AAAA". See [rules.RRValue] for all acceptable constants
// and their corresponding values.
DNSType rules.RRType

// Answer is true if this hostname or IP is from a DNS response.
// Answer if the filtering request is for filtering a DNS response.
Answer bool
}

Expand Down Expand Up @@ -116,7 +123,7 @@ func NewDNSEngine(s *filterlist.RuleStorage) *DNSEngine {
// 192.168.0.1 example.local
// 2000::1 example.local
func (d *DNSEngine) Match(hostname string) (res *DNSResult, matched bool) {
return d.MatchRequest(&DNSRequest{Hostname: hostname, ClientIP: "0.0.0.0"})
return d.MatchRequest(&DNSRequest{Hostname: hostname})
}

// MatchRequest matches the specified DNS request. The return parameter matched
Expand Down Expand Up @@ -164,7 +171,7 @@ func (d *DNSEngine) MatchRequest(dReq *DNSRequest) (res *DNSResult, matched bool
continue
}

if hostRule.IP.To4() != nil {
if hostRule.IP.Is4() {
res.HostRulesV4 = append(res.HostRulesV4, hostRule)
} else {
res.HostRulesV6 = append(res.HostRulesV6, hostRule)
Expand Down
Loading

0 comments on commit 0ae94bb

Please sign in to comment.