Try to add SSL support.
Some checks failed
Check syntax / check (push) Has been cancelled
Some checks failed
Check syntax / check (push) Has been cancelled
This commit is contained in:
parent
f367f6b43f
commit
beaf57607e
1 changed files with 60 additions and 18 deletions
66
server.py
66
server.py
|
@ -2,7 +2,7 @@
|
||||||
__version__ = "0.0.1-pre-alpha"
|
__version__ = "0.0.1-pre-alpha"
|
||||||
print(f"Codename IRCat v{__version__}")
|
print(f"Codename IRCat v{__version__}")
|
||||||
print("Welcome! /ᐠ ˵> ⩊ <˵マ")
|
print("Welcome! /ᐠ ˵> ⩊ <˵マ")
|
||||||
import socket, time, threading, traceback, sys, subprocess, yaml, sqlite3, os, bcrypt
|
import socket, ssl, time, threading, traceback, sys, subprocess, yaml, sqlite3, os, bcrypt
|
||||||
from requests import get
|
from requests import get
|
||||||
if not len(sys.argv) == 2:
|
if not len(sys.argv) == 2:
|
||||||
print("IRCat requires the following arguments: config.yml")
|
print("IRCat requires the following arguments: config.yml")
|
||||||
|
@ -45,6 +45,18 @@ with open(sys.argv[1], 'r') as file:
|
||||||
except: print("Using 255 as a ping timeout.")
|
except: print("Using 255 as a ping timeout.")
|
||||||
try: restrict_ip = data["restrict-ip"]
|
try: restrict_ip = data["restrict-ip"]
|
||||||
except: print("Listening on all hosts possible.")
|
except: print("Listening on all hosts possible.")
|
||||||
|
try: ssl_option = data["ssl"] == "on"
|
||||||
|
except:
|
||||||
|
print("SSL will be off.")
|
||||||
|
ssl_option = False
|
||||||
|
if ssl_option:
|
||||||
|
try: ssl_cert = data["ssl_cert"]
|
||||||
|
except:
|
||||||
|
print("IRCat needs an SSL cert to use SSL!")
|
||||||
|
sys.exit(1)
|
||||||
|
try: ssl_pkey = data["ssl_pkey"]
|
||||||
|
except:
|
||||||
|
print("IRCat needs an SSL Private Key to use SSL!")
|
||||||
file.close()
|
file.close()
|
||||||
print("Successfully loaded config!")
|
print("Successfully loaded config!")
|
||||||
class IRCat_DATA_BROKER:
|
class IRCat_DATA_BROKER:
|
||||||
|
@ -68,11 +80,23 @@ class IRCat_DATA_BROKER:
|
||||||
return ["Nickname doesn't exist."]
|
return ["Nickname doesn't exist."]
|
||||||
|
|
||||||
config = IRCat_DATA_BROKER()
|
config = IRCat_DATA_BROKER()
|
||||||
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sockets = {}
|
||||||
tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
sockets_ssl = {}
|
||||||
server_address = (restrict_ip, 6667)
|
# Open the specified non-SSL sockets.
|
||||||
tcp_socket.bind(server_address)
|
for i in restrict_ip.split(" "):
|
||||||
tcp_socket.listen(1)
|
sockets[i] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sockets[i].setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sockets[i].bind((i,6667))
|
||||||
|
sockets[i].listen(1)
|
||||||
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||||
|
if ssl_option:
|
||||||
|
context.load_cert_chain(certfile=ssl_cert)
|
||||||
|
context.load_keyfile(keyfile=ssl_pkey)
|
||||||
|
for i in restrict_ip.split(" "):
|
||||||
|
sockets_ssl[i] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sockets_ssl[i].setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sockets_ssl[i].bind((i,6697))
|
||||||
|
sockets_ssl[i].listen(1)
|
||||||
opened=True
|
opened=True
|
||||||
reserved = ["nickserv", "chanserv", "gitserv"] # Reserved nicknames
|
reserved = ["nickserv", "chanserv", "gitserv"] # Reserved nicknames
|
||||||
nickname_list = {} # Stores nicknames and the respective sockets
|
nickname_list = {} # Stores nicknames and the respective sockets
|
||||||
|
@ -97,7 +121,7 @@ def pinger(nick, connection):
|
||||||
connection.shutdown(socket.SHUT_WR)
|
connection.shutdown(socket.SHUT_WR)
|
||||||
connection.close()
|
connection.close()
|
||||||
break
|
break
|
||||||
def session(connection, client, ip):
|
def session(connection, client, ip, ssl=False):
|
||||||
global property_list
|
global property_list
|
||||||
pending = "*" # The nickname of the client
|
pending = "*" # The nickname of the client
|
||||||
already_set = False # If the client gave the server a NICK packet
|
already_set = False # If the client gave the server a NICK packet
|
||||||
|
@ -566,14 +590,32 @@ def cleanup_manual():
|
||||||
if k != h and k in nickname_list:
|
if k != h and k in nickname_list:
|
||||||
nickname_list[k].sendall(f":{h}!~DISCONNECTED@DISCONNECTED PART {j} :IRCat Cleanup: Found missing connection!!\r\n")
|
nickname_list[k].sendall(f":{h}!~DISCONNECTED@DISCONNECTED PART {j} :IRCat Cleanup: Found missing connection!!\r\n")
|
||||||
|
|
||||||
try:
|
def tcp_session(sock):
|
||||||
|
try:
|
||||||
while opened:
|
while opened:
|
||||||
print("Waiting for connection...")
|
print("Waiting for connection...")
|
||||||
connection, client = tcp_socket.accept()
|
connection, client = sock.accept()
|
||||||
ip_to = restrict_ip
|
ip_to = restrict_ip
|
||||||
threading.Thread(target=session, daemon=True, args=[connection, client, ip_to]).start()
|
threading.Thread(target=session, daemon=True, args=[connection, client, ip_to]).start()
|
||||||
except:
|
except:
|
||||||
print("Shutting down...")
|
print("Shutting down...")
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
tcp_socket.shutdown(1)
|
sock.shutdown(1)
|
||||||
tcp_socket.close()
|
sock.close()
|
||||||
|
print("Something went wrong...")
|
||||||
|
print(traceback.format_exc())
|
||||||
|
def ssl_session(sock2):
|
||||||
|
try:
|
||||||
|
while opened:
|
||||||
|
print("Waiting for connection...")
|
||||||
|
with context.wrap_socket(sock, server_side=True) as sock:
|
||||||
|
connection, client = sock.accept()
|
||||||
|
ip_to = restrict_ip
|
||||||
|
threading.Thread(target=session, daemon=True, args=[connection, client, ip_to]).start()
|
||||||
|
except:
|
||||||
|
print("Shutting down...")
|
||||||
|
time.sleep(2)
|
||||||
|
sock.shutdown(1)
|
||||||
|
sock.close()
|
||||||
|
print("Something went wrong...")
|
||||||
|
print(traceback.format_exc())
|
Loading…
Reference in a new issue