diff --git a/cli/setup/setup.go b/cli/setup/setup.go index 3245d8b2e..8449e4e85 100644 --- a/cli/setup/setup.go +++ b/cli/setup/setup.go @@ -63,11 +63,13 @@ func InteractiveSetup(conf *util.ConfigType) { if conf.TelegramAlert { askValue("Telegram bot token (you can get it from @BotFather)", "", &conf.TelegramToken) askValue("Telegram chat ID", "", &conf.TelegramChat) + askValue("Telegram Proxy URL", "", &conf.AlertUrlProxy) } askConfirmation("Enable slack alerts?", false, &conf.SlackAlert) if conf.SlackAlert { askValue("Slack Webhook URL", "", &conf.SlackUrl) + askValue("Slack Proxy URL", "", &conf.AlertUrlProxy) } askConfirmation("Enable LDAP authentication?", false, &conf.LdapEnable) diff --git a/services/tasks/alert.go b/services/tasks/alert.go index 4f833a54e..21c242c27 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -2,12 +2,14 @@ package tasks import ( "bytes" - "github.com/ansible-semaphore/semaphore/lib" - "github.com/ansible-semaphore/semaphore/util" "html/template" "net/http" + "net/url" "strconv" "strings" + + "github.com/ansible-semaphore/semaphore/lib" + "github.com/ansible-semaphore/semaphore/util" ) const emailTemplate = "Subject: Task '{{ .Name }}' failed\r\n" + @@ -153,6 +155,18 @@ func (t *TaskRunner) sendTelegramAlert() { panic(err) } + httpTransport := &http.Transport{} + if len(util.Config.AlertUrlProxy) != 0 { // Set the proxy only if the proxy param is specified + alertUrlProxy, proxyErr := url.Parse(util.Config.AlertUrlProxy) + if proxyErr == nil { + httpTransport.Proxy = http.ProxyURL(alertUrlProxy) + } + if proxyErr != nil { + t.Log("Can't send slack alert! Error: " + proxyErr.Error()) + } + } + http := http.Client{Transport: httpTransport} + resp, err := http.Post("https://api.telegram.org/bot"+util.Config.TelegramToken+"/sendMessage", "application/json", &telegramBuffer) if err != nil { @@ -173,6 +187,18 @@ func (t *TaskRunner) sendSlackAlert() { slackUrl := util.Config.SlackUrl + httpTransport := &http.Transport{} + if len(util.Config.AlertUrlProxy) != 0 { // Set the proxy only if the proxy param is specified + alertUrlProxy, proxyErr := url.Parse(util.Config.AlertUrlProxy) + if proxyErr == nil { + httpTransport.Proxy = http.ProxyURL(alertUrlProxy) + } + if proxyErr != nil { + t.Log("Can't send slack alert! Error: " + proxyErr.Error()) + } + } + http := http.Client{Transport: httpTransport} + var slackBuffer bytes.Buffer var version string diff --git a/util/config.go b/util/config.go index 3d053da3c..b4abd7b89 100644 --- a/util/config.go +++ b/util/config.go @@ -160,6 +160,7 @@ type ConfigType struct { LdapNeedTLS bool `json:"ldap_needtls" env:"SEMAPHORE_LDAP_NEEDTLS"` // telegram and slack alerting + AlertUrlProxy string `json:"alert_url_proxy" env:"SEMAPHORE_ALERT_PROXY_URL"` TelegramAlert bool `json:"telegram_alert" env:"SEMAPHORE_TELEGRAM_ALERT"` TelegramChat string `json:"telegram_chat" env:"SEMAPHORE_TELEGRAM_CHAT"` TelegramToken string `json:"telegram_token" env:"SEMAPHORE_TELEGRAM_TOKEN"`