This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from deputinizer/fix-optimize
Optimizations + target DNS injection
- Loading branch information
Showing
18 changed files
with
733 additions
and
457 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package customresolver | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
var getIPcachedResolver = &CustomResolver{ | ||
ParentResolver: InjectionGoResolver, | ||
} | ||
|
||
// Modified to use stoppropaganda's CustomResolver | ||
// so that it caches DNS records | ||
func CustomLookupIP(host string, helperIPBuf []net.IP) ([]net.IP, error) { | ||
addrs, err := getIPcachedResolver.LookupIPAddr(context.Background(), host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ips := helperIPBuf[:0] | ||
for _, ia := range addrs { | ||
ips = append(ips, ia.IP) | ||
} | ||
return ips, nil | ||
} | ||
|
||
func GetIPs(host string, helperIPBuf []net.IP) (ips []net.IP, err error) { | ||
addr := net.ParseIP(host) | ||
if addr == nil { | ||
ips, err := CustomLookupIP(host, helperIPBuf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for i := 0; i < len(ips); i++ { | ||
ip := ips[i] | ||
if ipv4 := ip.To4(); ipv4 == nil { | ||
// Remove inplace trick | ||
// - swap i and last element | ||
ips[i] = ips[len(ips)-1] | ||
// - pop last element | ||
ips[len(ips)-1] = nil | ||
ips = ips[:len(ips)-1] | ||
} | ||
} | ||
return ips, nil | ||
} | ||
helperIPBuf[0] = addr | ||
return helperIPBuf[:1], nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
internal/stoppropaganda/customresolver/injectionresolver.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package customresolver | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/erkexzcx/stoppropaganda/internal/stoppropaganda/targets" | ||
) | ||
|
||
var InjectionGoResolver = &net.Resolver{ | ||
PreferGo: true, | ||
Dial: func(ctx context.Context, network, address string) (conn net.Conn, err error) { | ||
d := net.Dialer{ | ||
Timeout: time.Millisecond * time.Duration(1000), | ||
} | ||
|
||
// use DNS targets from dns.go | ||
for _, dnsTarget := range targets.ReferenceDNSServersForHTTP { | ||
// eg. d.DialContext(ctx, "tcp", "194.54.14.186:53") | ||
conn, err = d.DialContext(ctx, network, dnsTarget) | ||
if err == nil { | ||
// return first working conn to DNS | ||
return | ||
} | ||
} | ||
|
||
return d.DialContext(ctx, network, address) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.