-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
107 lines (87 loc) · 3.07 KB
/
server.py
File metadata and controls
107 lines (87 loc) · 3.07 KB
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
import socket
import threading
import os
from datetime import datetime
istemciler = []
kullanici_adlari = []
istemci_adresleri = {}
yasakli_ipler = set()
def yayinla(mesaj, gonderen=None):
zaman = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if gonderen:
log_mesaj = f"{gonderen} | {mesaj.decode('utf-8')} | {zaman}"
with open("log.txt", "a", encoding="utf-8") as f:
f.write(log_mesaj + "\n")
for istemci in istemciler:
try:
istemci.send(mesaj)
except:
pass
def istemciyi_yonet(istemci):
indeks = istemciler.index(istemci)
kullanici_adi = kullanici_adlari[indeks]
ip = istemci_adresleri[istemci]
while True:
try:
mesaj = istemci.recv(1024)
if not mesaj:
break
yayinla(mesaj, gonderen=kullanici_adi)
except:
break
istemciler.remove(istemci)
kullanici_adlari.remove(kullanici_adi)
del istemci_adresleri[istemci]
istemci.close()
yayinla(f"{kullanici_adi} sohbetten ayrıldı.".encode('utf-8'))
def komutlari_dinle():
while True:
komut = input()
if komut.startswith("/block "):
ip = komut.split("/block ")[1].strip()
yasakli_ipler.add(ip)
print(f"IP adresi engellendi: {ip}")
elif komut.startswith("/unblock "):
ip = komut.split("/unblock ")[1].strip()
if ip in yasakli_ipler:
yasakli_ipler.remove(ip)
print(f"IP adresi engeli kaldırıldı: {ip}")
else:
print(f"Bu IP engellenmemiş: {ip}")
elif komut.strip() == "/help":
print("/block USER_IP")
print("/unblock USER_IP")
else:
print("Bilinmeyen komut. Yardım için /help yaz.")
def dinle():
os.system("cls" if os.name == "nt" else "clear")
sunucu = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sunucu.bind(("0.0.0.0", 55555))
sunucu.listen()
print("Sunucu çalışıyor...")
threading.Thread(target=komutlari_dinle, daemon=True).start()
while True:
istemci, adres = sunucu.accept()
ip = adres[0]
if ip in yasakli_ipler:
print(f"Engellenmiş IP'den bağlantı engellendi: {ip}")
istemci.close()
continue
istemci.send("KULLANICI_ADI".encode('utf-8'))
try:
kullanici_adi = istemci.recv(1024).decode('utf-8')
except:
istemci.close()
continue
zaman = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"{kullanici_adi} | IP: {ip} | {zaman}")
# Bağlantı logunu dosyaya yaz
with open("connect_log.txt", "a", encoding="utf-8") as f:
f.write(f"{kullanici_adi} | {ip} | {zaman}\n")
kullanici_adlari.append(kullanici_adi)
istemciler.append(istemci)
istemci_adresleri[istemci] = ip
yayinla(f"{kullanici_adi} sohbete katıldı!".encode('utf-8'))
is_parcasi = threading.Thread(target=istemciyi_yonet, args=(istemci,))
is_parcasi.start()
dinle()