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
* ✓ Config
* ✓ logging zap
* Proxy download
* ✓ Proxy download
* ✓ Storage csv
* Posting to telegram
* Storage csv
## MVP 2
* Podcasts

View File

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

View File

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

View File

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

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