summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesusaves <cpntb1@ymail.com>2020-12-15 19:01:27 -0300
committerJesusaves <cpntb1@ymail.com>2020-12-15 19:01:27 -0300
commit1f05044181ceb6c497b88602d8876236374bd176 (patch)
tree517fa17299ccf3721c784c909acca8a4e9b0c929
parentfb6e726de1892f8d3f0237e01549db3a2950b5a2 (diff)
downloadclient-1f05044181ceb6c497b88602d8876236374bd176.tar.gz
client-1f05044181ceb6c497b88602d8876236374bd176.tar.bz2
client-1f05044181ceb6c497b88602d8876236374bd176.tar.xz
client-1f05044181ceb6c497b88602d8876236374bd176.zip
Websocket path building
-rw-r--r--game/defs.rpy116
-rw-r--r--game/script.rpy55
2 files changed, 62 insertions, 109 deletions
diff --git a/game/defs.rpy b/game/defs.rpy
index 6cb1b67..1c41462 100644
--- a/game/defs.rpy
+++ b/game/defs.rpy
@@ -220,7 +220,7 @@ init -3 python:
# File Managment Functions
def get_path(path):
- if renpy.android:
+ if True or renpy.android:
#print "Android detected"
path=path.replace("/", "_")
#return renpy.loader.get_path(path)
@@ -229,7 +229,7 @@ init -3 python:
return renpy.loader.get_path(path)
def get_path_if_exists(path):
- if renpy.android:
+ if True or renpy.android:
print "Android detected, not checking"
path=path.replace("/", "_")
#return renpy.loader.get_path(path)
@@ -258,6 +258,7 @@ init -3 python:
if (int(persistent.version) < ver):
# Check if the server have SSL support
+ """
try:
stdout("Downloading certificate from server")
x=requests.get("http://"+HOST+'/certificate.pem')
@@ -274,6 +275,7 @@ init -3 python:
except:
stdout("SSL: DISABLED")
persistent.ssl_enabled=False
+ """
# Download quests.json
f=open(get_path("quests.json"), "w")
@@ -344,6 +346,8 @@ init -1 python:
import ssl
print("Using system-wide SSL implementation...")
+ # wsock already imported
+
print("======================= %s %s %s" % (config.name, config.version, persistent.release_name))
print "[STDBY] Loading Basic functions......."
@@ -433,104 +437,22 @@ init -1 python:
return Player["code"]
def send_packet_now(packet, args="", legacy=False):
- global tr_load, tr_val
- try:
- sock80 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- if not renpy.android:
- sock80.settimeout(10)
- else:
- stdout("RAW Socket Ready")
-
- # TODO: Request server if it is running with SSL or not
- # If SSL is enabled (server configuration)
- if persistent.ssl_enabled:
- if not SSL_IS_BROKEN:
- # Create the contest
- context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
-
- # Protect the most possible, if appliable.
- context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1
-
- # FIXME We should check it
- if persistent.ssl_enabled != "IGNORE":
- context.check_hostname = True
- context.verify_mode = ssl.CERT_OPTIONAL
- else:
- stdout("[SECURITY] SSL VERIFICATION DISABLED.")
- context.check_hostname = False
- context.verify_mode = ssl.CERT_NONE
- #context.load_default_certs() # <- A .crt file
- context.load_verify_locations(get_path("cert/certificate.pem"))
- stdout("SSL Context Ready")
-
- sock = context.wrap_socket(sock80, server_hostname=HOST)
- else:
- CIPHERS_OVERRIDE = ":".join(
- [
- "ECDHE+AESGCM",
- #"ECDHE+CHACHA20",
- #"DHE+AESGCM",
- #"DHE+CHACHA20",
- "ECDH+AESGCM",
- #"DH+AESGCM",
- "ECDH+AES",
- #"DH+AES",
- "RSA+AESGCM",
- "RSA+AES",
- #"!aNULL",
- #"!eNULL",
- #"!MD5",
- #"!DSS",
- ]
- )
- if persistent.ssl_enabled != "IGNORE":
- sock = ssl.wrap_socket(sock80, ca_certs=get_path("cert/certificate.pem"), ssl_version=2, cert_reqs=ssl.CERT_OPTIONAL, ciphers=CIPHERS_OVERRIDE)
- else:
- stdout("[SECURITY] SSL VERIFICATION DISABLED.")
- sock = ssl.wrap_socket(sock80, ca_certs=get_path("cert/certificate.pem"), ssl_version=2, cert_reqs=ssl.CERT_NONE, ciphers=CIPHERS_OVERRIDE)
- stdout("Android SSL Wrapper Ready")
-
- else:
- sock=sock80
-
- if not renpy.android:
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- else:
- stdout("Socket Ready")
+ global tr_load, tr_val, tr_busy
+ global ws
- sock.connect((HOST, PORT))
-
- if renpy.android:
- stdout("Socket Ready")
- stdout("Connected: (%s, %d), Socket %s" % (HOST, PORT, sock.getsockname()))
- renpy.pause(1.0)
-
- #m=get_token() + ";" + packet
- m=get_token() + ";" + packet + ";" + args
-
- stdout("Sending: %s" % m)
- sock.sendall(m)
-
- # Receive data from the server
- received = sock.recv(2048)
- stdout("received: %s" % received)
+ # TODO: if tr_busy already true, wait until it is made false
+ tr_busy=True
+ tr_load=False
+ tr_val=""
- # Close the socket
- sock.shutdown(socket.SHUT_RDWR)
- sock.close()
+ stdout("Sending: %s" % packet)
+ try:
+ ws.send(get_token() + ";" + packet + ";" + args)
except:
- received=FAILUREMSG
-
- if persistent.fatality is not None:
- raise
-
- # Legacy support (for ping)
- if legacy:
- return received
-
- tr_load=True
- tr_val=str(received)
- return
+ return False
+ # TODO: Wait for onmsg
+ # TODO: ping packets
+ return True
def send_packet(packet, args=""):
global tr_load, tr_val
diff --git a/game/script.rpy b/game/script.rpy
index 1db5d3f..07bc415 100644
--- a/game/script.rpy
+++ b/game/script.rpy
@@ -22,9 +22,18 @@ No warranties.{/b}{fast}"
return
init python:
+ tr_busy=False
+
# Same as ondata?
def onmsg(self, message):
- print(message)
+ global tr_load, tr_val, tr_busy
+ stdout(message)
+
+ # Inform whatever is waiting for us that a reply was obtained
+ tr_load=True
+ tr_val=str(received)
+ # We do not clean tr_busy here
+ return
def ondata(self, msg, dtype, cont):
print("data received")
@@ -38,7 +47,7 @@ init python:
The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
The 4th argument is continue flag. if 0, the data continue
"""
- renpy.notify(msg)
+ #renpy.notify(msg)
return
def onopen(self):
@@ -58,19 +67,35 @@ label start:
"We shall now begin testing a websocket"
python:
- # server, sock, address
- # on_open=None, on_message=None, on_error=None,
- # on_close=None, on_ping=None, on_pong=None,
- # on_data=None):
- ws = wsock.WebSocketApp("wss://localhost:61000",
- on_data=ondata, on_error=onerror, on_open=onopen,
- on_message=onmsg)
+ ws=persistent.ws
+ if (ws is not None):
+ stdout("Socket exists, trying to reuse connection...")
+ try:
+ boom = send_packet_now("ping")
+ if not boom:
+ del boom
+ raise
+ del boom
+ except:
+ ws = None
+
+ if ws is None:
+ stdout("Opening new socket...")
+ # server, sock, address
+ # on_open=None, on_message=None, on_error=None,
+ # on_close=None, on_ping=None, on_pong=None,
+ # on_data=None):
+ ws = wsock.WebSocketApp("wss://localhost:61000",
+ on_data=ondata, on_error=onerror, on_open=onopen,
+ on_message=onmsg)
print repr(ws)
ws.on_open = onopen
renpy.invoke_in_thread(ws.run_forever, sslopt={"cert_reqs": ssl.CERT_NONE})
#ws.connect("wss://localhost:61000")
"Connection established!"
+ $ ws.send("Hello world")
+ "Message sent!"
return
# Run updater
python:
@@ -175,7 +200,7 @@ label login:
# Successful login? Update client data and begin routines
$ persistent.password=password
$ dlcode-=5000
- $ ping_routine()
+ #$ ping_routine()
$ renpy.invoke_in_thread(irc_loop)
# We're now logged in, load misc data (TODO: What if ERR_ is returned?)
@@ -347,6 +372,12 @@ label quit:
except:
pass
+ # If needed, close the socket
+ try:
+ ws.close()
+ except:
+ pass
+
# Delete variables
try:
del allunitsbase
@@ -363,8 +394,8 @@ label quit:
irc_kill()
# Data cleaned up
- # TODO: There might still be open sockets we need to close
- # But as we've sent LOGOUT protocol, these aren't important
+ # Destroy the socket
+ $ persistent.ws = None
$ print("Sockets closed and data flushed!")
return