diff --git a/config/struct/struct.go b/config/struct/struct.go index b366199..34206fb 100644 --- a/config/struct/struct.go +++ b/config/struct/struct.go @@ -15,9 +15,10 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . + package configstruct -// Config's root. +// ConfigStruct is a config's root. type ConfigStruct struct { SlackHandler ConfigSlackHandler `yaml:"slackhandler"` Webhooks map[string]ConfigWebhook `yaml:"webhooks"` @@ -59,8 +60,19 @@ type ConfigMatrix struct { Room string `yaml:"room"` } -// Telegram pusher configuration +// ConfigTelegram is a telegram pusher configuration type ConfigTelegram struct { - BotID string `yaml:"bot_id"` - ChatID string `yaml:"chat_id"` + BotID string `yaml:"bot_id"` + ChatID string `yaml:"chat_id"` + Proxy ConfigProxy `yaml:"proxy"` +} + +// ConfigProxy represents proxy server configuration. +type ConfigProxy struct { + // ProxyType is a proxy type. Currently ignored. + Enabled bool `yaml:"enabled"` + ProxyType string `yaml:"proxy_type"` + Address string `yaml:"address"` + User string `yaml:"user"` + Password string `yaml:"password"` } diff --git a/opensaps.example.yaml b/opensaps.example.yaml index 775144b..bde82df 100644 --- a/opensaps.example.yaml +++ b/opensaps.example.yaml @@ -27,4 +27,10 @@ matrix: telegram: telegram_test: bot_id: "bot:id" - chat_id: "chat_id or -chat_id" \ No newline at end of file + chat_id: "chat_id or -chat_id" + proxy: + enabled: false + type: "http" + address: "localhost:3128" + user: "" + password: "" \ No newline at end of file diff --git a/pushers/telegram/telegramconnection.go b/pushers/telegram/telegramconnection.go index 302cab7..77da1eb 100644 --- a/pushers/telegram/telegramconnection.go +++ b/pushers/telegram/telegramconnection.go @@ -26,19 +26,16 @@ import ( "strings" // local + "gitlab.com/pztrn/opensaps/config/struct" "gitlab.com/pztrn/opensaps/slack/message" ) type TelegramConnection struct { - botId string - chatId string - connName string + config configstruct.ConfigTelegram } -func (tc *TelegramConnection) Initialize(connName string, botId string, chatId string) { - tc.connName = connName - tc.chatId = chatId - tc.botId = botId +func (tc *TelegramConnection) Initialize(connName string, cfg configstruct.ConfigTelegram) { + tc.config = cfg } func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) { @@ -140,15 +137,47 @@ func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) func (tc *TelegramConnection) SendMessage(message string) { msgdata := url.Values{} - msgdata.Set("chat_id", tc.chatId) + msgdata.Set("chat_id", tc.config.ChatID) msgdata.Set("text", message) msgdata.Set("parse_mode", "HTML") - client := &http.Client{} - botUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.botId) + // Are we should use proxy? + httpTransport := &http.Transport{} + if tc.config.Proxy.Enabled { + // Compose proxy URL. + proxyURL := "http://" + if tc.config.Proxy.User != "" { + proxyURL += tc.config.Proxy.User + if tc.config.Proxy.Password != "" { + proxyURL += ":" + tc.config.Proxy.Password + } + proxyURL += "@" + } + proxyURL += tc.config.Proxy.Address + + proxyURLParsed, err := url.Parse(proxyURL) + if err != nil { + c.Log.Errorln("Error while constructing/parsing proxy URL:", err.Error()) + } else { + httpTransport.Proxy = http.ProxyURL(proxyURLParsed) + } + } + + client := &http.Client{Transport: httpTransport} + botUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.config.BotID) c.Log.Debugln("Bot URL:", botUrl) - response, _ := client.PostForm(botUrl, msgdata) - c.Log.Debugln("Status:", response.Status) + response, err := client.PostForm(botUrl, msgdata) + if err != nil { + c.Log.Errorln("Error ocured while sending data to Telegram:", err.Error()) + } else { + c.Log.Debugln("Status:", response.Status) + if response.StatusCode != 200 { + body := []byte{} + _, _ = response.Body.Read(body) + response.Body.Close() + c.Log.Debugln(body) + } + } } func (tc *TelegramConnection) Shutdown() { diff --git a/pushers/telegram/telegrampusher.go b/pushers/telegram/telegrampusher.go index 9af3c27..9aac91c 100644 --- a/pushers/telegram/telegrampusher.go +++ b/pushers/telegram/telegrampusher.go @@ -33,7 +33,7 @@ func (tp TelegramPusher) Initialize() { c.Log.Infof("Initializing connection: '%s'", name) conn := TelegramConnection{} connections[name] = &conn - go conn.Initialize(name, config.BotID, config.ChatID) + go conn.Initialize(name, config) } } diff --git a/slack/slackhandler.go b/slack/slackhandler.go index 24c7426..661e321 100644 --- a/slack/slackhandler.go +++ b/slack/slackhandler.go @@ -55,7 +55,7 @@ func (sh SlackHandler) ServeHTTP(respwriter http.ResponseWriter, req *http.Reque var sent_to_pusher bool = false for name, config := range cfg.Webhooks { - if strings.Contains(url_splitted[2], config.Slack.Random1) && strings.Contains(url_splitted[3], config.Slack.Random2) && strings.Contains(url_splitted[4], config.Slack.LongRandom) { + if strings.Contains(req.URL.Path, config.Slack.Random1) && strings.Contains(req.URL.Path, config.Slack.Random2) && strings.Contains(req.URL.Path, config.Slack.LongRandom) { c.Log.Debugf("Passed data belongs to '%s' and should go to '%s' pusher, protocol '%s'", name, config.Remote.PushTo, config.Remote.Pusher) // Parse message into SlackMessage structure. if strings.Contains(string(body)[0:7], "payload") {