Fixed requests router and added possibility to connect to Telegram via HTTP proxy (fixes #2).

This commit is contained in:
Stanislav Nikitin 2019-03-05 18:32:34 +05:00
parent a0f3ddfa16
commit b13cf6ecec
5 changed files with 66 additions and 19 deletions

View File

@ -15,9 +15,10 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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"`
}

View File

@ -27,4 +27,10 @@ matrix:
telegram:
telegram_test:
bot_id: "bot:id"
chat_id: "chat_id or -chat_id"
chat_id: "chat_id or -chat_id"
proxy:
enabled: false
type: "http"
address: "localhost:3128"
user: ""
password: ""

View File

@ -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() {

View File

@ -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)
}
}

View File

@ -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") {