all: wait for network up before running

If ctrld setup the interface correctly, the interface DNS is set to
ctrld listener address. At boot time, the ctrld is not up yet, so it
would break the processing Control D config fetching.

Fixing this by waiting for network up before doing the query.
This commit is contained in:
Cuong Manh Le
2023-01-21 13:19:05 +07:00
committed by Cuong Manh Le
parent cd37d93b06
commit 837563dcd5
3 changed files with 56 additions and 3 deletions
+24 -1
View File
@@ -2,8 +2,10 @@ package controld
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"time"
)
@@ -13,6 +15,20 @@ const (
InvalidConfigCode = 40401
)
const bootstrapDNS = "76.76.2.0:53"
var dialer = &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: 10 * time.Second,
}
return d.DialContext(ctx, "udp", bootstrapDNS)
},
},
}
// ResolverConfig represents Control D resolver data.
type ResolverConfig struct {
DOH string `json:"doh"`
@@ -52,7 +68,14 @@ func FetchResolverConfig(uid string) (*ResolverConfig, error) {
q.Set("platform", "ctrld")
req.URL.RawQuery = q.Encode()
req.Header.Add("Content-Type", "application/json")
client := http.Client{Timeout: 5 * time.Second}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, addr)
}
client := http.Client{
Timeout: 10 * time.Second,
Transport: transport,
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("client.Do: %w", err)