From c426ee0108b6f4157df2a7af521e3fe3bbccde49 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 9 Jul 2019 18:37:24 +0300 Subject: [PATCH] * move "client", "transport" to "config" --- home/config.go | 15 +++++++++++++-- home/control.go | 9 --------- home/control_update.go | 4 ++-- home/filter.go | 2 +- home/home.go | 1 + 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/home/config.go b/home/config.go index 69ce0aae..ca576f45 100644 --- a/home/config.go +++ b/home/config.go @@ -2,6 +2,7 @@ package home import ( "io/ioutil" + "net/http" "os" "path/filepath" "runtime" @@ -54,6 +55,8 @@ type configuration struct { appSignalChannel chan os.Signal clients clientsContainer controlLock sync.Mutex + transport *http.Transport + client *http.Client BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server @@ -169,8 +172,16 @@ var config = configuration{ SchemaVersion: currentSchemaVersion, } -// init initializes default configuration for the current OS&ARCH -func init() { +// initConfig initializes default configuration for the current OS&ARCH +func initConfig() { + config.transport = &http.Transport{ + DialContext: customDialContext, + } + config.client = &http.Client{ + Timeout: time.Minute * 5, + Transport: config.transport, + } + if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" { // Use plain DNS on MIPS, encryption is too slow defaultDNS = []string{"1.1.1.1", "1.0.0.1"} diff --git a/home/control.go b/home/control.go index 0b3dd9e6..e6c41d92 100644 --- a/home/control.go +++ b/home/control.go @@ -30,15 +30,6 @@ var versionCheckLastTime time.Time var protocols = []string{"tls://", "https://", "tcp://", "sdns://"} -var transport = &http.Transport{ - DialContext: customDialContext, -} - -var client = &http.Client{ - Timeout: time.Minute * 5, - Transport: transport, -} - // ---------------- // helper functions // ---------------- diff --git a/home/control_update.go b/home/control_update.go index 2c58d036..c6babbf1 100644 --- a/home/control_update.go +++ b/home/control_update.go @@ -87,7 +87,7 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) { } log.Tracef("Downloading data from %s", versionCheckURL) - resp, err := client.Get(versionCheckURL) + resp, err := config.client.Get(versionCheckURL) if err != nil { httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", versionCheckURL, err, err) return @@ -349,7 +349,7 @@ func copySupportingFiles(files []string, srcdir, dstdir string, useSrcNameOnly, // Download package file and save it to disk func getPackageFile(u *updateInfo) error { - resp, err := client.Get(u.pkgURL) + resp, err := config.client.Get(u.pkgURL) if err != nil { return fmt.Errorf("HTTP request failed: %s", err) } diff --git a/home/filter.go b/home/filter.go index 1cd39935..ede0cf6f 100644 --- a/home/filter.go +++ b/home/filter.go @@ -308,7 +308,7 @@ func parseFilterContents(contents []byte) (int, string) { func (filter *filter) update() (bool, error) { log.Tracef("Downloading update for filter %d from %s", filter.ID, filter.URL) - resp, err := client.Get(filter.URL) + resp, err := config.client.Get(filter.URL) if resp != nil && resp.Body != nil { defer resp.Body.Close() } diff --git a/home/home.go b/home/home.go index 10801c05..fdcdf4cc 100644 --- a/home/home.go +++ b/home/home.go @@ -108,6 +108,7 @@ func run(args options) { os.Exit(0) }() + initConfig() config.clients.Init() if !config.firstRun {