forked from hasameli/foghorn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foghornc.py
executable file
·64 lines (51 loc) · 2.23 KB
/
foghornc.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
#!/usr/bin/env python
"""Foghornc --- Foghorn client for RPC"""
import httplib
import socket
import xmlrpclib
import sys
class UnixStreamHTTPConnection(httplib.HTTPConnection):
"""Connect socket"""
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(self.host)
class UnixStreamTransport(xmlrpclib.Transport, object):
"""Connect HTTP to Unix Socket"""
def __init__(self, socket_path):
self.socket_path = socket_path
super(UnixStreamTransport, self).__init__()
def make_connection(self, host):
return UnixStreamHTTPConnection(self.socket_path)
if __name__ == '__main__':
if len(sys.argv) == 2:
sys.argv.append(None)
if len(sys.argv) == 3:
sys.argv.append(None)
if len(sys.argv) != 4:
print "foghornc.py [command] <arg> <arg2>"
exit(1)
(_, COMMAND, ARG, ARG2) = sys.argv
SERVER = xmlrpclib.Server('http://arg_unused',
transport=UnixStreamTransport("foghornd.sock"),
allow_none=1)
FUNCTIONS = {
"query_host": lambda: SERVER.query_host(ARG),
"add_to_whitelist": lambda: SERVER.add_to_whitelist(ARG, ARG2),
"add_to_blacklist": lambda: SERVER.add_to_blacklist(ARG, ARG2),
"add_to_greylist": lambda: SERVER.add_to_greylist(ARG, ARG2),
"delete_from_whitelist": lambda: SERVER.delete_from_whitelist(ARG),
"delete_from_blacklist": lambda: SERVER.delete_from_blacklist(ARG),
"delete_from_greylist": lambda: SERVER.delete_from_greylist(ARG),
"delete_tag_from_whitelist": lambda: SERVER.delete_tag_from_whitelist(ARG),
"delete_tag_from_blacklist": lambda: SERVER.delete_tag_from_blacklist(ARG),
"delete_tag_from_greylist": lambda: SERVER.delete_tag_from_greylist(ARG),
"query_all_lists": lambda: SERVER.query_all_lists(),
"query_blacklist": lambda: SERVER.query_blacklist(),
"query_whitelist": lambda: SERVER.query_whitelist(),
"query_greylist": lambda: SERVER.query_greylist(),
}
if COMMAND in FUNCTIONS.keys():
RESULT = FUNCTIONS[COMMAND]()
print "Result: %s" % RESULT
else:
print "Unkown command: %s" % COMMAND