diff --git a/cmd/root.go b/cmd/root.go index aaf61b1..c55fdd9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -78,6 +78,11 @@ func load() { } defer logger.Sync() LOG = logger.Sugar() + + if CFG.Proxy != "" { + LOG.Debugf("set proxy: %s", CFG.Proxy) + os.Setenv("HTTP_PROXY", CFG.Proxy) + } } func loadSystems(c *ctx.Ctx) []error { @@ -85,6 +90,7 @@ func loadSystems(c *ctx.Ctx) []error { for _, sysCfg := range c.Config.Systems { c.Logger.Debugf("loading system of type %s ...", sysCfg.Type) + sysCfg.Config["proxy"] = CFG.Proxy sys, err := system.New(sysCfg.Type, &sysCfg.Config, LOG) if err != nil { c.Logger.Errorf("error loading system: %s", err) diff --git a/config/config.go b/config/config.go index 9d92e77..78efbb8 100644 --- a/config/config.go +++ b/config/config.go @@ -41,6 +41,7 @@ type ThemeItemConfig struct { type Config struct { Debug bool Log string + Proxy string RenderImages bool @@ -185,6 +186,7 @@ func (cfg *Config) Save() error { func SetDefaults(cacheDir string) { viper.SetDefault("Debug", "false") viper.SetDefault("Log", path.Join(cacheDir, "neonmodem.log")) + viper.SetDefault("Proxy", "") viper.SetDefault("RenderImages", "true") diff --git a/system/discourse/api/client.go b/system/discourse/api/client.go index 71439e7..bdbc375 100644 --- a/system/discourse/api/client.go +++ b/system/discourse/api/client.go @@ -11,6 +11,7 @@ import ( "net/http" "net/url" "strings" + "time" "github.com/hashicorp/go-retryablehttp" ) @@ -74,13 +75,34 @@ type Client struct { func NewDefaultClientConfig( endpoint string, + proxy string, credentials map[string]string, logger Logger, ) ClientConfig { + var httpClient *http.Client = nil + var httpTransport *http.Transport = nil + + if proxy != "" { + proxyURL, err := url.Parse(proxy) + if err != nil { + logger.Error(err) + } else { + logger.Debugf("setting up http proxy transport: %s\n", proxyURL.String()) + httpTransport = &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + } + } + } + + httpClient = &http.Client{ + Transport: httpTransport, + Timeout: time.Second * 10, + } + return ClientConfig{ Endpoint: endpoint, Credentials: credentials, - HTTPClient: http.DefaultClient, + HTTPClient: httpClient, Logger: logger, } } diff --git a/system/discourse/discourse.go b/system/discourse/discourse.go index 5951c82..d148ed7 100644 --- a/system/discourse/discourse.go +++ b/system/discourse/discourse.go @@ -3,7 +3,6 @@ package discourse import ( "context" "fmt" - "net/http" "net/url" "strconv" "time" @@ -20,10 +19,11 @@ import ( ) type System struct { - ID int - config map[string]interface{} - logger *zap.SugaredLogger - client *api.Client + ID int + config map[string]interface{} + logger *zap.SugaredLogger + client *api.Client + clientCfg api.ClientConfig } func (sys *System) GetID() int { @@ -113,12 +113,13 @@ func (sys *System) Load() error { credentials[k] = v.(string) } - sys.client = api.NewClient(&api.ClientConfig{ - Endpoint: url.(string), - Credentials: credentials, - HTTPClient: http.DefaultClient, - Logger: sys.logger, - }) + sys.clientCfg = api.NewDefaultClientConfig( + url.(string), + sys.config["proxy"].(string), + credentials, + sys.logger, + ) + sys.client = api.NewClient(&sys.clientCfg) return nil } diff --git a/system/lobsters/api/client.go b/system/lobsters/api/client.go index f5606c3..dfc7583 100644 --- a/system/lobsters/api/client.go +++ b/system/lobsters/api/client.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "net/url" + "time" "github.com/hashicorp/go-retryablehttp" ) @@ -70,13 +71,34 @@ type Client struct { func NewDefaultClientConfig( endpoint string, + proxy string, credentials map[string]string, logger Logger, ) ClientConfig { + var httpClient *http.Client = nil + var httpTransport *http.Transport = nil + + if proxy != "" { + proxyURL, err := url.Parse(proxy) + if err != nil { + logger.Error(err) + } else { + logger.Debugf("setting up http proxy transport: %s\n", proxyURL.String()) + httpTransport = &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + } + } + } + + httpClient = &http.Client{ + Transport: httpTransport, + Timeout: time.Second * 10, + } + return ClientConfig{ Endpoint: endpoint, Credentials: credentials, - HTTPClient: http.DefaultClient, + HTTPClient: httpClient, Logger: logger, } } diff --git a/system/lobsters/lobsters.go b/system/lobsters/lobsters.go index 3fdae94..a2ee968 100644 --- a/system/lobsters/lobsters.go +++ b/system/lobsters/lobsters.go @@ -3,7 +3,6 @@ package lobsters import ( "context" "fmt" - "net/http" "net/url" "time" @@ -19,10 +18,11 @@ import ( ) type System struct { - ID int - config map[string]interface{} - logger *zap.SugaredLogger - client *api.Client + ID int + config map[string]interface{} + logger *zap.SugaredLogger + client *api.Client + clientCfg api.ClientConfig } func (sys *System) GetID() int { @@ -110,12 +110,13 @@ func (sys *System) Load() error { credentials := make(map[string]string) - sys.client = api.NewClient(&api.ClientConfig{ - Endpoint: url.(string), - Credentials: credentials, - HTTPClient: http.DefaultClient, - Logger: sys.logger, - }) + sys.clientCfg = api.NewDefaultClientConfig( + url.(string), + sys.config["proxy"].(string), + credentials, + sys.logger, + ) + sys.client = api.NewClient(&sys.clientCfg) return nil }