2015-10-23 21:31:14 +00:00
|
|
|
//Package matterhook provides interaction with mattermost incoming/outgoing webhooks
|
2015-10-23 15:09:08 +00:00
|
|
|
package matterhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-10-24 23:00:19 +00:00
|
|
|
"crypto/tls"
|
2015-10-23 15:09:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2016-07-11 21:22:56 +00:00
|
|
|
"net"
|
2015-10-23 15:09:08 +00:00
|
|
|
"net/http"
|
2017-02-21 21:13:34 +00:00
|
|
|
"time"
|
2018-06-09 10:47:40 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/schema"
|
2020-03-01 19:59:19 +00:00
|
|
|
"github.com/slack-go/slack"
|
2015-10-23 15:09:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// OMessage for mattermost incoming webhook. (send to mattermost)
|
|
|
|
type OMessage struct {
|
2017-09-18 21:43:21 +00:00
|
|
|
Channel string `json:"channel,omitempty"`
|
|
|
|
IconURL string `json:"icon_url,omitempty"`
|
|
|
|
IconEmoji string `json:"icon_emoji,omitempty"`
|
|
|
|
UserName string `json:"username,omitempty"`
|
|
|
|
Text string `json:"text"`
|
2018-02-22 23:48:25 +00:00
|
|
|
Attachments []slack.Attachment `json:"attachments,omitempty"`
|
2017-09-18 21:43:21 +00:00
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Props map[string]interface{} `json:"props"`
|
2015-10-23 15:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IMessage for mattermost outgoing webhook. (received from mattermost)
|
|
|
|
type IMessage struct {
|
2016-09-05 14:34:37 +00:00
|
|
|
BotID string `schema:"bot_id"`
|
|
|
|
BotName string `schema:"bot_name"`
|
2015-10-23 15:09:08 +00:00
|
|
|
Token string `schema:"token"`
|
|
|
|
TeamID string `schema:"team_id"`
|
|
|
|
TeamDomain string `schema:"team_domain"`
|
|
|
|
ChannelID string `schema:"channel_id"`
|
|
|
|
ChannelName string `schema:"channel_name"`
|
|
|
|
Timestamp string `schema:"timestamp"`
|
|
|
|
UserID string `schema:"user_id"`
|
|
|
|
UserName string `schema:"user_name"`
|
2018-11-15 19:43:43 +00:00
|
|
|
PostId string `schema:"post_id"` //nolint:golint
|
2016-09-05 14:34:37 +00:00
|
|
|
RawText string `schema:"raw_text"`
|
2018-11-15 19:43:43 +00:00
|
|
|
ServiceId string `schema:"service_id"` //nolint:golint
|
2015-10-23 15:09:08 +00:00
|
|
|
Text string `schema:"text"`
|
|
|
|
TriggerWord string `schema:"trigger_word"`
|
2017-08-16 19:27:18 +00:00
|
|
|
FileIDs string `schema:"file_ids"`
|
2015-10-23 15:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client for Mattermost.
|
|
|
|
type Client struct {
|
2018-11-15 19:43:43 +00:00
|
|
|
// URL for incoming webhooks on mattermost.
|
|
|
|
Url string // nolint:golint
|
2015-10-24 23:00:19 +00:00
|
|
|
In chan IMessage
|
|
|
|
Out chan OMessage
|
|
|
|
httpclient *http.Client
|
2015-10-23 15:09:08 +00:00
|
|
|
Config
|
|
|
|
}
|
|
|
|
|
2015-10-24 16:01:15 +00:00
|
|
|
// Config for client.
|
2015-10-23 15:09:08 +00:00
|
|
|
type Config struct {
|
2015-12-12 20:26:53 +00:00
|
|
|
BindAddress string // Address to listen on
|
2015-10-24 23:00:19 +00:00
|
|
|
Token string // Only allow this token from Mattermost. (Allow everything when empty)
|
|
|
|
InsecureSkipVerify bool // disable certificate checking
|
2015-10-28 16:18:05 +00:00
|
|
|
DisableServer bool // Do not start server for outgoing webhooks from Mattermost.
|
2015-10-23 15:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New Mattermost client.
|
|
|
|
func New(url string, config Config) *Client {
|
2015-10-28 16:18:05 +00:00
|
|
|
c := &Client{Url: url, In: make(chan IMessage), Out: make(chan OMessage), Config: config}
|
2015-10-24 23:00:19 +00:00
|
|
|
tr := &http.Transport{
|
2018-12-05 23:40:55 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.InsecureSkipVerify}, //nolint:gosec
|
2015-10-24 23:00:19 +00:00
|
|
|
}
|
|
|
|
c.httpclient = &http.Client{Transport: tr}
|
2015-10-28 16:18:05 +00:00
|
|
|
if !c.DisableServer {
|
2016-07-17 20:15:19 +00:00
|
|
|
_, _, err := net.SplitHostPort(c.BindAddress)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("incorrect bindaddress %s", c.BindAddress)
|
|
|
|
}
|
2015-10-28 16:18:05 +00:00
|
|
|
go c.StartServer()
|
|
|
|
}
|
2015-10-23 15:09:08 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartServer starts a webserver listening for incoming mattermost POSTS.
|
|
|
|
func (c *Client) StartServer() {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", c)
|
2017-02-21 21:13:34 +00:00
|
|
|
srv := &http.Server{
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
Handler: mux,
|
|
|
|
Addr: c.BindAddress,
|
|
|
|
}
|
2016-07-11 21:22:56 +00:00
|
|
|
log.Printf("Listening on http://%v...\n", c.BindAddress)
|
2017-02-21 21:13:34 +00:00
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
2015-10-23 15:09:08 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implementation.
|
|
|
|
func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-10-24 15:44:14 +00:00
|
|
|
if r.Method != "POST" {
|
|
|
|
log.Println("invalid " + r.Method + " connection from " + r.RemoteAddr)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-10-23 15:09:08 +00:00
|
|
|
msg := IMessage{}
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
decoder := schema.NewDecoder()
|
|
|
|
err = decoder.Decode(&msg, r.PostForm)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-10-24 15:44:14 +00:00
|
|
|
if msg.Token == "" {
|
|
|
|
log.Println("no token from " + r.RemoteAddr)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2015-10-24 16:01:15 +00:00
|
|
|
if c.Token != "" {
|
|
|
|
if msg.Token != c.Token {
|
|
|
|
log.Println("invalid token " + msg.Token + " from " + r.RemoteAddr)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 15:09:08 +00:00
|
|
|
c.In <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receive returns an incoming message from mattermost outgoing webhooks URL.
|
|
|
|
func (c *Client) Receive() IMessage {
|
2017-07-13 22:35:01 +00:00
|
|
|
var msg IMessage
|
|
|
|
for msg := range c.In {
|
|
|
|
return msg
|
2015-10-23 15:09:08 +00:00
|
|
|
}
|
2017-07-13 22:35:01 +00:00
|
|
|
return msg
|
2015-10-23 15:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send sends a msg to mattermost incoming webhooks URL.
|
|
|
|
func (c *Client) Send(msg OMessage) error {
|
|
|
|
buf, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-28 16:18:05 +00:00
|
|
|
resp, err := c.httpclient.Post(c.Url, "application/json", bytes.NewReader(buf))
|
2015-10-23 15:09:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Read entire body to completion to re-use keep-alive connections.
|
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|