-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook-proxy.js
More file actions
169 lines (133 loc) · 4.33 KB
/
webhook-proxy.js
File metadata and controls
169 lines (133 loc) · 4.33 KB
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
159
160
161
162
163
164
165
166
167
168
var fs = require("fs")
var concat = require("concat-stream")
var http = require('http');
var https = require('https');
if (require.main === module)
{
var config_pipe = process.stdin;
if (process.argv.length > 2)
config_pipe = fs.createReadStream(process.argv[2]);
config_pipe.pipe(concat(function(body) {
var config = JSON.parse(body);
setTimeout(webhook_proxy.startServer, 0, config);
}))
}
var webhook_proxy = module.exports = function () {
var config, publishers = [], listeners = [], output;
var server = {
WP_DEFAULT_PORT: 8777,
WP_UNRECOGNIZED: "unrecognized",
WP_BROADCAST: "broadcast",
WP_IGNORE: "ignore",
};
function init(conf)
{
config = conf || {};
if (typeof config == "string") {
config = JSON.parse(fs.readFileSync(config));
if (!config)
throw new Error("Couldn't parse config file");
}
server.config = config;
var level = config.level || "info";
var levels = {"trace": 10, "debug": 20, "info": 30, "warn": 40, "error": 50, "fatal": 60};
var output_pipe = config.logfile ? fs.createWriteStream(config.logfile, {flags: 'a'}) : process.stdout;
if (!output_pipe)
throw new Error("Couldn't open file [" + config.logfile + "] for writing.");
output = server.output = function(sev, str) {
if (levels[sev] >= levels[level])
output_pipe.write(sev + ': ' + str + '\n');
};
output("info", "Initializing webhook-proxy...");
config.port = config.port || server.WP_DEFAULT_PORT;
config.host = config.host || "localhost";
publishers = config.publishers || {};
listeners = config.listeners || {};
if (!publishers.length)
throw new Error("No publishers defined, aborting.");
if (!listeners.length)
throw new Error("No listeners defined, aborting.");
publishers.forEach(function(module) {
if (!module.name)
throw new Error("Invalid configuration, publisher module has no name");
var plugin = require('./lib/publishers/' + module.name);
if (!plugin)
throw new Error("No plugin exists for publisher module [" + module.name + "]");
module.plugin = new plugin(server, module);
});
listeners.forEach(function(module) {
if (!module.name)
throw new Error("Invalid configuration, listener module has no name");
var plugin = require('./lib/listeners/' + module.name);
if (!plugin)
throw new Error("No plugin exists for listener module [" + module.name + "]");
module.plugin = new plugin(server, module);
});
}
function handleRequest(req, res) {
var e;
req.pipe(concat(function (body) {
var incoming;
if (publishers.every(function(module) {
if ( (incoming = module.plugin.processRequest(body)) && incoming.status != server.WP_UNRECOGNIZED)
{
if (incoming.status == server.WP_BROADCAST)
{
broadcast(incoming);
output("debug", "request handled by plugin [" + module.name + "], broadcasting to listeners");
} else {
output("debug", "request tossed by plugin [" + module.name + "]");
}
return false;
}
return true;
})) {
output("error", "None of the configured plugins recognized the last event.");
output("debug", "Event body:\n" + body);
res.statusCode = 400;
res.statusMessage = "Unhandled webhook";
}
res.end()
}));
}
function broadcast(event)
{
listeners.every(function(module) {
if (module.plugin.proxyWebhook(event))
output("debug", "Event sent to module [" + module.name + "]");
else {
output("error", "Plugin for [" + module.name + "] failed to send event.");
output("debug", "Event: \n" + JSON.stringify(event, null, 2));
}
return true;
})
}
server.startServer = function(conf) {
try {
init(conf);
var s = http.createServer(function(req, res) {
if (req.method === 'POST') {
handleRequest(req, res);
} else {
output('warn', "Got non-POST request: " + req.method + ' ' + req.url);
res.statusCode = 405;
res.statusMessage = "POST only";
}
res.end();
}).listen(config.port, config.host);
output('info', 'webhook-proxy listening on ' + config.host + ':' + config.port);
process.on('SIGINT', function() {
s.close();
output('info', 'webhook-proxy has closed.');
process.exit();
});
} catch (e) {
if (typeof output != 'undefined')
output('fatal', 'unhandled error: ' + e.toString());
else
console.log(e.toString());
process.exit(1);
}
}
return server;
} ();