From 2919feb52961f30839968019823be8e6ea465cb0 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Fri, 4 Nov 2022 10:12:23 +0100 Subject: [PATCH] Unhardcode more chunk headers --- lib/chunk.rb | 9 ++++++++- lib/net_base.rb | 1 + lib/teeworlds-client.rb | 40 ++++++++++++++++++++++------------------ sample.rb | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/lib/chunk.rb b/lib/chunk.rb index cb38350..e79a5df 100644 --- a/lib/chunk.rb +++ b/lib/chunk.rb @@ -4,7 +4,7 @@ require_relative 'bytes' class NetChunk attr_reader :next, :data, :msg, :sys, :flags - @@sent_vital_chunks = 3 # BIG TODO: SEND READY AND SHIT WITH PROPER HEADER + @@sent_vital_chunks = 1 def initialize(data) @next = nil @@ -20,6 +20,13 @@ class NetChunk @next = data[chunk_end..] if data.size > chunk_end end + def to_s + "NetChunk\n" + + " msg=#{msg} sys=#{sys}\n" + + " #{@flags}\n" + + " data: #{str_hex(data)}" + end + ## # Create int array ready to be send over the network # diff --git a/lib/net_base.rb b/lib/net_base.rb index 142a432..5edbf90 100644 --- a/lib/net_base.rb +++ b/lib/net_base.rb @@ -6,6 +6,7 @@ class NetBase @port = nil @s = nil @ack = 0 + @server_token = [0xFF, 0xFF, 0xFF, 0xFF].map { |b| b.to_s(16) }.join('') end def connect(socket, ip, port) diff --git a/lib/teeworlds-client.rb b/lib/teeworlds-client.rb index 290fde0..fb61e8c 100644 --- a/lib/teeworlds-client.rb +++ b/lib/teeworlds-client.rb @@ -16,15 +16,10 @@ class TwClient def initialize(options = {}) @verbose = options[:verbose] || false - @client_token = MY_TOKEN.map { |b| b.to_s(16) }.join('') - puts "client token #{@client_token}" @state = NET_CONNSTATE_OFFLINE @ip = 'localhost' @port = 8303 @packet_flags = {} - @ticks = 0 - @netbase = NetBase.new - @netbase.client_token = @client_token @hooks = {} @thread_running = false @signal_disconnect = false @@ -42,6 +37,11 @@ class TwClient return end end + @ticks = 0 + @client_token = MY_TOKEN.map { |b| b.to_s(16) }.join('') + puts "client token #{@client_token}" + @netbase = NetBase.new + @netbase.client_token = @client_token @ip = ip @port = port puts "connecting to #{@ip}:#{@port} .." @@ -87,14 +87,14 @@ class TwClient end def send_msg_connect() - header = [0x04, 0x00, 0x00] + str_bytes(@token) - msg = header + [NET_CTRLMSG_CONNECT] + str_bytes(@client_token) + Array.new(501, 0x00) - @s.send(msg.pack('C*'), 0, @ip, @port) + msg = [NET_CTRLMSG_CONNECT] + str_bytes(@client_token) + Array.new(501, 0x00) + @netbase.send_packet(msg, 0, control: true) end def send_ctrl_with_token() @state = NET_CONNSTATE_TOKEN - @s.send(MSG_TOKEN.pack('C*'), 0, @ip, @port) + msg = [NET_CTRLMSG_TOKEN] + str_bytes(@client_token) + Array.new(512, 0x00) + @netbase.send_packet(msg, 0, control: true) end def send_info() @@ -102,21 +102,25 @@ class TwClient end def send_msg_startinfo() - header = [0x00, 0x04, 0x01] + str_bytes(@token) - msg = header + MSG_STARTINFO - @s.send(msg.pack('C*'), 0, @ip, @port) + # todo: build startinfo chunk here + + # create unused chunk just to bump + # the sequence number ._. + NetChunk.create_vital_header({vital: true}, 1) + + @netbase.send_packet(MSG_STARTINFO) end def send_msg_ready() - header = [0x00, 0x01, 0x01] + str_bytes(@token) - msg = header + [0x40, 0x01, 0x02, 0x25] - @s.send(msg.pack('C*'), 0, @ip, @port) + @netbase.send_packet( + NetChunk.create_vital_header({vital: true}, 1) + + [pack_msg_id(NETMSG_READY, system: true)]) end def send_enter_game() @netbase.send_packet( NetChunk.create_vital_header({vital: true}, 1) + - [pack_msg_id(NETMSG_ENTERGAME, true)]) + [pack_msg_id(NETMSG_ENTERGAME, system: true)]) end ## @@ -125,8 +129,8 @@ class TwClient # Takes a NETMSGTYPE_CL_* integer # and returns a byte that can be send over # the network - def pack_msg_id(msg_id, system = false) - (msg_id << 1) | (system ? 1 : 0) + def pack_msg_id(msg_id, options = {system: false}) + (msg_id << 1) | (options[:system] ? 1 : 0) end def send_chat(str) diff --git a/sample.rb b/sample.rb index 030c812..86ffe30 100755 --- a/sample.rb +++ b/sample.rb @@ -31,7 +31,7 @@ client.hook_chat do |msg| puts "chat: #{msg}" end -client.connect(args[:ip], args[:port], detach: true) +client.connect(args[:ip], args[:port], detach: false) loop do sleep 2