From 5e55fea0fd2e9f32269c5768f23ef75304401f87 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Sun, 1 May 2022 19:13:27 -0400 Subject: [PATCH] fix proxy --- TODO.md | 4 ++-- app/app.go | 2 ++ app/config.go | 21 +++++++++++++++++++++ exp/proxy/main.go | 1 + exp/ref/main.go | 26 ++++++++++++++++++++++++++ proxy/proxy.go | 32 ++++++++++++++++++++------------ 6 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 exp/ref/main.go diff --git a/TODO.md b/TODO.md index a58bd33..1bbe9d5 100644 --- a/TODO.md +++ b/TODO.md @@ -4,9 +4,9 @@ * ✓ Cron * ✓ Config * ✓ logging zap -* Proxy download +* ✓ Proxy download +* ✓ Storage csv * Posting to telegram -* Storage csv ## MVP 2 * Podcasts diff --git a/app/app.go b/app/app.go index be0fb06..28cc250 100644 --- a/app/app.go +++ b/app/app.go @@ -22,6 +22,8 @@ func Run(configPath string) { } }() + // tgram := NewTelegramSender(cfg.TelegramProxy) + for _, feed := range cfg.Feeds { log.Info("processing feed", "feed", feed.Name) ProcessFeed(feed, scheduler, cfg.DbDir) diff --git a/app/config.go b/app/config.go index 4aa1889..c08159f 100644 --- a/app/config.go +++ b/app/config.go @@ -17,6 +17,8 @@ type FeedCfg struct { } type Config struct { + Proxy string `yaml:"proxy"` + TelegramProxy string `yaml:"telegram_proxy"` DataDir string `yaml:"data_dir"` LastSuccessPath string `yaml:"last_loaded_path"` DbDir string `yaml:"db_dir"` @@ -49,5 +51,24 @@ func ParseConfig(configPath string) (*Config, error) { c.DbDir = path.Join(c.DataDir, "feed_data") } + if c.Proxy != "" { + if c.TelegramProxy == "" { + c.TelegramProxy = c.Proxy + } + for i, _ := range c.Feeds { + feedCfg := &c.Feeds[i] + if feedCfg.Proxy == "" { + feedCfg.Proxy = c.Proxy + } + } + } + + for i, _ := range c.Feeds { + feedCfg := &c.Feeds[i] + if feedCfg.Proxy == "NONE" { + feedCfg.Proxy = "" + } + } + return &c, nil } diff --git a/exp/proxy/main.go b/exp/proxy/main.go index f9e8d97..822b3d5 100644 --- a/exp/proxy/main.go +++ b/exp/proxy/main.go @@ -19,4 +19,5 @@ func main() { log.Panicln(err) } fmt.Printf("status: %v, data: %s\n", res.Status, data) + fmt.Printf("%#v\n", client.Transport) } diff --git a/exp/ref/main.go b/exp/ref/main.go new file mode 100644 index 0000000..524be9c --- /dev/null +++ b/exp/ref/main.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +type dummy struct { + name string +} + +func main() { + fmt.Println("vim-go") + dummies := []dummy{ + dummy{"foo"}, + dummy{"bar"}, + dummy{"baz"}, + } + + for i, _ := range dummies { + dum := &dummies[i] + dum.name = "hello" + } + + for _, dum := range dummies { + fmt.Println(dum.name) + } + +} diff --git a/proxy/proxy.go b/proxy/proxy.go index 84ddff9..8919484 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -9,6 +9,9 @@ import ( ) func GetClient(proxy string) (*http.Client, error) { + if proxy == "" { + return http.DefaultClient, nil + } proxyUrl, err := url.Parse(proxy) if err != nil { return nil, fmt.Errorf("failed to parse proxyUrl, url:%s, err: %w", proxy, err) @@ -28,20 +31,25 @@ func unixSocks5Proxy(path string) (*http.Client, error) { } ctxDialer, ok := dialer.(proxy.ContextDialer) if !ok { - return nil, fmt.Errorf("proxy.SOCKS5 did not return a ContextDialer") // This should never happen + panic("proxy.SOCKS5 did not return a ContextDialer") } - - return &http.Client{ - Transport: &http.Transport{ - DialContext: ctxDialer.DialContext, - }, - }, nil + trans := defaultTransport() + trans.DialContext = ctxDialer.DialContext + trans.Proxy = nil + return &http.Client{Transport: trans}, nil } func proxyHttp(proxyUrl *url.URL) (*http.Client, error) { - return &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyURL(proxyUrl), - }, - }, nil + trans := defaultTransport() + trans.Proxy = http.ProxyURL(proxyUrl) + return &http.Client{Transport: trans}, nil +} + +func defaultTransport() *http.Transport { + transPtr, ok := http.DefaultTransport.(*http.Transport) + if !ok { + panic("http.DefaultTransport is not a *http.Transport") + } + trans := transPtr.Clone() + return trans }