Skip to content
This repository has been archived by the owner on Dec 25, 2022. It is now read-only.

Commit

Permalink
add DNS caching
Browse files Browse the repository at this point in the history
  • Loading branch information
erkexzcx committed Mar 3, 2022
1 parent f943d83 commit c6e2041
Show file tree
Hide file tree
Showing 12 changed files with 1,523 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
)

require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/valyala/fasthttp v1.33.0
golang.org/x/mod v0.5.1 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/klauspost/compress v1.14.4 h1:eijASRJcobkVtSt81Olfh7JX43osYLwy5krOJo6
github.com/klauspost/compress v1.14.4/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/miekg/dns v1.1.46 h1:uzwpxRtSVxtcIZmz/4Uz6/Rn7G11DvsaslXoy5LxQio=
github.com/miekg/dns v1.1.46/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/peterbourgon/ff/v3 v3.1.2 h1:0GNhbRhO9yHA4CC27ymskOsuRpmX0YQxwxM9UPiP6JM=
github.com/peterbourgon/ff/v3 v3.1.2/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
Expand Down
35 changes: 35 additions & 0 deletions internal/stoppropaganda/customresolver/customresolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package customresolver

import (
"context"
"net"
"time"

"github.com/patrickmn/go-cache"
)

var resolver *net.Resolver
var dnscache *cache.Cache

type CustomResolver struct{}

type Resolver interface {
LookupIPAddr(context.Context, string) (names []net.IPAddr, err error)
}

func (cs *CustomResolver) LookupIPAddr(ctx context.Context, host string) (names []net.IPAddr, err error) {
if c, found := dnscache.Get(host); found {
return c.([]net.IPAddr), nil
}

names, err = resolver.LookupIPAddr(ctx, host)
if err == nil {
dnscache.SetDefault(host, names)
}
return
}

func init() {
resolver = net.DefaultResolver
dnscache = cache.New(5*time.Minute, 10*time.Minute)
}
2 changes: 2 additions & 0 deletions internal/stoppropaganda/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type DNSServer struct {
target string
}

var dnsClient *dns.Client

var dnsServers = map[string]*DNSServer{}

func startDNS() {
Expand Down
9 changes: 4 additions & 5 deletions internal/stoppropaganda/stoppropaganda.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"time"

"github.com/erkexzcx/stoppropaganda/internal/stoppropaganda/customresolver"
"github.com/erkexzcx/stoppropaganda/internal/stoppropaganda/sockshttp"
"github.com/miekg/dns"
"github.com/peterbourgon/ff/v3"
Expand All @@ -36,10 +37,6 @@ func Start() {
panic(fasthttp.ListenAndServe(*flagBind, fasthttpRequestHandler))
}

var httpClient *fasthttp.Client

var dnsClient *dns.Client

func init() {
rand.Seed(time.Now().UnixNano())

Expand Down Expand Up @@ -82,12 +79,14 @@ func makeDialFunc() fasthttp.DialFunc {
masterDialer = MakeDialerThrough(dialer, proxyChain, proxyTimeout)
}

myResolver := &customresolver.CustomResolver{}
dial := (&TCPDialer{
Concurrency: math.MaxInt,
Concurrency: 0,
DNSCacheDuration: 5 * time.Minute,

// stoppropaganda's implementation
ParentDialer: masterDialer,
Resolver: myResolver,
}).Dial
return dial
}
15 changes: 12 additions & 3 deletions internal/stoppropaganda/websites.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ type Website struct {
req *fasthttp.Request
}

var httpClient *fasthttp.Client

var websites = map[string]*Website{}

func startWebsites() {
Expand Down Expand Up @@ -412,10 +414,17 @@ func runWebsiteWorker(c chan *Website) {
website.dnsLastChecked = time.Now()

if err != nil {
if strings.HasSuffix(err.Error(), "Temporary failure in name resolution") || strings.HasSuffix(err.Error(), "connection refused") {
website.mux.Lock()
website.Status = "Your DNS servers unreachable or returned an error"
website.mux.Unlock()
time.Sleep(1 * time.Second)
website.pauseMux.Unlock()
continue
}

website.mux.Lock()
switch {
case strings.HasSuffix(err.Error(), "Temporary failure in name resolution"):
website.Status = "Your DNS servers unreachable or returned an error"
case strings.HasSuffix(err.Error(), "no such host"):
website.Status = "Domain does not exist"
case strings.HasSuffix(err.Error(), "No address associated with hostname"):
Expand All @@ -426,7 +435,7 @@ func runWebsiteWorker(c chan *Website) {
website.paused = true
website.mux.Unlock()

time.Sleep(5 * time.Minute)
time.Sleep(10 * time.Second)
website.pauseMux.Unlock()
continue
}
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/patrickmn/go-cache/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/patrickmn/go-cache/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions vendor/github.com/patrickmn/go-cache/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c6e2041

Please sign in to comment.