-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
158 lines (140 loc) · 3.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"encoding/json"
"fmt"
"io"
"net/url"
"github.com/gin-gonic/gin"
"github.com/gotify/plugin-api"
)
const routeName = "webhook"
const (
ContentTypeUnknown = iota
ContentTypeJSON
ContentTypeMarkdown
)
// GetGotifyPluginInfo returns gotify plugin info
func GetGotifyPluginInfo() plugin.Info {
return plugin.Info{
Name: "Webhooks",
Description: "Plugin that enables Gotify to receive generic webhooks",
ModulePath: "git.leon.wtf/leon/gotify-webhook-plugin",
Author: "Leon Schmidt <[email protected]>",
Website: "https://leon-schmidt.dev",
}
}
// Plugin is plugin instance
type Plugin struct {
userCtx plugin.UserContext
msgHandler plugin.MessageHandler
basePath string
}
// Enable implements plugin.Plugin
func (p *Plugin) Enable() error {
return nil
}
// Disable implements plugin.Plugin
func (p *Plugin) Disable() error {
return nil
}
const helpMessageTemplate = "Use this **webhook URL**: %s\n\n" +
"You can set the content type of the payload via the `content-type` query parameter (e.g. `%s?content-type=application/json`) or the `content-type` or `x-content-type` request headers.\n\n" +
"The following content types are supported: `application/json`, `text/markdown`"
// GetDisplay implements plugin.Displayer
func (p *Plugin) GetDisplay(location *url.URL) string {
baseHost := ""
if location != nil {
baseHost = fmt.Sprintf("%s://%s", location.Scheme, location.Host)
}
webhookURL := baseHost + p.basePath + routeName
return fmt.Sprintf(helpMessageTemplate, webhookURL, webhookURL)
}
// SetMessageHandler implements plugin.Messenger
func (p *Plugin) SetMessageHandler(h plugin.MessageHandler) {
// invoced during initialization
p.msgHandler = h
}
// RegisterWebhook implements plugin.Webhooker
func (p *Plugin) RegisterWebhook(basePath string, mux *gin.RouterGroup) {
p.basePath = basePath
webhookHandler := func(c *gin.Context) {
// read body
bytes, err := io.ReadAll(c.Request.Body)
if err != nil {
p.msgHandler.SendMessage(makeMarkdownMessage(
"Error reading request body",
err.Error(),
c.ClientIP(),
false,
))
return
}
contentType := getContentTypeFromRequest(c.Request)
// if content type is unknown, try JSON anyway (some clients cannot set a content-type)
if contentType == ContentTypeUnknown {
var data interface{}
err = json.Unmarshal(bytes, &data)
if err == nil {
contentType = ContentTypeJSON
}
}
switch contentType {
case ContentTypeJSON:
// try to parse json to verify format
var data interface{}
err = json.Unmarshal(bytes, &data)
if err != nil {
p.msgHandler.SendMessage(makeMarkdownMessage(
"Error parsing JSON message",
err.Error(),
c.ClientIP(),
false,
))
return
}
// re-indent JSON
jsonStr, err := json.MarshalIndent(data, "", " ")
if err != nil {
p.msgHandler.SendMessage(makeMarkdownMessage(
"Error re-marshalling payload",
err.Error(),
c.ClientIP(),
true,
))
return
}
p.msgHandler.SendMessage(makeMarkdownMessage(
"Recieved webhook with JSON",
string(jsonStr),
c.ClientIP(),
true,
))
case ContentTypeMarkdown:
p.msgHandler.SendMessage(makeMarkdownMessage(
"Recieved webhook with Markdown",
string(bytes),
c.ClientIP(),
false,
))
case ContentTypeUnknown:
// just send the string
p.msgHandler.SendMessage(makeMarkdownMessage(
"Recieved webhook with unknown content type",
string(bytes),
c.ClientIP(),
true,
))
}
}
mux.POST("/"+routeName, webhookHandler)
mux.PUT("/"+routeName, webhookHandler)
}
// NewGotifyPluginInstance creates a plugin instance for a user context.
func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin {
return &Plugin{
userCtx: ctx,
}
}
func main() {
panic("this should be built as go plugin")
}