From abba2cb37ddffbe49014a912e9f881370dba90bc Mon Sep 17 00:00:00 2001 From: icecream78 Date: Sat, 7 Dec 2019 06:29:47 +0300 Subject: [PATCH] Add socks5 proxy to plugin (#87) --- main.go | 6 ++++++ plugin.go | 19 +++++++++++++++++-- plugin_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 0525209..889d081 100644 --- a/main.go +++ b/main.go @@ -245,6 +245,11 @@ func main() { Usage: "Provides the target deployment environment for the running build. This value is only available to promotion and rollback pipelines.", EnvVar: "DRONE_DEPLOY_TO", }, + cli.StringFlag{ + Name: "socks5", + Usage: "Socks5 proxy URL", + EnvVar: "PLUGIN_SOCKS5,SOCKS5", + }, } if err := app.Run(os.Args); err != nil { @@ -310,6 +315,7 @@ func run(c *cli.Context) error { Venue: c.StringSlice("venue"), Format: c.String("format"), GitHub: c.Bool("github"), + Socks5: c.String("socks5"), }, } diff --git a/plugin.go b/plugin.go index 6c5bf9c..b6e9e6b 100644 --- a/plugin.go +++ b/plugin.go @@ -8,6 +8,8 @@ import ( "html" "io/ioutil" "log" + "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -83,6 +85,7 @@ type ( Venue []string Format string GitHub bool + Socks5 string } // Plugin values. @@ -273,9 +276,21 @@ func (p Plugin) Exec() (err error) { } } + var proxyURL *url.URL + if proxyURL, err = url.Parse(p.Config.Socks5); err != nil { + return fmt.Errorf("unable to unmarshall socks5 proxy url from string '%s': %v", p.Config.Socks5, err) + } + var bot *tgbotapi.BotAPI - if bot, err = tgbotapi.NewBotAPI(p.Config.Token); err != nil { - return + if len(p.Config.Socks5) > 0 { + proxyClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}} + bot, err = tgbotapi.NewBotAPIWithClient(p.Config.Token, proxyClient) + } else { + bot, err = tgbotapi.NewBotAPI(p.Config.Token) + } + + if err != nil { + return err } bot.Debug = p.Config.Debug diff --git a/plugin_test.go b/plugin_test.go index 943a40d..f77e677 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -385,3 +385,54 @@ func TestTemplateVars(t *testing.T) { err := plugin.Exec() assert.Nil(t, err) } + +func TestProxySendMessage(t *testing.T) { + plugin := Plugin{ + Repo: Repo{ + Name: "go-hello", + Namespace: "appleboy", + }, + Commit: Commit{ + Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", + Author: "Bo-Yi Wu", + Branch: "master", + Message: "start use proxy", + Email: "test@gmail.com", + }, + Build: Build{ + Tag: "1.0.0", + Number: 101, + Status: "success", + Link: "https://github.com/appleboy/go-hello", + }, + + Config: Config{ + Token: os.Getenv("TELEGRAM_TOKEN"), + To: []string{os.Getenv("TELEGRAM_TO"), os.Getenv("TELEGRAM_TO") + ":appleboy@gmail.com", "中文ID", "1234567890"}, + Message: []string{"Test Telegram Chat Bot From Travis or Local", "commit message: 『{{ build.message }}』", " "}, + Photo: []string{"tests/github.png", "1234", " "}, + Document: []string{"tests/gophercolor.png", "1234", " "}, + Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "}, + Audio: []string{"tests/audio.mp3", "1234", " "}, + Voice: []string{"tests/voice.ogg", "1234", " "}, + Location: []string{"24.9163213 121.1424972", "1", " "}, + Venue: []string{"35.661777 139.704051 竹北體育館 新竹縣竹北市", "24.9163213 121.1424972", "1", " "}, + Video: []string{"tests/video.mp4", "1234", " "}, + Debug: false, + Socks5: os.Getenv("SOCKS5"), + }, + } + + err := plugin.Exec() + assert.NotNil(t, err) + + plugin.Config.Format = formatMarkdown + plugin.Config.Message = []string{"Test escape under_score"} + err = plugin.Exec() + assert.NotNil(t, err) + + // disable message + plugin.Config.Message = []string{} + err = plugin.Exec() + assert.NotNil(t, err) +}