Fixed requests router and added possibility to connect to Telegram via HTTP proxy (fixes #2).
This commit is contained in:
parent
a0f3ddfa16
commit
b13cf6ecec
@ -15,9 +15,10 @@
|
|||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package configstruct
|
package configstruct
|
||||||
|
|
||||||
// Config's root.
|
// ConfigStruct is a config's root.
|
||||||
type ConfigStruct struct {
|
type ConfigStruct struct {
|
||||||
SlackHandler ConfigSlackHandler `yaml:"slackhandler"`
|
SlackHandler ConfigSlackHandler `yaml:"slackhandler"`
|
||||||
Webhooks map[string]ConfigWebhook `yaml:"webhooks"`
|
Webhooks map[string]ConfigWebhook `yaml:"webhooks"`
|
||||||
@ -59,8 +60,19 @@ type ConfigMatrix struct {
|
|||||||
Room string `yaml:"room"`
|
Room string `yaml:"room"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Telegram pusher configuration
|
// ConfigTelegram is a telegram pusher configuration
|
||||||
type ConfigTelegram struct {
|
type ConfigTelegram struct {
|
||||||
BotID string `yaml:"bot_id"`
|
BotID string `yaml:"bot_id"`
|
||||||
ChatID string `yaml:"chat_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"`
|
||||||
}
|
}
|
||||||
|
@ -28,3 +28,9 @@ telegram:
|
|||||||
telegram_test:
|
telegram_test:
|
||||||
bot_id: "bot:id"
|
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: ""
|
@ -26,19 +26,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
// local
|
// local
|
||||||
|
"gitlab.com/pztrn/opensaps/config/struct"
|
||||||
"gitlab.com/pztrn/opensaps/slack/message"
|
"gitlab.com/pztrn/opensaps/slack/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TelegramConnection struct {
|
type TelegramConnection struct {
|
||||||
botId string
|
config configstruct.ConfigTelegram
|
||||||
chatId string
|
|
||||||
connName string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *TelegramConnection) Initialize(connName string, botId string, chatId string) {
|
func (tc *TelegramConnection) Initialize(connName string, cfg configstruct.ConfigTelegram) {
|
||||||
tc.connName = connName
|
tc.config = cfg
|
||||||
tc.chatId = chatId
|
|
||||||
tc.botId = botId
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
||||||
@ -140,15 +137,47 @@ func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage)
|
|||||||
|
|
||||||
func (tc *TelegramConnection) SendMessage(message string) {
|
func (tc *TelegramConnection) SendMessage(message string) {
|
||||||
msgdata := url.Values{}
|
msgdata := url.Values{}
|
||||||
msgdata.Set("chat_id", tc.chatId)
|
msgdata.Set("chat_id", tc.config.ChatID)
|
||||||
msgdata.Set("text", message)
|
msgdata.Set("text", message)
|
||||||
msgdata.Set("parse_mode", "HTML")
|
msgdata.Set("parse_mode", "HTML")
|
||||||
|
|
||||||
client := &http.Client{}
|
// Are we should use proxy?
|
||||||
botUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.botId)
|
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)
|
c.Log.Debugln("Bot URL:", botUrl)
|
||||||
response, _ := client.PostForm(botUrl, msgdata)
|
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)
|
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() {
|
func (tc *TelegramConnection) Shutdown() {
|
||||||
|
@ -33,7 +33,7 @@ func (tp TelegramPusher) Initialize() {
|
|||||||
c.Log.Infof("Initializing connection: '%s'", name)
|
c.Log.Infof("Initializing connection: '%s'", name)
|
||||||
conn := TelegramConnection{}
|
conn := TelegramConnection{}
|
||||||
connections[name] = &conn
|
connections[name] = &conn
|
||||||
go conn.Initialize(name, config.BotID, config.ChatID)
|
go conn.Initialize(name, config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ func (sh SlackHandler) ServeHTTP(respwriter http.ResponseWriter, req *http.Reque
|
|||||||
|
|
||||||
var sent_to_pusher bool = false
|
var sent_to_pusher bool = false
|
||||||
for name, config := range cfg.Webhooks {
|
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)
|
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.
|
// Parse message into SlackMessage structure.
|
||||||
if strings.Contains(string(body)[0:7], "payload") {
|
if strings.Contains(string(body)[0:7], "payload") {
|
||||||
|
Loading…
Reference in New Issue
Block a user