87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
import sqlite3
|
|
import bcrypt
|
|
|
|
def initialize_db():
|
|
conn = sqlite3.connect('servers.db')
|
|
cursor = conn.cursor()
|
|
|
|
# Create table for servers
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS servers (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
ip_address TEXT NOT NULL,
|
|
name TEXT NOT NULL
|
|
)
|
|
''')
|
|
|
|
# Create table for users
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT NOT NULL UNIQUE,
|
|
password TEXT NOT NULL
|
|
)
|
|
''')
|
|
|
|
# Create table for actions
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS actions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL UNIQUE,
|
|
endpoint TEXT NOT NULL
|
|
)
|
|
''')
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
def add_user(username, password):
|
|
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
|
conn = sqlite3.connect('servers.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
def check_user(username, password):
|
|
conn = sqlite3.connect('servers.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT password FROM users WHERE username = ?", (username,))
|
|
user = cursor.fetchone()
|
|
|
|
if user and bcrypt.checkpw(password.encode('utf-8'), user[0].encode('utf-8')):
|
|
return True
|
|
return False
|
|
|
|
def add_server(ip_address, name):
|
|
conn = sqlite3.connect('servers.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute("INSERT INTO servers (ip_address, name) VALUES (?, ?)", (ip_address, name))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
def get_servers():
|
|
conn = sqlite3.connect('servers.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT * FROM servers")
|
|
data = cursor.fetchall()
|
|
conn.close()
|
|
return data
|
|
|
|
def add_action(name, endpoint):
|
|
conn = sqlite3.connect('servers.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute("INSERT INTO actions (name, endpoint) VALUES (?, ?)", (name, endpoint))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def get_actions():
|
|
conn = sqlite3.connect('servers.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT * FROM actions")
|
|
data = cursor.fetchall()
|
|
conn.close()
|
|
return data
|
|
|