-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.py
289 lines (232 loc) · 8.74 KB
/
webhook.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/python3 -u
helptext = """IFTTT Webhook for json requests to control a windows PC.
Supported commands: wake, suspend, poweroff, poweroff_linux, reboot_linux"""
import argparse
import configparser
import pprint
pp = pprint.PrettyPrinter(width=1)
import logging
import binascii
import socket
import struct
import http.server
import socketserver
import ssl
import os.path
import cgi
import json
import time
import subprocess
import urllib.request
parser = argparse.ArgumentParser(description=helptext)
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-m", "--mock", action="store_true",
help="""don't actually cause any actions within the network""")
parser.add_argument('command', nargs='?',
help="""command to execute directly instead of starting the server""")
parser.add_argument('argument', nargs='?',
help="""argument for the command""")
parser.add_argument("-q", "--quiet", help="set logging to ERROR",
action="store_const", dest="loglevel",
const=logging.ERROR, default=logging.INFO)
parser.add_argument("-d", "--debug", help="set logging to DEBUG",
action="store_const", dest="loglevel",
const=logging.DEBUG, default=logging.INFO)
args = parser.parse_args()
config = configparser.ConfigParser()
config.read('webhook.ini', encoding='utf-8')
# WOL send_magic_packet conf
MAC = config['wake']['mac'] # if not given as 2nd argument
BROADCAST_IP = '255.255.255.255' # for config['win']['host'] comment out sock.setsockopt below
DEFAULT_PORT = int(config['wake']['port'])
logging.basicConfig(level=args.loglevel,
format='%(levelname)-8s %(message)s')
_log = logging.getLogger('webhook')
# Command dispatcher and implementations
def command(cmd, arg):
"""Command dispatcher. Only interpret known commands, otherwise return False."""
if cmd is None:
return False
_log.info("Received command {0} with argument {1}".format(cmd, arg))
if cmd == 'wake':
wake(arg)
elif cmd == 'suspend':
suspend()
elif cmd == 'poweroff':
poweroff_win(arg)
elif cmd == 'poweroff_linux':
poweroff_linux()
elif cmd == 'reboot_linux':
reboot_linux()
elif cmd == 'poweroff_buero':
poweroff_buero()
else:
_log.error("Unknown command {0}".format(cmd))
return False
return True
def wake(mac):
if mac is None:
mac = MAC
_log.debug("Wake up {0}".format(mac))
if not args.mock:
send_magic_packet(mac)
def suspend():
_log.debug("Suspend {0}".format(config['win']['host']))
remote_command("psshutdown -d -t 00 -v 00",
config['win']['user'],
config['win']['host'])
def poweroff_win(userhost):
if userhost is None:
user = config['win']['user']
host = config['win']['host']
else:
[user, host] = userhost.split('@')
_log.debug("Poweroff {0} with ssh user user {1}".format(host, user))
remote_command("psshutdown -k -t 00 -v 00", user, host)
def poweroff_linux():
_log.debug("Poweroff {0}".format(config['linux']['host']))
remote_command("sudo chvt 1 ; sudo halt",
config['linux']['user'],
config['linux']['host'])
def reboot_linux():
_log.debug("Poweroff {0}".format(config['linux']['host']))
remote_command("sudo chvt 1 ; sudo reboot",
config['linux']['user'],
config['linux']['host'])
def poweroff_buero():
_log.debug("Poweroff {0}".format(config['buero']['host']))
remote_command("sudo halt",
config['buero']['user'],
config['buero']['host'])
time.sleep(30)
_log.debug("Poweroff Switch {0}".format(config['buero']['switch']))
poweroff_switch(config['buero']['switch'])
# myStrom
def poweroff_switch(switch):
if not args.mock:
urllib.request.urlopen(
"http://[{0}]/relay?state=0".format(switch))
# SSH to the Windows PC
def remote_command(cmd, remote_user, remote_host):
"""Ececute a command on the remote host."""
ssh = "su {0} -c 'ssh {1}@{2} \"{3}\" '".format(
config['webhook']['ssh_user'], remote_user, remote_host, cmd)
_log.debug(ssh)
if not args.mock:
try:
subprocess.run(ssh, shell=True, timeout=30)
except subprocess.TimeoutExpired:
pass
# WOL from https://github.com/remcohaszing/pywakeonlan/blob/master/wakeonlan.py
def create_magic_packet(macaddress):
"""
Create a magic packet.
A magic packet is a packet that can be used with the for wake on lan
protocol to wake up a computer. The packet is constructed from the
mac address given as a parameter.
Args:
macaddress (str): the mac address that should be parsed into a
magic packet.
"""
if len(macaddress) == 12:
pass
elif len(macaddress) == 17:
sep = macaddress[2]
macaddress = macaddress.replace(sep, '')
else:
raise ValueError('Incorrect MAC address format')
# Pad the synchronization stream
data = b'FFFFFFFFFFFF' + (macaddress * 16).encode()
send_data = b''
# Split up the hex values in pack
for i in range(0, len(data), 2):
send_data += struct.pack(b'B', int(data[i: i + 2], 16))
return send_data
def send_magic_packet(*macs, **kwargs):
"""
Wake up computers having any of the given mac addresses.
Wake on lan must be enabled on the host device.
Args:
macs (str): One or more macaddresses of machines to wake.
Keyword Args:
ip_address (str): the ip address of the host to send the magic packet
to (default "255.255.255.255")
port (int): the port of the host to send the magic packet to
(default 9)
"""
packets = []
ip = kwargs.pop('ip_address', BROADCAST_IP)
port = kwargs.pop('port', DEFAULT_PORT)
for k in kwargs:
raise TypeError('send_magic_packet() got an unexpected keyword '
'argument {!r}'.format(k))
for mac in macs:
packet = create_magic_packet(mac)
packets.append(packet)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# comment out for config['win']['host'] insead of 255.255.255.255:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.connect((ip, port))
_log.debug("connected to {0}".format(ip))
for packet in packets:
sock.send(packet)
_log.debug("sent packet {0}".format(binascii.hexlify(packet)))
sock.close()
# HTTPS server
class Handler(http.server.BaseHTTPRequestHandler):
def sendresponse(self, code):
self.send_response(code)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
"""Only respond with a HTML page to GET requests in verbose mode"""
if not args.verbose:
self.sendresponse(400)
return
self.sendresponse(200)
self.wfile.write("<html><head><title>IFTTT Webhook</title></head>".encode("utf-8"))
self.wfile.write("<body><p>IFTTT Webhook</p>".encode("utf-8"))
self.wfile.write("</body></html>".encode("utf-8"))
def do_POST(self):
if args.verbose:
pp.pprint(self.headers.as_string())
ctype, _ = cgi.parse_header(self.headers['content-type'])
if ctype != 'application/json':
self.sendresponse(400)
return
length = int(self.headers['content-length'])
raw = self.rfile.read(length).decode('utf-8')
msg = json.loads(raw)
if args.verbose:
pp.pprint(msg)
if msg.get('password') != config['webhook']['password']:
_log.error("Authentication failure")
time.sleep(10)
self.sendresponse(403)
return
# execute the command with the optional argument
cmd = msg.get('command')
arg = msg.get('argument')
if not command(cmd, arg):
_log.error("Unknown command {0} with argumend {1}".format(cmd, arg))
self.sendresponse(400)
return
self.sendresponse(200)
def start_http():
httpd = socketserver.TCPServer(("", int(config['webhook']['https_port'])), Handler)
httpd.socket = ssl.wrap_socket(httpd.socket,
keyfile=os.path.join(config['webhook']['ssl_dir'], config['webhook']['ssl_key']),
certfile=os.path.join(config['webhook']['ssl_dir'], config['webhook']['ssl_cert']),
server_side=True)
_log.info("Starting the webhook.py server")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
httpd.server_close()
if __name__ == '__main__':
if args.command:
command(args.command, args.argument)
else:
start_http()