From d284956268d06f499c9d8ff7363e0b870232078e Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Mon, 2 May 2022 11:36:24 -0400 Subject: [PATCH] improve proxy --- app/config.go | 4 ++++ exp/funint/main.go | 35 +++++++++++++++++++++++++++++++++++ exp/proxy/main.go | 6 +++++- log/log.go | 1 + proxy/proxy.go | 32 ++++++++++++++++++++++++-------- 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 exp/funint/main.go diff --git a/app/config.go b/app/config.go index ce73040..34f10dd 100644 --- a/app/config.go +++ b/app/config.go @@ -65,6 +65,10 @@ func ParseConfig(configPath string) (*Config, error) { } } + if c.TelegramProxy == "NONE" { + c.TelegramProxy = "" + } + for i, _ := range c.Feeds { feedCfg := &c.Feeds[i] if feedCfg.Proxy == "NONE" { diff --git a/exp/funint/main.go b/exp/funint/main.go new file mode 100644 index 0000000..b053f3e --- /dev/null +++ b/exp/funint/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "net" +) + +type Foo interface { + Bar() +} + +func bar() { + fmt.Println("bar") +} + +func run(f Foo) { + f.Bar() +} + +type t struct { + Bar func() +} + +type t2 struct { +} + +func (t t2) Bar() { + bar() +} + +func main() { + run(t2{}) + var d net.Dialer + d.Cancel +} diff --git a/exp/proxy/main.go b/exp/proxy/main.go index 822b3d5..36941a4 100644 --- a/exp/proxy/main.go +++ b/exp/proxy/main.go @@ -4,12 +4,16 @@ import ( "fmt" "io" "log" + "net/http" "go.balki.me/tss/proxy" ) func main() { - client, err := proxy.GetClient("socks5://unix/run/tor/socks") + trans, err := proxy.GetTransport("socks5://unix/run/tor/socks") + client := &http.Client{ + Transport: trans, + } res, err := client.Get("https://ip.balki.me") if err != nil { log.Panicln(err) diff --git a/log/log.go b/log/log.go index 2e48b6c..7df8b48 100644 --- a/log/log.go +++ b/log/log.go @@ -10,6 +10,7 @@ var Debug, Info, Warn, Error, Panic func(msg string, keysAndValues ...interface{ func init() { + //json logging cfg := zap.NewProductionConfig() devCfg := zap.NewDevelopmentConfig() diff --git a/proxy/proxy.go b/proxy/proxy.go index 9dc2d6b..77f0ade 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -1,7 +1,9 @@ package proxy import ( + "context" "fmt" + "net" "net/http" "net/url" @@ -20,12 +22,28 @@ func GetTransport(proxy string) (http.RoundTripper, error) { len(proxyUrl.Path) > 1 /* Path cannot be empty or just / */ { return unixSocks5Proxy(proxyUrl.Path) } - return proxyHttp(proxyUrl) + return proxyTcp(proxyUrl) +} + +type forwardDialer struct { + dialContext func(ctx context.Context, network, address string) (net.Conn, error) +} + +func (d *forwardDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { + return d.dialContext(ctx, network, address) +} +func (d *forwardDialer) Dial(network, address string) (net.Conn, error) { + panic("Dial should not be called") + //default Dial is nil + //return defaultTransport().Dial(network, address) } func unixSocks5Proxy(path string) (http.RoundTripper, error) { - // TODO: Auth? - dialer, err := proxy.SOCKS5("unix", path, nil /*auth*/, nil) + trans := defaultTransport().Clone() + if trans.DialContext == nil { + panic("DefaultTransport has nil DialContext") + } + dialer, err := proxy.SOCKS5("unix", path, nil /*auth*/, &forwardDialer{trans.DialContext}) if err != nil { return nil, fmt.Errorf("failed to make socks proxy, path: %s, err: %w", path, err) } @@ -33,14 +51,13 @@ func unixSocks5Proxy(path string) (http.RoundTripper, error) { if !ok { panic("proxy.SOCKS5 did not return a ContextDialer") } - trans := defaultTransport() trans.DialContext = ctxDialer.DialContext trans.Proxy = nil return trans, nil } -func proxyHttp(proxyUrl *url.URL) (http.RoundTripper, error) { - trans := defaultTransport() +func proxyTcp(proxyUrl *url.URL) (http.RoundTripper, error) { + trans := defaultTransport().Clone() trans.Proxy = http.ProxyURL(proxyUrl) return trans, nil } @@ -50,6 +67,5 @@ func defaultTransport() *http.Transport { if !ok { panic("http.DefaultTransport is not a *http.Transport") } - trans := transPtr.Clone() - return trans + return transPtr }