Added auth tokens

This commit is contained in:
Eduard Urbach 2024-01-22 23:41:48 +01:00
parent 1f6c128893
commit 532a1faa21
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 60 additions and 33 deletions

View File

@ -2,14 +2,24 @@ extends PacketHandler
@export var playerScene: PackedScene
var logged_in := false
var auth_token: String
func _ready():
%Client.set_handler(Packet.LOGIN, self)
send_login()
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
if data[0] != 0:
print("[Client] Login failed.")
return
print("[Client] Login succeeded.")
auth_token = data.slice(1).get_string_from_ascii()
print("[Client] Auth token: %s" % auth_token)
Global.player = spawn_player()
func send_login():
if logged_in:
if is_logged_in():
return
var buffer := StreamPeerBuffer.new()
@ -18,14 +28,8 @@ func send_login():
%Client.socket.put_packet(buffer.data_array)
print("[Client] Connecting...")
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
if data[0] != 0:
print("[Client] Login failed.")
return
print("[Client] Login succeeded.")
logged_in = true
Global.player = spawn_player()
func is_logged_in() -> bool:
return auth_token != ""
func spawn_player() -> Player:
var player = playerScene.instantiate()

10
network/server/Client.gd Normal file
View File

@ -0,0 +1,10 @@
class_name Client
var peer: PacketPeerUDP
var address: String
var last_packet: int
var auth_token: String
var account: Account
func is_logged_in() -> bool:
return auth_token != ""

View File

@ -1,5 +0,0 @@
class_name Connection
var peer: PacketPeerUDP
var address: String
var last_packet: int

View File

@ -14,6 +14,11 @@ func _ready():
%Server.set_handler(Packet.LOGIN, self)
func handle_packet(data: PackedByteArray, peer: PacketPeer):
var client = %Server.peer_to_client(peer)
if client.is_logged_in():
return
var data_string = data.get_string_from_utf8()
var login_request = JSON.parse_string(data_string)
@ -32,9 +37,12 @@ func handle_packet(data: PackedByteArray, peer: PacketPeer):
fail_login(peer)
return
client.auth_token = generate_auth_token()
var buffer := StreamPeerBuffer.new()
buffer.put_8(Packet.LOGIN)
buffer.put_8(SUCCESS)
buffer.put_data(client.auth_token.to_ascii_buffer())
peer.put_packet(buffer.data_array)
func fail_login(peer: PacketPeer):
@ -42,3 +50,6 @@ func fail_login(peer: PacketPeer):
buffer.put_8(Packet.LOGIN)
buffer.put_8(FAIL)
peer.put_packet(buffer.data_array)
func generate_auth_token() -> String:
return "secret"

View File

@ -27,20 +27,11 @@ func _process(_delta):
# Accept new connections
while server.is_connection_available():
var peer: PacketPeerUDP = server.take_connection()
var address := "%s:%d" % [peer.get_packet_ip(), peer.get_packet_port()]
if !clients.has(address):
var connection = Connection.new()
connection.peer = peer
connection.address = address
clients[address] = connection
peer_connected(connection)
clients[address].last_packet = now
peer_to_client(peer).last_packet = now
# Process packets from clients
for key in clients:
var client = clients[key]
for address in clients:
var client = clients[address]
var peer = client.peer
while peer.get_available_packet_count() > 0:
@ -62,8 +53,24 @@ func _process(_delta):
packet_count = 0
last_statistics = Time.get_ticks_msec()
func peer_connected(c: Connection):
func peer_address(peer: PacketPeerUDP):
return "%s:%d" % [peer.get_packet_ip(), peer.get_packet_port()]
func peer_to_client(peer: PacketPeerUDP) -> Client:
var address = peer_address(peer)
if !clients.has(address):
var client = Client.new()
client.peer = peer
client.address = address
clients[address] = client
peer_connected(client)
return client
return clients[address]
func peer_connected(c: Client):
print("[Server] Connected: ", c.address)
func peer_disconnected(c: Connection):
func peer_disconnected(c: Client):
print("[Server] Disconnected: ", c.address)

View File

@ -75,15 +75,15 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 7.00829, 1.28057, -1.95247)
[node name="Slime3" parent="Enemies" instance=ExtResource("16_fuixr")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.53558, 1.28057, -0.306177)
[node name="World" type="Node3D" parent="."]
[node name="Objects" type="Node3D" parent="."]
[node name="Tree" parent="World" instance=ExtResource("15_csh38")]
[node name="Tree" parent="Objects" instance=ExtResource("15_csh38")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5.11323, 0, -4.64839)
[node name="Tree2" parent="World" instance=ExtResource("15_csh38")]
[node name="Tree2" parent="Objects" instance=ExtResource("15_csh38")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5.11323, 0, 5.35161)
[node name="Arena" parent="World" instance=ExtResource("17_q45cd")]
[node name="Arena" parent="Objects" instance=ExtResource("17_q45cd")]
[node name="UI" type="Control" parent="."]
layout_mode = 3