fix proxy

This commit is contained in:
Balakrishnan Balasubramanian 2022-05-01 19:13:27 -04:00
parent 38e964c522
commit 5e55fea0fd
6 changed files with 72 additions and 14 deletions

View File

@ -4,9 +4,9 @@
* ✓ Cron * ✓ Cron
* ✓ Config * ✓ Config
* ✓ logging zap * ✓ logging zap
* Proxy download * ✓ Proxy download
* ✓ Storage csv
* Posting to telegram * Posting to telegram
* Storage csv
## MVP 2 ## MVP 2
* Podcasts * Podcasts

View File

@ -22,6 +22,8 @@ func Run(configPath string) {
} }
}() }()
// tgram := NewTelegramSender(cfg.TelegramProxy)
for _, feed := range cfg.Feeds { for _, feed := range cfg.Feeds {
log.Info("processing feed", "feed", feed.Name) log.Info("processing feed", "feed", feed.Name)
ProcessFeed(feed, scheduler, cfg.DbDir) ProcessFeed(feed, scheduler, cfg.DbDir)

View File

@ -17,6 +17,8 @@ type FeedCfg struct {
} }
type Config struct { type Config struct {
Proxy string `yaml:"proxy"`
TelegramProxy string `yaml:"telegram_proxy"`
DataDir string `yaml:"data_dir"` DataDir string `yaml:"data_dir"`
LastSuccessPath string `yaml:"last_loaded_path"` LastSuccessPath string `yaml:"last_loaded_path"`
DbDir string `yaml:"db_dir"` DbDir string `yaml:"db_dir"`
@ -49,5 +51,24 @@ func ParseConfig(configPath string) (*Config, error) {
c.DbDir = path.Join(c.DataDir, "feed_data") 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 return &c, nil
} }

View File

@ -19,4 +19,5 @@ func main() {
log.Panicln(err) log.Panicln(err)
} }
fmt.Printf("status: %v, data: %s\n", res.Status, data) fmt.Printf("status: %v, data: %s\n", res.Status, data)
fmt.Printf("%#v\n", client.Transport)
} }

26
exp/ref/main.go Normal file
View File

@ -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)
}
}

View File

@ -9,6 +9,9 @@ import (
) )
func GetClient(proxy string) (*http.Client, error) { func GetClient(proxy string) (*http.Client, error) {
if proxy == "" {
return http.DefaultClient, nil
}
proxyUrl, err := url.Parse(proxy) proxyUrl, err := url.Parse(proxy)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse proxyUrl, url:%s, err: %w", proxy, err) 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) ctxDialer, ok := dialer.(proxy.ContextDialer)
if !ok { 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")
} }
trans := defaultTransport()
return &http.Client{ trans.DialContext = ctxDialer.DialContext
Transport: &http.Transport{ trans.Proxy = nil
DialContext: ctxDialer.DialContext, return &http.Client{Transport: trans}, nil
},
}, nil
} }
func proxyHttp(proxyUrl *url.URL) (*http.Client, error) { func proxyHttp(proxyUrl *url.URL) (*http.Client, error) {
return &http.Client{ trans := defaultTransport()
Transport: &http.Transport{ trans.Proxy = http.ProxyURL(proxyUrl)
Proxy: http.ProxyURL(proxyUrl), return &http.Client{Transport: trans}, nil
}, }
}, 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
} }