Added auth tokens
This commit is contained in:
parent
1f6c128893
commit
532a1faa21
@ -2,14 +2,24 @@ extends PacketHandler
|
|||||||
|
|
||||||
@export var playerScene: PackedScene
|
@export var playerScene: PackedScene
|
||||||
|
|
||||||
var logged_in := false
|
var auth_token: String
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
%Client.set_handler(Packet.LOGIN, self)
|
%Client.set_handler(Packet.LOGIN, self)
|
||||||
send_login()
|
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():
|
func send_login():
|
||||||
if logged_in:
|
if is_logged_in():
|
||||||
return
|
return
|
||||||
|
|
||||||
var buffer := StreamPeerBuffer.new()
|
var buffer := StreamPeerBuffer.new()
|
||||||
@ -18,14 +28,8 @@ func send_login():
|
|||||||
%Client.socket.put_packet(buffer.data_array)
|
%Client.socket.put_packet(buffer.data_array)
|
||||||
print("[Client] Connecting...")
|
print("[Client] Connecting...")
|
||||||
|
|
||||||
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
func is_logged_in() -> bool:
|
||||||
if data[0] != 0:
|
return auth_token != ""
|
||||||
print("[Client] Login failed.")
|
|
||||||
return
|
|
||||||
|
|
||||||
print("[Client] Login succeeded.")
|
|
||||||
logged_in = true
|
|
||||||
Global.player = spawn_player()
|
|
||||||
|
|
||||||
func spawn_player() -> Player:
|
func spawn_player() -> Player:
|
||||||
var player = playerScene.instantiate()
|
var player = playerScene.instantiate()
|
||||||
|
10
network/server/Client.gd
Normal file
10
network/server/Client.gd
Normal 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 != ""
|
@ -1,5 +0,0 @@
|
|||||||
class_name Connection
|
|
||||||
|
|
||||||
var peer: PacketPeerUDP
|
|
||||||
var address: String
|
|
||||||
var last_packet: int
|
|
@ -14,6 +14,11 @@ func _ready():
|
|||||||
%Server.set_handler(Packet.LOGIN, self)
|
%Server.set_handler(Packet.LOGIN, self)
|
||||||
|
|
||||||
func handle_packet(data: PackedByteArray, peer: PacketPeer):
|
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 data_string = data.get_string_from_utf8()
|
||||||
var login_request = JSON.parse_string(data_string)
|
var login_request = JSON.parse_string(data_string)
|
||||||
|
|
||||||
@ -32,9 +37,12 @@ func handle_packet(data: PackedByteArray, peer: PacketPeer):
|
|||||||
fail_login(peer)
|
fail_login(peer)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
client.auth_token = generate_auth_token()
|
||||||
|
|
||||||
var buffer := StreamPeerBuffer.new()
|
var buffer := StreamPeerBuffer.new()
|
||||||
buffer.put_8(Packet.LOGIN)
|
buffer.put_8(Packet.LOGIN)
|
||||||
buffer.put_8(SUCCESS)
|
buffer.put_8(SUCCESS)
|
||||||
|
buffer.put_data(client.auth_token.to_ascii_buffer())
|
||||||
peer.put_packet(buffer.data_array)
|
peer.put_packet(buffer.data_array)
|
||||||
|
|
||||||
func fail_login(peer: PacketPeer):
|
func fail_login(peer: PacketPeer):
|
||||||
@ -42,3 +50,6 @@ func fail_login(peer: PacketPeer):
|
|||||||
buffer.put_8(Packet.LOGIN)
|
buffer.put_8(Packet.LOGIN)
|
||||||
buffer.put_8(FAIL)
|
buffer.put_8(FAIL)
|
||||||
peer.put_packet(buffer.data_array)
|
peer.put_packet(buffer.data_array)
|
||||||
|
|
||||||
|
func generate_auth_token() -> String:
|
||||||
|
return "secret"
|
||||||
|
@ -27,20 +27,11 @@ func _process(_delta):
|
|||||||
# Accept new connections
|
# Accept new connections
|
||||||
while server.is_connection_available():
|
while server.is_connection_available():
|
||||||
var peer: PacketPeerUDP = server.take_connection()
|
var peer: PacketPeerUDP = server.take_connection()
|
||||||
var address := "%s:%d" % [peer.get_packet_ip(), peer.get_packet_port()]
|
peer_to_client(peer).last_packet = now
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
# Process packets from clients
|
# Process packets from clients
|
||||||
for key in clients:
|
for address in clients:
|
||||||
var client = clients[key]
|
var client = clients[address]
|
||||||
var peer = client.peer
|
var peer = client.peer
|
||||||
|
|
||||||
while peer.get_available_packet_count() > 0:
|
while peer.get_available_packet_count() > 0:
|
||||||
@ -62,8 +53,24 @@ func _process(_delta):
|
|||||||
packet_count = 0
|
packet_count = 0
|
||||||
last_statistics = Time.get_ticks_msec()
|
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)
|
print("[Server] Connected: ", c.address)
|
||||||
|
|
||||||
func peer_disconnected(c: Connection):
|
func peer_disconnected(c: Client):
|
||||||
print("[Server] Disconnected: ", c.address)
|
print("[Server] Disconnected: ", c.address)
|
||||||
|
@ -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")]
|
[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)
|
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)
|
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)
|
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="."]
|
[node name="UI" type="Control" parent="."]
|
||||||
layout_mode = 3
|
layout_mode = 3
|
||||||
|
Loading…
Reference in New Issue
Block a user