Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simply HTTP/HTTPS proxy support
http.DefaultTransport uses the http_proxy and https_proxy env. var. by
default
  • Loading branch information
sandeepmistry committed Jun 14, 2017
commit cd2bb987ac0123697aa11948ceec1b8f1e3ee40e
45 changes: 5 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you export the env variables and then read them in another function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They could be externally configured via environment while not passed as parameters I believe, so we must handle both the cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also export them so the spawned sketches can access the proxy settings, this is how Ubuntu shares them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I would pass them directly to the function after exporting them. And I would write a comment explaining why we export them.

To handle environmental variables we could maybe use https://github.com/namsral/flag

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm also going to explore using https://gowalker.org/golang.org/x/net/proxy#RegisterDialerType and https://godoc.org/golang.org/x/net/proxy#FromURL for the MQTT connection, this will clean things up as well.


// Setup MQTT connection
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down