feat(proxy): support http proxy

This commit is contained in:
iyear 2023-11-27 21:21:47 +08:00
parent 04f7116c21
commit 4a839cbb2b
4 changed files with 32 additions and 8 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/gotd/td/telegram/dcs"
"github.com/spf13/viper"
"go.uber.org/zap"
"golang.org/x/net/proxy"
"golang.org/x/time/rate"
"github.com/iyear/tdl/pkg/clock"
@ -75,9 +76,19 @@ func New(ctx context.Context, login bool, middlewares ...telegram.Middleware) (*
}
appId, appHash := app.AppID, app.AppHash
// process proxy
var dialer dcs.DialFunc = proxy.Direct.DialContext
if p := viper.GetString(consts.FlagProxy); p != "" {
d, err := utils.Proxy.GetDial(p)
if err != nil {
return nil, nil, errors.Wrap(err, "get dialer")
}
dialer = d.DialContext
}
opts := telegram.Options{
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: utils.Proxy.GetDial(viper.GetString(consts.FlagProxy)).DialContext,
Dial: dialer,
}),
ReconnectionBackoff: func() backoff.BackOff {
return Backoff(_clock)

1
go.mod
View File

@ -66,6 +66,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/iyear/connectproxy v0.1.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect

4
go.sum
View File

@ -208,6 +208,10 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/iyear/connectproxy v0.1.0 h1:Sz+Dsu5iAWpxpcVjMmYu6OyZPtN5M65mxJ30NzzdZdM=
github.com/iyear/connectproxy v0.1.0/go.mod h1:yD4zOmSMQCmwHIT4fk8mg4k2M15z8VoMSoeY6NNJdsA=
github.com/iyear/connectproxy v0.1.1 h1:JZOF/62vvwRGBWcgSyWRb0BpKD4FSs0++B5/y5pNE4c=
github.com/iyear/connectproxy v0.1.1/go.mod h1:yD4zOmSMQCmwHIT4fk8mg4k2M15z8VoMSoeY6NNJdsA=
github.com/jedib0t/go-pretty/v6 v6.4.9 h1:vZ6bjGg2eBSrJn365qlxGcaWu09Id+LHtrfDWlB2Usc=
github.com/jedib0t/go-pretty/v6 v6.4.9/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=

View File

@ -3,6 +3,8 @@ package utils
import (
"net/url"
"github.com/go-faster/errors"
"github.com/iyear/connectproxy"
"golang.org/x/net/proxy"
)
@ -10,19 +12,25 @@ type _proxy struct{}
var Proxy = _proxy{}
func (p _proxy) GetDial(_url string) proxy.ContextDialer {
func init() {
connectproxy.Register(&connectproxy.Config{
InsecureSkipVerify: true,
})
}
func (p _proxy) GetDial(_url string) (proxy.ContextDialer, error) {
u, err := url.Parse(_url)
if err != nil {
return proxy.Direct
return nil, errors.Wrap(err, "parse proxy url")
}
dialer, err := proxy.FromURL(u, proxy.Direct)
if err != nil {
return proxy.Direct
return nil, errors.Wrap(err, "proxy from url")
}
d, ok := dialer.(proxy.ContextDialer)
if !ok {
return proxy.Direct
if d, ok := dialer.(proxy.ContextDialer); ok {
return d, nil
}
return d
return nil, errors.New("proxy dialer is not ContextDialer")
}