complexorganizations / content-blocker

✔️ Content Blocker is a robust web filtering project aimed at enhancing online privacy and security.
Other
16 stars 1 forks source link

Use a different dns provider than the system one. #36

Closed ghost closed 3 years ago

ghost commented 3 years ago

Use google dns on this app and stop asking the user to change system dns.

Saugjunkie commented 3 years ago

????

ghost commented 3 years ago
package main

import (
    "context"
    "io/ioutil"
    "log"
    "net"
    "net/http"
    "time"
)

func main() {
    var (
        dnsResolverIP        = "8.8.8.8:53" // Google DNS resolver.
        dnsResolverProto     = "udp"        // Protocol to use for the DNS resolver
        dnsResolverTimeoutMs = 5000         // Timeout (ms) for the DNS resolver (optional)
    )

    dialer := &net.Dialer{
        Resolver: &net.Resolver{
            PreferGo: true,
            Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
                d := net.Dialer{
                    Timeout: time.Duration(dnsResolverTimeoutMs) * time.Millisecond,
                }
                return d.DialContext(ctx, dnsResolverProto, dnsResolverIP)
            },
        },
    }

    dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
        return dialer.DialContext(ctx, network, addr)
    }

    http.DefaultTransport.(*http.Transport).DialContext = dialContext
    httpClient := &http.Client{}

    // Testing the new HTTP client with the custom DNS resolver.
    resp, err := httpClient.Get("https://www.violetnorth.com")
    if err != nil {
        log.Fatalln(err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatalln(err)
    }

    log.Println(string(body))
}
ghost commented 3 years ago
package main

import (
    "context"
    "net"
    "time"
)

func main() {
    r := &net.Resolver{
        PreferGo: true,
        Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
            d := net.Dialer{
                Timeout: time.Millisecond * time.Duration(10000),
            }
            return d.DialContext(ctx, network, "1.1.1.1:53")
        },
    }
    ip, _ := r.LookupHost(context.Background(), "www.google.com")

    print(ip[0])
}