From 21c96238181f60ee7aac08cab95dceb0f5f7ef8e Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Tue, 13 Jun 2017 14:44:36 +0200 Subject: [PATCH 1/3] Use proxy info from config to export env. vars and for HTTP(S) downloads --- main.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index f9bbe7b2..9e0664b6 100644 --- a/main.go +++ b/main.go @@ -10,10 +10,12 @@ import ( "log" "net" "net/http" + "net/url" "os" "os/exec" "path/filepath" "strconv" + "strings" "syscall" "time" @@ -31,15 +33,21 @@ const ( func main() { var ( - id = flag.String("id", "", "id of the thing in aws iot") - uuid = flag.String("uuid", "", "A uuid generated the first time the connector is started") - url = flag.String("url", "", "url of the thing in aws iot") + id = flag.String("id", "", "id of the thing in aws iot") + uuid = flag.String("uuid", "", "A uuid generated the first time the connector is started") + url = flag.String("url", "", "url of the thing in aws iot") + http_proxy = flag.String("http_proxy", "", "URL of HTTP proxy to use") + https_proxy = flag.String("https_proxy", "", "URL of HTTPS proxy to use") + all_proxy = flag.String("all_proxy", "", "URL of SOCKS proxy to use") ) // Read configuration iniflags.SetConfigFile(configFile) iniflags.Parse() + // Export the proxy info + exportProxyEnvVars(http_proxy, https_proxy, all_proxy) + // Setup MQTT connection client, err := setupMQTTConnection("certificate.pem", "certificate.key", *id, *url) check(err) @@ -314,7 +322,10 @@ func downloadFile(filepath, url, token string) error { } defer out.Close() // Get the data - client := http.Client{} + client, err := proxiedHttpClient(url) + if err != nil { + return err + } req, err := http.NewRequest("GET", url, nil) if err != nil { return err @@ -340,6 +351,39 @@ func downloadFile(filepath, url, token string) error { return nil } +func proxiedHttpClient(downloadUrl string) (http.Client, error) { + client := http.Client{} + + httpProxy := os.Getenv("http_proxy") + httpsProxy := os.Getenv("https_proxy") + allProxy := os.Getenv("all_proxy") + + var rawProxyURL string = "" + + if strings.HasPrefix(downloadUrl, "https://") && httpsProxy != "" { + rawProxyURL = httpsProxy + } else if strings.HasPrefix(downloadUrl, "http://") && httpProxy != "" { + rawProxyURL = httpProxy + } else if allProxy != "" { + rawProxyURL = allProxy + } + + if rawProxyURL != "" { + proxyURL, err := url.Parse(rawProxyURL) + if err != nil { + return client, err + } + transport := http.Transport{ + Proxy: http.ProxyURL(proxyURL), + } + client = http.Client{ + Transport: &transport, + } + } + + return client, nil +} + // spawn Process creates a new process from a file func spawnProcess(filepath string) (int, io.ReadCloser, error) { cmd := exec.Command(filepath) @@ -349,3 +393,17 @@ func spawnProcess(filepath string) (int, io.ReadCloser, error) { } return cmd.Process.Pid, stdout, err } + +func exportProxyEnvVars(httpproxy, httpsproxy, allproxy *string) { + if httpproxy != nil && *httpproxy != "" { + os.Setenv("http_proxy", *httpproxy) + } + + if httpsproxy != nil && *httpsproxy != "" { + os.Setenv("https_proxy", *httpsproxy) + } + + if allproxy != nil && *allproxy != "" { + os.Setenv("all_proxy", *allproxy) + } +} From cd2bb987ac0123697aa11948ceec1b8f1e3ee40e Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 14 Jun 2017 15:42:56 +0200 Subject: [PATCH 2/3] Simply HTTP/HTTPS proxy support http.DefaultTransport uses the http_proxy and https_proxy env. var. by default --- main.go | 45 +++++---------------------------------------- 1 file changed, 5 insertions(+), 40 deletions(-) diff --git a/main.go b/main.go index 9e0664b6..b6423bb6 100644 --- a/main.go +++ b/main.go @@ -10,12 +10,10 @@ import ( "log" "net" "net/http" - "net/url" "os" "os/exec" "path/filepath" "strconv" - "strings" "syscall" "time" @@ -45,7 +43,10 @@ func main() { iniflags.SetConfigFile(configFile) iniflags.Parse() - // Export the proxy info + // Export the proxy info as environments variables, so that: + // - http.DefaultTransport can use the proxy settings + // - any spawned sketch process'es also have access to them + // Note, all_proxy will not be used by any HTTP/HTTPS connections. exportProxyEnvVars(http_proxy, https_proxy, all_proxy) // Setup MQTT connection @@ -322,10 +323,7 @@ func downloadFile(filepath, url, token string) error { } defer out.Close() // Get the data - client, err := proxiedHttpClient(url) - if err != nil { - return err - } + client := http.Client{} req, err := http.NewRequest("GET", url, nil) if err != nil { return err @@ -351,39 +349,6 @@ func downloadFile(filepath, url, token string) error { return nil } -func proxiedHttpClient(downloadUrl string) (http.Client, error) { - client := http.Client{} - - httpProxy := os.Getenv("http_proxy") - httpsProxy := os.Getenv("https_proxy") - allProxy := os.Getenv("all_proxy") - - var rawProxyURL string = "" - - if strings.HasPrefix(downloadUrl, "https://") && httpsProxy != "" { - rawProxyURL = httpsProxy - } else if strings.HasPrefix(downloadUrl, "http://") && httpProxy != "" { - rawProxyURL = httpProxy - } else if allProxy != "" { - rawProxyURL = allProxy - } - - if rawProxyURL != "" { - proxyURL, err := url.Parse(rawProxyURL) - if err != nil { - return client, err - } - transport := http.Transport{ - Proxy: http.ProxyURL(proxyURL), - } - client = http.Client{ - Transport: &transport, - } - } - - return client, nil -} - // spawn Process creates a new process from a file func spawnProcess(filepath string) (int, io.ReadCloser, error) { cmd := exec.Command(filepath) From 50112becccba5c98c80b1e212f1609f471f03d3b Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 14 Jun 2017 15:44:03 +0200 Subject: [PATCH 3/3] Set server name option for MQTT client --- main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index b6423bb6..5bd02a55 100644 --- a/main.go +++ b/main.go @@ -218,7 +218,10 @@ func setupMQTTConnection(cert, key, id, url string) (mqtt.Client, error) { opts := mqtt.NewClientOptions() // This line is different, we use the constructor function instead of creating the instance ourselves. opts.SetClientID(id) opts.SetMaxReconnectInterval(1 * time.Second) - opts.SetTLSConfig(&tls.Config{Certificates: []tls.Certificate{cer}}) + opts.SetTLSConfig(&tls.Config{ + Certificates: []tls.Certificate{cer}, + ServerName: url, + }) port := 8883 path := "/mqtt"