Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Update PostgreSQLClient.gd
Browse files Browse the repository at this point in the history
  • Loading branch information
Marzin-bot authored Nov 9, 2021
1 parent 0bc0651 commit 01f44ba
Showing 1 changed file with 49 additions and 35 deletions.
84 changes: 49 additions & 35 deletions PostgreSQLClient.gd
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ var process_backend_secret_key: int
var status_ssl = 0

var global_url = ""

var startup_message: PoolByteArray
var next_etape := 0
var con_ssl: bool

## Allows you to connect to a Postgresql backend at the specified url.
func connect_to_host(url: String, ssl := false, connect_timeout := 30) -> int:
global_url = url
con_ssl = ssl
var error := 1

# If the fontend was already connected to the backend, we disconnect it before reconnecting.
Expand All @@ -103,7 +106,7 @@ func connect_to_host(url: String, ssl := false, connect_timeout := 30) -> int:
### StartupMessage ###

# "postgres" is the database and user by default.
var startup_message = request("", "user".to_ascii() + PoolByteArray([0]) + result.strings[1].to_utf8() + PoolByteArray([0]) + "database".to_ascii() + PoolByteArray([0]) + result.strings[5].to_utf8() + PoolByteArray([0, 0]))
startup_message = request("", "user".to_ascii() + PoolByteArray([0]) + result.strings[1].to_utf8() + PoolByteArray([0]) + "database".to_ascii() + PoolByteArray([0]) + result.strings[5].to_utf8() + PoolByteArray([0, 0]))

password_global = result.strings[2]
user_global = result.strings[1]
Expand All @@ -120,14 +123,12 @@ func connect_to_host(url: String, ssl := false, connect_timeout := 30) -> int:
if not client.is_connected_to_host() and client.get_status() == StreamPeerTCP.STATUS_NONE:
error = client.connect_to_host(result.strings[3], port)

#if (error == OK)
# Get the fist message of server.
while not (error == OK and client.is_connected_to_host() and client.get_status() == StreamPeerTCP.STATUS_CONNECTED):
if ssl:
### SSLRequest ###

set_ssl_connection()
else:
peer.put_data(startup_message)
if error == OK:
next_etape = 1
else:
print("[PostgreSQLClient:%d] Invalid host Postgres." % [get_instance_id()])
else:
status = Status.STATUS_ERROR

Expand Down Expand Up @@ -172,6 +173,7 @@ func close(clean_closure := true) -> void:
error_object = {}

status = Status.STATUS_DISCONNECTED
next_etape = false
status_ssl = 0

emit_signal("connection_closed", clean_closure)
Expand Down Expand Up @@ -298,32 +300,44 @@ func poll() -> void:
stream_peer_ssl.poll()

if client.is_connected_to_host():
if client.get_status() == StreamPeerTCP.STATUS_CONNECTED and status_ssl == 1:
var response = peer.get_data(peer.get_available_bytes())
if response[0] == OK:
if not response[1].empty():
match char(response[1][0]):
'S':
#var crypto = Crypto.new()
#var ssl_key = crypto.generate_rsa(4096)
#var ssl_cert = crypto.generate_self_signed_certificate(ssl_key)
stream_peer_ssl.connect_to_stream(peer)
# stream_peer_ssl.blocking_handshake = false
status_ssl = 2
'N':
status = Status.STATUS_ERROR

push_error("[PostgreSQLClient:%d] The connection attempt failed. The backend does not want to establish a secure SSL/TLS connection." % [get_instance_id()])

close(false)
var value:
status = Status.STATUS_ERROR

push_error("[PostgreSQLClient:%d] The backend sent an unknown response to the request to establish a secure connection. Response is not recognized: '%c'." % [get_instance_id(), value])

close(false)
else:
push_warning("[PostgreSQLClient:%d] The backend did not send any data or there must have been a problem while the backend sent a response to the request." % [get_instance_id()])
if client.get_status() == StreamPeerTCP.STATUS_CONNECTED:
if next_etape:
if con_ssl:
### SSLRequest ###

set_ssl_connection()
else:
peer.put_data(startup_message)
startup_message = PoolByteArray()

next_etape = false

if status_ssl == 1:
var response = peer.get_data(peer.get_available_bytes())
if response[0] == OK:
if not response[1].empty():
match char(response[1][0]):
'S':
#var crypto = Crypto.new()
#var ssl_key = crypto.generate_rsa(4096)
#var ssl_cert = crypto.generate_self_signed_certificate(ssl_key)
stream_peer_ssl.connect_to_stream(peer)
# stream_peer_ssl.blocking_handshake = false
status_ssl = 2
'N':
status = Status.STATUS_ERROR

push_error("[PostgreSQLClient:%d] The connection attempt failed. The backend does not want to establish a secure SSL/TLS connection." % [get_instance_id()])

close(false)
var value:
status = Status.STATUS_ERROR

push_error("[PostgreSQLClient:%d] The backend sent an unknown response to the request to establish a secure connection. Response is not recognized: '%c'." % [get_instance_id(), value])

close(false)
else:
push_warning("[PostgreSQLClient:%d] The backend did not send any data or there must have been a problem while the backend sent a response to the request." % [get_instance_id()])


if status_ssl == 2 and stream_peer_ssl.get_status() == stream_peer_ssl.STATUS_CONNECTED:
Expand Down

0 comments on commit 01f44ba

Please sign in to comment.