-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
126 lines (94 loc) · 3.36 KB
/
server.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
#server side / host
import configparser
import base64
import sys, socket, select
from Crypto.Cipher import AES
import hashlib
import os
import signal
os.system("clear")
print("""
____ ____ ____ __ _ _ __ ____ ___
( \( _ \( __) / _\ ( \/ ) ___( )( _ \ / __)
) D ( ) / ) _) / \/ \/ \(___))( ) /( (__
(____/(__\_)(____)\_/\_/\_)(_/ (__)(__\_) \___)
Secure IRC by DreamSec
dreambooter.xyz
""")
def sigint_handler(signum, frame):
print('\n[error] user interupt')
print("[info] shutting down DREAM-IRC \n\n")
sys.exit()
signal.signal(signal.SIGINT, sigint_handler)
def hasher(key):
hash_object = hashlib.sha512(key)
hexd = hash_object.hexdigest()
hash_object = hashlib.md5(hexd)
hex_dig = hash_object.hexdigest()
return hex_dig
def encrypt(secret,data):
BLOCK_SIZE = 32
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
cipher = AES.new(secret)
encoded = EncodeAES(cipher, data)
return encoded
def decrypt(secret,data):
BLOCK_SIZE = 32
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
cipher = AES.new(secret)
decoded = DecodeAES(cipher, data)
return decoded
config = configparser.RawConfigParser()
config.read(r'dream.conf')
HOST = config.get('config', 'HOST')
PORT = int(config.get('config', 'PORT'))
PASSWORD = config.get('config', 'PASSWORD')
VIEW = str(config.get('config', 'VIEW'))
key = hasher(PASSWORD)
SOCKET_LIST = []
RECV_BUFFER = 4096
def chat_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
SOCKET_LIST.append(server_socket)
print("[Server] Started on port " + str(PORT))
while 1:
ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0)
for sock in ready_to_read:
if sock == server_socket:
sockfd, addr = server_socket.accept()
SOCKET_LIST.append(sockfd)
print("[Server] User [(%s, %s)] connected" % addr)
else:
try:
data = sock.recv(RECV_BUFFER)
data = decrypt(key,data)
if data:
broadcast(server_socket, sock,encrypt(key,"\r" + data))
if VIEW == '1':
print(data)
else:
if sock in SOCKET_LIST:
SOCKET_LIST.remove(sock)
broadcast(server_socket, sock,encrypt(key,"[Server] [(%s, %s)] Has left the server.\n" % addr))
except:
broadcast(server_socket, sock, "[Server] User [(%s, %s)] is offline\n" % addr)
continue
server_socket.close()
def broadcast (server_socket, sock, message):
for socket in SOCKET_LIST:
if socket != server_socket and socket != sock :
try :
socket.send(message)
except :
socket.close()
if socket in SOCKET_LIST:
SOCKET_LIST.remove(socket)
if __name__ == "__main__":
sys.exit(chat_server())