forked from R-a-dio/icecast-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icetool.py
executable file
·126 lines (111 loc) · 4.35 KB
/
icetool.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
#!/usr/bin/env python
import sys
import getpass
from database import SQLManager, User, Mount
import bcrypt
def cmd_users(action, username=None, password=None, privileges=-1):
with SQLManager() as session:
if action == "list":
print("Current Users:")
for row in session.query(User):
print("[{}] {}".format(row.privileges, row.user))
elif action == "del":
if not username or len(username) < 3:
print("Please provide a valid username")
sys.exit(0)
yes = raw_input(
"Are you sure you want to delete user {}? (Yes/no): ".format(
username
))
if yes.lower().strip() == "yes":
session.query(User).filter(User.user == username).delete()
session.commit()
elif action in ("add", "superadd"):
while not username or len(username) < 3:
username = raw_input(
"Please provide a username (3 chars min): ")
while not password or len(password) < 5:
password = getpass.getpass(
"please provide a password (5 chars min): ")
if action == "add":
while privileges < 1:
privileges = raw_input(
"Please provide a level of privileges (1 or higher): "
)
elif action == "superadd":
privileges = 0
for row in session.query(User).filter(User.user == username):
print(
"A username with that name exists already. "
"Please provide another username or use the "
"command 'users del' to delete and readd."
)
salt = bcrypt.gensalt()
cryptpw = bcrypt.hashpw(password, salt)
session.add(User(
user=username,
password=cryptpw,
privileges=privileges))
session.commit()
def cmd_routes(action, *args):
proto = ['http', 'xa', 'icy']
with SQLManager() as session:
if action == "list":
source = args and args[0] or None
if source:
routes = session.query(Mount).filter(Mount.source == source).all()
else:
routes = session.query(Mount).order_by(Mount.source).all()
routestr = " {source: <17} | {destination: <39} | {format: <17} "
print(routestr.format(
source="proxy mountpoint",
destination="destination server",
format="format"
))
print("-" * 80)
for route in routes:
print(routestr.format(
source=route.source,
destination="".join(
[proto[int(route.protocol)], '://',
route.host, ":",
str(route.port),
route.mount]),
format=route.format
))
elif action == "add":
if not args:
print(
"no arguments provided.\n\n"
"Syntax: routes add "
"<proxy mount> <server> <port> "
"<mount> <protocol> <format>")
sys.exit(1)
source, host, port, mount, protocol, format = args
user = None
password = None
while not user or not len(user.strip()):
user = raw_input("enter the server's username: ")
while not password or not len(password.strip()):
password = getpass.getpass("enter the password for %s: " % user)
route = Mount(
source=source,
host=host,
port=int(port),
password=password,
format=format,
protocol=str(proto.index(protocol)),
mount=mount,
user=user)
session.add(route)
session.commit()
elif action == "del":
sys.exit(1) # TODO
if __name__ == "__main__":
args = sys.argv[1:]
if args[0] == "users":
cmd_users(*args[1:])
elif args[0] == "routes":
cmd_routes(*args[1:])
elif args[0] == "status":
print("not implemented")