Exemple #1
0
def send_recv(ip: str, port: int, password: str, data: dict, packet_id: int,
              context: ssl.SSLContext = None, timeout: int = None):
    # This is a simple celery async task send.
    # However, it authenticates before sending.
    def create_socket(ip: str, port: int, context: ssl.SSLContext = None, timeout: int = 10):
        """
        Creates a new SSL-wrapped socket.
        :param ip: The IP to connect to.
        :param port: The port to connect to.
        :param context: The SSL context to use, or None for a default one to be created.
        :param timeout: The timeout for recv().
        :return: A new SSLSocket.
        """
        return gutsama.create_socket(ip, port, context, False, timeout)

    server = create_socket(ip, port, context, timeout)

    auth = gutsama.pack_sendrecv(server, {"password": password}, 0)
    if not auth:
        raise Exception("Failed to authenticate with Gutsama server.")
    else:
        data = gutsama.pack_sendrecv(server, data, packet_id)
        server.close()
        return data
Exemple #2
0
# --> Init app
logger.info("All --> Loading application...")
init.core_init(app)

# --> Load language
logger.info("All --> Loading language files...")
language.load_all()

# --> Check status of Gutsama
logger.info("All --> Synchronising with Gutsama...")
for server in app.config.get("SERVERS", []):
    logger.info("Pinging server {}".format(server["name"]))
    try:
        sock = create_socket(server["ip"], server["port"],
                             verify_hostname=app.config.get("SERVER_LOGIN_ON_CLIENT_VERIFY", True))
    except ConnectionRefusedError:
        logger.error("Failed to connect to Gutsama server --> Connection refused")
        continue
    # -> Authenticate
    auth = pack_sendrecv(sock, {"password": server["pw"]}, 0)
    if auth is None:
        # Authentication failed
        logger.error("Authentication failed for server {}".format(server["name"]))
        exit(22)
    ping = pack_sendrecv(sock, {"cmd": "ping"}, 1)
    if ping["data"].get("response", None) != "pong":
        logger.error("Failed to connect to Gutsama server {}".format(server))
    else:
        logger.info("ping --> {}".format(ping["data"]))
    sock.close()