コード例 #1
0
ファイル: server.py プロジェクト: 1715173329/Ragnarok
def main():
    global serverkey
    global ServerCipher
    global Clients
    global ClientsCiphers
    global ClientsModulus
    global CliensLock

    Clients ={}
    ClientsCiphers={}
    ClientsModulus={}

    ActiveThread = []
    CliensLock = threading.Lock()

    serverkey = RSA.generate(2048)
    ServerCipher = RSAcipher(serverkey)

    server_config = cfgloader.loadcfg()
    host = server_config["server_ip"]
    port = int(server_config["server_port"])
    servername = server_config["server_name"]
    serverpass = server_config["server_password"]

    s = socket(AF_INET, SOCK_STREAM)
    s.bind((host, port))
    s.listen(5)
    print("Listening on %d" % port)
    # Start server

    while True:
        try:
            conn, addr = s.accept()
            print("Connect from:", addr)

            # recv client public key
            modulus = conn.recv(256)
            components = (int.from_bytes(modulus, byteorder='big'), 65537)
            clientkey = RSA.construct(components)
            ClientCipher = RSAcipher(clientkey)

            # send server public key
            buffer = serverkey.n.to_bytes(256, byteorder='big')
            conn.send(buffer)

            # password auth
            if serverpass != "":
                buffer = conn.recv(4)
                header = int.from_bytes(buffer, byteorder='little')
                buffer = conn.recv(header)
                clientpass = ServerCipher.DcptString(buffer)
                if clientpass != serverpass:
                    conn.close()
                    continue

            # recv client name
            buffer = conn.recv(4)
            header = int.from_bytes(buffer, byteorder='little')
            buffer = conn.recv(header)
            name = ServerCipher.DcptString(buffer)
            print(name + "Connected")

            # check client name available
            if name in Clients.values():
                conn.close()
            else:

                CliensLock.acquire()
                Clients[conn] = name
                CliensLock.release()

                client_collection = Clients.values()
                print("|".join(client_collection))

                # send server name
                buffer = ClientCipher.EncptString(servername)
                header = len(buffer)
                conn.send(header.to_bytes(length=4, byteorder='little'))
                conn.send(buffer)

                # start client thread
                t = Thread(target=distribution, args=(conn,))
                t.start()

                CliensLock.acquire()
                ClientsCiphers[name] = ClientCipher
                ClientsModulus[name] = modulus
                CliensLock.release()

                ActiveThread.append(t)

            # recycle thread
            for t in ActiveThread:
                if t.is_alive() is False:
                    t.join()

        except:
            print("ACCEPT EXCEPTION")
            conn.close()
            break
コード例 #2
0
                else:
                    break

        finally:
            try:
                print("Disconnected with", self.a_UserID)
                UserID[self.a_UserID].close()
                del UserID[self.a_UserID]
                UserID[self.TargetID].close()
                del UserID[self.TargetID]
            except:
                pass


#Load port
CFG = cfgloader.loadcfg()
server_port = int((CFG["server_port"]))
server_ip = CFG["server_ip"]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Start server
s.bind((server_ip, server_port))
s.listen(10)
print("Listening on %d" % server_port)

while True:
    #ID auth
    while True:
        conn, addr = s.accept()
        try:
            a_UserID = conn.recv(1024).decode()
            print("Connect with", a_UserID)
コード例 #3
0
import asyncio
import json
import ssl
from cfgloader import loadcfg
import websockets

CONFIG = loadcfg()
PASSWORD = CONFIG["server_password"]
USERS = {}
SERVERNAME = "TestServer"
host = CONFIG["server_ip"]
port = CONFIG["server_port"]


def onlineUsers_event():
    str_list = list(USERS.values())
    return json.dumps({"msgType": "OnlineUsers", "Users": str_list})


def ServerInfo_event():
    return json.dumps({"ServerName": SERVERNAME})


async def notify_onlineUsers():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = onlineUsers_event()
        await Broadcast(message)


async def notify_serverinfo():
    if USERS:  # asyncio.wait doesn't accept an empty list