Changeset 3232

Show
Ignore:
Timestamp:
01/21/10 23:13:51 (7 weeks ago)
Author:
ghoulsblade
Message:
packet debugging utils
Location:
trunk/lua
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/lua/lib.protocol.lua

    r3212 r3232  
    6363                local sPacketTypeName = gPacketTypeId2Name[iId] -- get packet-type-name 
    6464                local packet_debuginfo = sprintf("typeid=0x%02x,size=%d,typename=%s",iId,iPacketSize,sPacketTypeName or "") 
     65                local iBFSubCmd = (iId == kPacket_Generic_Command) and input:PeekNetUint16(3) 
     66                if (iBFSubCmd) then 
     67                        local genname = gGenericSubCommandNamesByID[iBFSubCmd] or "???" 
     68                        packet_debuginfo = packet_debuginfo .. sprintf(",subcmd=%s[0x%02x]",genname,iBFSubCmd) 
     69                end 
    6570                if (not gNoLogPackets_ByPacket[iId]) then -- log on 
    6671                        printdebug("net",sprintf("NET: ProtocolPacketRecvHandler "..packet_debuginfo)) 
    67                         if (gEnablePacketDebug_Short) then print("packet",packet_debuginfo) end 
     72                        if (gEnablePacketDebug_Short) then  
     73                                if (iBFSubCmd and gNoLogPackets_BySubCmd and gNoLogPackets_BySubCmd[iBFSubCmd]) then 
     74                                        -- skip 
     75                                else  
     76                                        print("packet",packet_debuginfo)  
     77                                end 
     78                        end 
    6879                        if (gbPacketLogToFadeLines) then GuiAddChatLine("recv "..packet_debuginfo) end 
    6980                end 
  • trunk/lua/lib.proxy.lua

    r2986 r3232  
    11-- uoproxy for debugging 
    22 
     3function UOProxyOpenListener (port) 
     4        local timeout = Client_GetTicks() + 5*1000 
     5        local listener 
     6        repeat 
     7                listener = NetListen(port) 
     8                if (not listener) then print("port listen bind fail, retrying...") Client_USleep(1 * 1000) end 
     9        until listener or Client_GetTicks() > timeout  
     10        assert(listener,"failed to bind to local port "..(port or 0)) 
     11        return listener 
     12end 
    313function UOProxyMode (host,port) 
    414        print("starting proxy mode") 
     
    717        gProxyHost = host 
    818        gProxyPort = port 
    9          
    10         local timeout = Client_GetTicks() + 5*1000 
    11         repeat 
    12                 gServerListenerTCP = NetListen(port) 
    13                 if (not gServerListenerTCP) then print("port listen bind fail, retrying...") Client_USleep(1 * 1000) end 
    14         until gServerListenerTCP or Client_GetTicks() > timeout  
    15         assert(gServerListenerTCP,"failed to bind to local port "..(port or 0)) 
     19        gProxyPort2 = port+1 
     20         
     21        gServerListenerTCP = UOProxyOpenListener(gProxyPort) 
     22        gServerListenerTCP2 = UOProxyOpenListener(gProxyPort2) 
    1623         
    1724        print("listen port opened....") 
    1825                 
    1926        while true do 
     27                local listener = gServerListenerTCP 
    2028                while true do 
    21                         local newcon = gServerListenerTCP:PopAccepted() 
     29                        local newcon = listener:PopAccepted() 
    2230                        if (not newcon) then break end 
     31                        print("###############################") 
     32                        print("#### PROXY : connection started, listener=",(listener == gServerListenerTCP) and "A" or "B") 
     33                        print("###############################") 
    2334                        UOProxyOneConnection(newcon) 
     35                        print("###############################") 
     36                        print("#### PROXY : connection ended") 
     37                        print("###############################") 
     38                        listener = gServerListenerTCP2 
    2439                        --~ print("proxy end") return 
    2540                end 
     
    106121        --~ if (bIsFromServer and gProxyServerHuffmanStarted) then bInterpret = false end -- huffman comp&decomp active now 
    107122        --~ if (bIsFromClient and gProxyServerHuffmanStarted) then bInterpret = false end -- not really needed but something seems bugged 
    108          
     123        if (gDisableProxyInterpretation) then bInterpret = false end 
    109124        local bScrambleTest = false 
    110125        local bClientBlockTest = true 
    111126         
    112         if (bIsFromClient and gProxyServerHuffmanStarted and (not gProxySecondPartHeaderStarted)) then 
    113                 gProxySecondPartHeaderStarted = true 
    114                 local iPacketSize = 4 
    115                 print("recv:",fromname,"4byte header before protocol start")  
    116                 print(FIFOHexDump(fifo_in,0,iPacketSize)) 
    117                 fifo_out:PushFIFOPartRaw(fifo_in,0,iPacketSize)  
    118                 fifo_in:PopRaw(iPacketSize) 
    119                 return true 
    120         end 
    121          
    122         -- scramble kPacket_Generic_SubCommand_Screensize (0xBF subcmd 0x05)  last 5 bytes (unknown) 
    123         --~ bf 00 0d 00 05 00 00 03 20 3f d0 11 00            |........ ?...| 
    124         if (bScrambleTest and bIsFromClient and fifo_in:Size() >= 13) then  
    125                 if (fifo_in:PeekNetUint8(0) == 0xBF and 
    126                         fifo_in:PeekNetUint8(1) == 0x00 and 
    127                         fifo_in:PeekNetUint8(2) == 0x0d and 
    128                         fifo_in:PeekNetUint8(3) == 0x00 and 
    129                         fifo_in:PeekNetUint8(4) == 0x05) then 
    130                         fifo_in:PokeNetUint8(9,0xff) 
    131                         fifo_in:PokeNetUint8(10,0xff) 
    132                         fifo_in:PokeNetUint8(11,0xff) 
    133                         fifo_in:PokeNetUint8(12,0xff) 
    134                         print("SCRAMBLED kPacket_Generic_SubCommand_Screensize") 
    135                 end 
    136         end 
    137          
    138          
    139          
    140         -- modify the unknown se packets 
    141         if (bIsFromClient and fifo_in:Size() >= 6) then 
    142                 --~ bf 00 06 00 24 5e 
    143                 if (fifo_in:PeekNetUint8(0) == 0xBF and 
    144                         fifo_in:PeekNetUint8(1) == 0x00 and 
    145                         fifo_in:PeekNetUint8(2) == 0x06 and 
    146                         fifo_in:PeekNetUint8(3) == 0x00 and 
    147                         fifo_in:PeekNetUint8(4) == 0x24) then 
    148                         --~ fifo_in:PokeNetUint8(5,math.random(0,255)) 
    149                         print("detected unknownSE") 
    150                         if (bScrambleTest) then  
    151                                 fifo_in:PokeNetUint8(5,16) 
    152                                 print("SCRAMBLED unknownSE") 
     127        if (bInterpret) then  
     128                -- 4-byte-head con1 
     129                if (bIsFromClient and (not gProxyFirstPartHeaderStarted)) then 
     130                        gProxyFirstPartHeaderStarted = true 
     131                        local iPacketSize = 4 
     132                        print("recv:",fromname,"4byte header before protocol start con1")  
     133                        print(FIFOHexDump(fifo_in,0,iPacketSize)) 
     134                        fifo_out:PushFIFOPartRaw(fifo_in,0,iPacketSize)  
     135                        fifo_in:PopRaw(iPacketSize) 
     136                        return true 
     137                end 
     138                 
     139                -- 4-byte-head con2 
     140                if (bIsFromClient and gProxyServerHuffmanStarted and (not gProxySecondPartHeaderStarted)) then 
     141                        gProxySecondPartHeaderStarted = true 
     142                        local iPacketSize = 4 
     143                        print("recv:",fromname,"4byte header before protocol start con2")  
     144                        print(FIFOHexDump(fifo_in,0,iPacketSize)) 
     145                        fifo_out:PushFIFOPartRaw(fifo_in,0,iPacketSize)  
     146                        fifo_in:PopRaw(iPacketSize) 
     147                        return true 
     148                end 
     149         
     150                -- scramble kPacket_Generic_SubCommand_Screensize (0xBF subcmd 0x05)  last 5 bytes (unknown) 
     151                --~ bf 00 0d 00 05 00 00 03 20 3f d0 11 00            |........ ?...| 
     152                if (bScrambleTest and bIsFromClient and fifo_in:Size() >= 13) then  
     153                        if (fifo_in:PeekNetUint8(0) == 0xBF and 
     154                                fifo_in:PeekNetUint8(1) == 0x00 and 
     155                                fifo_in:PeekNetUint8(2) == 0x0d and 
     156                                fifo_in:PeekNetUint8(3) == 0x00 and 
     157                                fifo_in:PeekNetUint8(4) == 0x05) then 
     158                                fifo_in:PokeNetUint8(9,0xff) 
     159                                fifo_in:PokeNetUint8(10,0xff) 
     160                                fifo_in:PokeNetUint8(11,0xff) 
     161                                fifo_in:PokeNetUint8(12,0xff) 
     162                                print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     163                                print("SCRAMBLED kPacket_Generic_SubCommand_Screensize") 
     164                                print("+++++++++++++++++++++++++++++++++++++++++++++++") 
    153165                        end 
    154                         if (bClientBlockTest) then gProxyBlockingClient = true end 
    155                 end 
    156         end 
    157          
    158          
    159         -- modify kPacket_Pre_Login 0x5D packet data 
    160         if (bScrambleTest and bIsFromClient and fifo_in:Size() >= 49) then 
    161                 if (fifo_in:PeekNetUint8(0) == 0x5D and 
    162                         fifo_in:PeekNetUint8(1) == 0xED and 
    163                         fifo_in:PeekNetUint8(2) == 0xED and 
    164                         fifo_in:PeekNetUint8(3) == 0xED and 
    165                         fifo_in:PeekNetUint8(4) == 0xED) then 
    166                         --~ fifo_in:PokeNetUint8(5,math.random(0,255)) 
    167                         fifo_in:PokeNetUint8(48,0x11) 
    168                         print("SCRAMBLED kPacket_Pre_Login") 
     166                end 
     167                 
     168                 
     169                 
     170                -- modify the unknown se packets 
     171                if (bIsFromClient and fifo_in:Size() >= 6) then 
     172                        --~ bf 00 06 00 24 5e 
     173                        if (fifo_in:PeekNetUint8(0) == 0xBF and 
     174                                fifo_in:PeekNetUint8(1) == 0x00 and 
     175                                fifo_in:PeekNetUint8(2) == 0x06 and 
     176                                fifo_in:PeekNetUint8(3) == 0x00 and 
     177                                fifo_in:PeekNetUint8(4) == 0x24) then 
     178                                --~ fifo_in:PokeNetUint8(5,math.random(0,255)) 
     179                                print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     180                                print("detected unknownSE") 
     181                                print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     182                                if (bScrambleTest) then  
     183                                        fifo_in:PokeNetUint8(5,16) 
     184                                        print("SCRAMBLED unknownSE") 
     185                                end 
     186                                if (bClientBlockTest) then gProxyBlockingClient = true end 
     187                        end 
     188                end 
     189                 
     190                 
     191                -- modify kPacket_Pre_Login 0x5D packet data 
     192                if (bScrambleTest and bIsFromClient and fifo_in:Size() >= 49) then 
     193                        if (fifo_in:PeekNetUint8(0) == 0x5D and 
     194                                fifo_in:PeekNetUint8(1) == 0xED and 
     195                                fifo_in:PeekNetUint8(2) == 0xED and 
     196                                fifo_in:PeekNetUint8(3) == 0xED and 
     197                                fifo_in:PeekNetUint8(4) == 0xED) then 
     198                                --~ fifo_in:PokeNetUint8(5,math.random(0,255)) 
     199                                fifo_in:PokeNetUint8(48,0x11) 
     200                                print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     201                                print("SCRAMBLED kPacket_Pre_Login") 
     202                                print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     203                        end 
    169204                end 
    170205        end 
     
    173208                local iId = fifo_in:PeekNetUint8(0) 
    174209                local iPacketSize = gPacketSizeByID[iId] 
    175                 print("UOProxyHandlePacket",fromname,sprintf("0x%02x",iId),gPacketTypeId2Name[iId], 
    176                                 iPacketSize,"t="..t_since_start) 
     210                print("UOProxyHandlePacket",fromname,sprintf("0x%02x",iId),gPacketTypeId2Name[iId],iPacketSize,"t="..t_since_start) 
    177211                assert(iPacketSize) 
    178                 if (iPacketSize == 0 and fifo_in:Size() < 3) then return end -- packet incomplete 
     212                if (iPacketSize == 0 and fifo_in:Size() < 3) then print("incomplete packet dynsize? <3") return end -- packet incomplete 
    179213                if (iPacketSize == 0) then iPacketSize = fifo_in:PeekNetUint16(1) end 
    180                 if (fifo_in:Size() < iPacketSize) then return end -- packet incomplete 
     214                if (fifo_in:Size() < iPacketSize) then print("incomplete packet ",fifo_in:Size(),iPacketSize) return end -- packet incomplete 
    181215                 
    182216                 
     
    186220                        fifo_in:PokeNetUint8(16*3-5,0) 
    187221                        fifo_in:PokeNetUint8(16*3-6,1) 
     222                        print("adjusted kPacket_Server_List") -- TODO : more than one server ? 
     223                        print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     224                        print("+++++    adjusted  kPacket_Server_List") 
     225                        print("+++++++++++++++++++++++++++++++++++++++++++++++") 
    188226                end 
    189227                -- login.uogamers.com (209=0xD1.173=0xAD.139=0x8B.110=0x6E) 
     
    199237                        fifo_in:PokeNetUint8(3,0) 
    200238                        fifo_in:PokeNetUint8(4,1) 
     239                        local iGameServerPort = gProxyPort2 
     240                        fifo_in:PokeNetUint8(5,floor(iGameServerPort/256)) -- port 
     241                        fifo_in:PokeNetUint8(6,math.mod(iGameServerPort,256)) -- port 
     242                         
    201243                        gProxyServerHuffmanStartedNextRound = true 
     244                        print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     245                        print("+++++    adjusted kPacket_Server_Redirect") 
     246                        print("+++++++++++++++++++++++++++++++++++++++++++++++") 
     247                        gDisableProxyInterpretation = true 
    202248                end 
    203249                 
    204250                -- todo : NetStartHuffman (after redirect?) 
    205251                 
    206                  
     252                --[[ 
    207253                if (bIsFromServer and iId == kPacket_Compressed_Gump) then      --0xDD 
    208254                        local packetfifo = CreateFIFO() 
     
    225271                        MyPrintField(gumpdata,"textline_unicode") 
    226272                end 
    227                  
    228                  
    229                  
    230                  
    231                  
    232273                 
    233274                 
     
    256297                        end 
    257298                end 
     299                ]]-- 
    258300                 
    259301                 
     
    265307                return true 
    266308        else 
    267                 print("recv:",fromname,"(uninterpreted) t_since_start=",t_since_start)  
     309                print("recv:",fromname,"(uninterpreted) t_since_start=",t_since_start,fifo_in:Size())  
    268310                print(FIFOHexDump(fifo_in)) 
    269311                fifo_out:PushFIFOPartRaw(fifo_in)  
     
    322364        while bAlive do 
    323365                -- send  
     366                gProxyServerHuffmanStarted = false 
    324367                if (gProxyServerHuffmanStarted) then 
    325368                        HuffmanCompress(gProxyClientSendFifo,gProxyClientSendCompFifo) -- does NOT remove data from in-fifo. compression can always be completed. 
     
    348391                        gProxyServerCon:Pop(gProxyServerRecvFifo) 
    349392                end 
     393                gProxyServerHuffmanStarted = false 
    350394                 
    351395                -- handle packets