Add socks5 proxy to plugin (#87)

This commit is contained in:
icecream78 2019-12-07 06:29:47 +03:00 committed by Bo-Yi Wu
parent 7f8bd6ba04
commit abba2cb37d
3 changed files with 74 additions and 2 deletions

View File

@ -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.", Usage: "Provides the target deployment environment for the running build. This value is only available to promotion and rollback pipelines.",
EnvVar: "DRONE_DEPLOY_TO", EnvVar: "DRONE_DEPLOY_TO",
}, },
cli.StringFlag{
Name: "socks5",
Usage: "Socks5 proxy URL",
EnvVar: "PLUGIN_SOCKS5,SOCKS5",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -310,6 +315,7 @@ func run(c *cli.Context) error {
Venue: c.StringSlice("venue"), Venue: c.StringSlice("venue"),
Format: c.String("format"), Format: c.String("format"),
GitHub: c.Bool("github"), GitHub: c.Bool("github"),
Socks5: c.String("socks5"),
}, },
} }

View File

@ -8,6 +8,8 @@ import (
"html" "html"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -83,6 +85,7 @@ type (
Venue []string Venue []string
Format string Format string
GitHub bool GitHub bool
Socks5 string
} }
// Plugin values. // 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 var bot *tgbotapi.BotAPI
if bot, err = tgbotapi.NewBotAPI(p.Config.Token); err != nil { if len(p.Config.Socks5) > 0 {
return 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 bot.Debug = p.Config.Debug

View File

@ -385,3 +385,54 @@ func TestTemplateVars(t *testing.T) {
err := plugin.Exec() err := plugin.Exec()
assert.Nil(t, err) 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)
}