This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
74 lines (62 loc) · 2.28 KB
/
server.py
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
#!/usr/bin/env python3
# th 2019
# loosely based on
# https://github.com/mraerino/zammad-smtp-receiver/blob/master/server.py
from http.server import BaseHTTPRequestHandler, HTTPServer
from subprocess import Popen, PIPE
import socketserver
import cgi
import os
import hashlib
import hmac
import sys
sys.stdout = sys.stderr
def verify_token(token, timestamp, signature):
hmac_digest = hmac.new(key=os.environ['MAILGUN_API_KEY'].encode('utf-8'),
msg='{}{}'.format(timestamp, token).encode('utf-8'),
digestmod=hashlib.sha256).hexdigest()
return hmac.compare_digest(signature.encode('utf-8'), hmac_digest.encode('utf-8'))
class S(BaseHTTPRequestHandler):
def _set_headers(self, code=200):
self.send_response(code)
self.send_header('Content-type', 'text/plain')
self.end_headers()
def do_HEAD(self):
self._set_headers()
def do_POST(self):
print("new message")
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
})
if verify_token(form.getvalue('token'), form.getvalue('timestamp'), form.getvalue('signature')):
print("token ok")
message = form.getvalue('body-mime')
cmd = "bundle exec rails r 'Channel::Driver::MailStdin.new(trusted: true)'"
process = Popen(cmd, shell=True, cwd=os.environ['ZAMMAD_DIR'], stdin=PIPE)
pipe = process.stdin
pipe.write(message.encode('utf-8'))
pipe.close()
process.wait()
if process.returncode == 0:
print("processed")
self._set_headers(200)
else:
print("not processed")
self._set_headers(500)
else:
print("token not ok")
self._set_headers(403)
def run(server_class=HTTPServer, handler_class=S, port=1337):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting get-mail...')
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()