-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
139 lines (113 loc) · 3.93 KB
/
utils.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
import datetime
import re
from cryptography.fernet import Fernet
from peewee import MySQLDatabase
from models import Server, IpAddress, Log
import os
from dotenv import load_dotenv
from post_install import run_command
load_dotenv()
def encrypt(key, plain_str):
fernet = Fernet(key)
return fernet.encrypt(plain_str.encode())
def decrypt(key, encrypted_str):
fernet = Fernet(key)
return fernet.decrypt(encrypted_str).decode()
def insert_server_detail(host, name, port, protocol):
Server.insert(
host=host,
name=name,
port=port.strip(),
protocol=protocol,
created_at=datetime.datetime.utcnow()
).on_conflict(
"replace"
).execute()
print("%s server registered" % name)
def create_tables(db, models):
db.connect()
db.create_tables(models)
db.close()
def init_db(db):
models = [Server, IpAddress, Log]
print("Creating tables from models...")
create_tables(db, models)
def connect_remote_db():
host = os.getenv("REMOTE_DB_HOST")
user = os.getenv("REMOTE_DB_USER")
port = os.getenv("REMOTE_DB_PORT")
passwd = os.getenv("REMOTE_DB_PASS")
db = os.getenv("REMOTE_DB_NAME")
mysql = MySQLDatabase(
db,
host=host,
port=int(port),
user=user,
passwd=passwd
)
return mysql
def get_remote_db_data():
table = os.getenv("REMOTE_DB_TABLE")
select_field = os.getenv("REMOTE_DB_COLUMN")
mysql = connect_remote_db()
mysql.connect()
# TODO Set logic to fetch rows using OFFSET and LIMIT
sql = "SELECT %s FROM %s" % (select_field, table)
cursor = mysql.execute_sql(sql)
res = cursor.fetchall()
return res
def sync_remote_and_local_db():
rule_name = os.getenv("IPSET_RULE_NAME")
remote_ips = get_remote_db_data()
# sync remote to local
for remote_ip in remote_ips:
query = IpAddress.select().where(IpAddress.ip_address == remote_ip[0]).where(IpAddress.is_active == 1)
if query.exists() is False:
try:
cmd = "sudo ipset add {rule_name} {ip}".format(rule_name=rule_name, ip=remote_ip[0])
run_command(cmd)
IpAddress.insert(
ip_address=remote_ip[0],
is_active=True,
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow()
).on_conflict(
action='replace',
).execute()
except BaseException as e:
Log.insert(text="IP: %s %s" % (remote_ip[0], str(e)), created_at=datetime.datetime.utcnow()).execute()
# sync local to remote
local_ips = IpAddress.select().execute()
for local_ip in local_ips:
is_exists = False
try:
for remote_ip in remote_ips:
if local_ip.ip_address == remote_ip[0]:
is_exists = True
if is_exists is False:
cmd = "sudo ipset del {rule_name} {ip}".format(
rule_name=rule_name,
ip=local_ip.ip_address
)
run_command(cmd)
IpAddress.update({
IpAddress.is_active: 0,
IpAddress.updated_at: datetime.datetime.utcnow()
}).where(
IpAddress.ip_address == local_ip.ip_address
).execute()
except BaseException as e:
Log.insert(
text="IP: %s %s" % (local_ip, str(e)),
created_at=datetime.datetime.utcnow()
).execute()
def reset_data():
IpAddress.delete().execute()
run_command("sudo ipset -F")
def is_valid_hostname(hostname):
if len(hostname) > 255 or len(hostname) == 0:
return False
if hostname[-1] == ".":
hostname = hostname[:-1]
allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))