Sign in to follow this  
Followers 0
Glyph

Problem with TCP chat

5 posts in this topic

#1 ·  Posted (edited)

;=+===-======================================+
;= Extoksion TCP Chat Server v0.1 (BETA2)====+
;= Author: (BlackHat Industries) Glyph=======+
;=+===-======================================+
#include<guiconstants.au3>
#include <MemoryConstants.au3>
#include <Memory.au3>
#include <File.au3>
#include <Array.au3>
#include <inet.au3>
$Banned = 0
$username = 0
$password = 0
Global $Debug_Ed = False
$TITLE = "TCP Server"
$gui = GUICreate($TITLE, 500, 400)
GUISetBkColor("0x000000", $gui)
$wee = GUICtrlCreatePic("bg.jpg", 0, 0, 500, 400)
GUICtrlSetState($wee, $gui_disable)
$window = GUICtrlCreateEdit("", 10, 10, 350, 335, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))
GUICtrlSetColor(-1, "0x00FF00")
GUICtrlSetBkColor(-1, "0x000000")
GUICtrlSetFont(-1, 7.5, 400)
Global $Users_Online = 0
Global $Admins_Online = 0
$userbar = GUICtrlCreateListView("(" & $Users_Online & ") User(s)            ", 370, 10, 120, 335)
GUICtrlSetColor(-1, "0xffffff")
GUICtrlSetBkColor(-1, "0x000000")


Dim $user[1]
$user[0] = "User array 0"

$usercon = 0

$exit = GUICtrlCreateButton("Exit", 395, 375, 75, 20)

Global $Ip = @IPAddress1
Global $Port = 666
GUICtrlSetData($window, "(Hosted on: " & $Ip & ":" & $Port & ")" & @CRLF & @CRLF)

GUISetState()

TCPStartup()

Global $UserLimit = 10000;max users at once.
Global $User_List[$UserLimit+1][4] ;4 Dimensional userlist array
;[0] = socket
;[1] = user
;[2] = pass
;[3] = token
Global $Database_File = "Database.ini"
;Create the main admin user.
;Create random 12 character password for default admin account
If Not FileExists($Database_File) Then
    $pass = ""
    $wee = 0
    Do
        $wee += 1
        $pass = $pass & Chr(Random(33, 126, 1))
    Until $wee = 12
    
    IniWrite($Database_File, "Admin", "Password", $pass)
    IniWrite($Database_File, "Admin", "Admin", "1")
EndIf

;start to listen for users and messages.
$MainSocket = TCPListen($Ip, $Port, $UserLimit)
;if the server was unable to bind then
If $MainSocket = -1 Then
    MsgBox(16, "Error", "Unable to bind to socket! Port: (" & $Port & ")")
    Exit
EndIf

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  Done!" & @CRLF)

While 1
    Sleep(100)
    $msg = GUIGetMsg()
    
    If $msg = $GUI_EVENT_CLOSE Or $msg = $exit Then
        ExitLoop
    EndIf
    
    ;Gather Data and look what should be done with it.
    _RecvData()
    ;Wait for a user to connect
    $New_Socket = TCPAccept($MainSocket)

    ;if not, then try more.
    If $New_Socket = -1 Then ContinueLoop

    ;Add the user that's trying to connect!
    _AddUser($New_Socket)
WEnd

Func _RecvData()
    ;Enter a loop to check all the users.
    For $x = 1 To $UserLimit; 1 - 25k LOL?
        If $User_List[$x][0] = 0 Then ContinueLoop ;no socket
        ;Receive data from the users.
        $Recv = TCPRecv($User_List[$x][0], 2048);receive from socket
        ;If data is empty then skip the rest of the loop.
        If $Recv = "" Then ContinueLoop
        ;If @error then the user has disconnected.
        If @error Then _DelSocket($User_List[$x][0])
        ;Split the data
        $Data = StringSplit($Recv, "|")
        If Not IsArray($Data) Then ContinueLoop

        ;check and see if we have enought data to work with.
        If UBound($Data) < 2 Then ContinueLoop
        
        $ipz = SocketToIP($User_List[$x][0])
        
        If FileExists("ipbans.txt") Then
            $Lines = _FileCountLines("ipbans.txt")
        Else
            $Lines = 0
        EndIf
        
        If $Lines > 0 Then
            $file = FileOpen("ipbans.txt", 0)
            
            For $v = 1 To $Lines
                $line = FileReadLine($file, $v)
                If $ipz <> $line Then
                    $Banned = 0
                Else
                    $Banned = 1
                    _DelSocket($User_List[$x][0])
                    _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  " & $ipz & " Is banned." & @CRLF)
                EndIf
            Next
        EndIf
        If $Banned = 0 Then
            
            Switch $Data[1]
                Case "Hello"
                    $Data = StringSplit($Recv, "|")
                    If $Data[0] = "4" Then ; login | name | pass | client token
                        
                        $var = IniReadSectionNames($Database_File)
                        If @error Then
                            MsgBox(4096, "", "Error occurred, probably no INI file.")
                        Else
                            For $i = 1 To $var[0] ;check existing list of registered users to see if the kid logging in exists and has correct password.
                                ConsoleWrite("attempted login"&@CRLF)
                                If $var[$i] = $Data[2] Then ;user exists, now check pass.
                                    ConsoleWrite("user match"&@CRLF)
                                    $username = 1
                                    If IniRead($Database_File,$var[$i],"Password","") = $Data[3] Then ; pass matches, log token and show login.
                                        $password = 1
                                        ConsoleWrite("pass match"&@CRLF)
                                            ConsoleWrite("index is: "&$Users_Online&@CRLF)
                                            If $User_List[$Users_Online][0] = 0 Then
                                                $usercon += 1
                                                $Users_Online += 1
                                                GUICtrlSetData($userbar, "(" & $Users_Online & ") User(s)")
                                                $User_List[$Users_Online][1] = $Data[2]
                                                $User_List[$Users_Online][2] = $Data[3]
                                                $User_List[$Users_Online][3] = $Data[4]
                                                _ArrayAdd($user, GUICtrlCreateListViewItem($Data[2], $userbar))
                                                
                                                For $t = 1 To $Users_Online
                                                    ;send userlist
                                                    If $User_List[$Users_Online][1] = $Data[2] Then
                                                        ;
                                                    Else
                                                        ConsoleWrite("index2 is: "&$t&@CRLF)
                                                        TCPSend($User_List[$t][0], "Login|" & $User_List[$Users_Online-1][1])
                                                    EndIf
                                                Next
                                                
                                                ;GUICtrlSetBkColor($user[$pos],0x6f6f6f)
                                                ConsoleWrite("index3 is: "&$Users_Online&@CRLF)
                                                ;GUICtrlSetImage($user[$x-1], "image1.bmp")
                                                ;checkchar()
                                                _GUICtrlEdit_AppendText($window, "================================================" & @CRLF & "[User Login#" & _
                                                        $usercon & " @" & @HOUR & ":" & @MIN & ":" & @SEC & "]" & @CRLF & "+IP/Hostname: " & $ipz & " / " & _
                                                        _TCPIpToName($ipz) & @CRLF & "+Token: " & $User_List[$Users_Online-1][3] & @CRLF & "+Index: " & $Users_Online-1 & _
                                                        @CRLF & "+User/Pass: " & $Data[2] & "/" & $Data[3] & @CRLF & "================================================" & @CRLF)
                                                
                                                For $lu = 1 To $Users_Online
                                                    _SendAll("login|", $Data[2])
                                                    ConsoleWrite("index send is: "&$lu&@CRLF)
                                                Next
                                            Else
                                                ;_broadcast($ipz, "server|duplicates")
                                            EndIf;done
                                    EndIf
                                EndIf
                            Next
                        EndIf
                        If $username = 0 Then
                            ;checkchar()
                            _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  " & $ipz & " failed username authentication: " & $Data[2] & ":" & $Data[3] & @CRLF)
                            ;_broadcast($ipz, "server|failuser1")
                        Else
                            If $password = 0 Then
                                ;checkchar()
                                _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  " & $ipz & " failed password authentication: " & $Data[2] & ":" & $Data[3] & @CRLF)
                                ;_broadcast($ipz, "server|failuser2")
                            EndIf
                        EndIf
                        If $username = 1 Or $password = 1 Then;reset the varibales to allow other users to login.
                            $username = 0
                            $password = 0
                        EndIf
                    EndIf
                    
                Case "Register"
                    If $Data[0] = "3" Then ;register | name | pass
                        If StringLen($Data[2]) <= "24" And StringLen($Data[3]) <= "24" And StringLen($Data[2]) >= "1" And StringLen($Data[3]) >= "1" _
                                And StringInStr($Data[2], "=") = 0 And StringInStr($Data[2], ":") = 0 And _
                                StringInStr($Data[2], "/") = 0 And StringInStr($Data[2], "\") = 0 And _
                                StringInStr($Data[2], "#") = 0 And StringIsASCII($Data[2]) = 1 And _
                                StringInStr($Data[2], "[") = 0 And StringInStr($Data[2], "]") = 0 And StringInStr($Data[3], "]") = 0 And _
                                StringInStr($Data[3], "[") = 0 And StringInStr($Data[3], "=") = 0 Then
                            $Data[2] = StringStripWS($Data[2], 8)
                            $var = IniRead($Database_File, $Data[2], "Password", "Not Found")
                            $bannamed = IniRead("userbans.txt", "Members", $Data[2], "Not Found")
                            If $bannamed = "Not Found" Then
                                
                                If $var = "Not Found" Then
                                    IniWrite($Database_File, $Data[2], "Password", $Data[3])
                                    IniWrite($Database_File, $Data[2], "Admin", "0")
                                    ;checkchar()
                                    _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  " & $Data[2] & "'s registration was completed from: " & $ipz & "." & @CRLF)
                                Else
                                    ;checkchar()
                                    _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  Failed account creation from: " & $ipz & ", """ & $Data[2] & """ already exists!" & @CRLF)
                                    _SendAll("server", "regexists")
                                EndIf
                            Else
                                ;checkchar()
                                _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  Failed account creation from: " & $ipz & ", """ & $Data[2] & @CRLF)
                                _SendAll("server", "banneduser|" & $Data[2])
                            EndIf
                        Else
                            ;checkchar()
                            _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "]  Failed account creation from: " & $ipz & ", """ & $Data[2] & "/" & $Data[3] & """ Illegal character(s)" & @CRLF)
                            _SendAll("server", "illegalchar")
                        EndIf
                    EndIf
                Case "Text"
                    MsgBox(0, "", $Data[2])
                    
                Case "Showmepeople"
                    ;show the user logged on users
                    
                Case "Bye"
                    ;disc them
                    
                Case "PM"
                    ;
                Case "Kick"
                    ;
                Case "BanUser"
                    ;ban user
                    
                Case "BanIP"
                    ;ban IP
                    
                Case "Unban"
                    ;
                Case Else
                    ;Debug stuff.
                    If $Data[1] <> "" Then
                        ConsoleWrite("Unknown Command: " & $Data[1] & @CRLF)
                    EndIf
            EndSwitch
        EndIf
    Next
    ;Safely exit the function.
    Return 1
EndFunc   ;==>_RecvData

Func _AddUser($iSocket)
    ;Declare a $var to check. if not, the server is full.
    Local $Temp = 0

    ;Enter a For loop look for an open spot.
    For $x = 1 To $UserLimit
        ;If there is a spot open then
        If $User_List[$x][0] = 0 Then
            ;Set the user's socket to a spot
            $User_List[$x][0] = $iSocket
            ;Exit the For loop, no need to continue when we found an open spot.
            ExitLoop
        EndIf
    Next
EndFunc   ;==>_AddUser

Func _DelSocket($sSocket)
    ;enter a loop to check all the users.
    For $x = 1 To $UserLimit

        ;If we hit the correct user then
        If $User_List[$x][0] = $sSocket Then

            ;Close his socket.
            TCPCloseSocket($User_List[$x][0])


            ;if a username is register to the array then.
            If $User_List[$x][1] <> "" Then

                ;tell the rest of the people to delete that user from the user list.
                _SendAll("Deluser", $User_List[$x][1])
                ;then delete the user form the gui
                
                ;Set the uers online status to 0.
                IniWrite($Database_File, $User_List[$x][1], "Online_Status", "0")

                ;Set the users/admins online for the console.
                If IniRead($Database_File, $User_List[$x][1], "Admin", "0") = "1" Then
                    $Admins_Online -= 1
                Else
                    $Users_Online -= 1
                EndIf
                
                $User_List[$x][0] = 0
            EndIf
            Return 1
        EndIf
    Next
EndFunc   ;==>_DelSocket

Func _SendAll($sCommand, $sString)
    For $x = 1 To $UserLimit
        ;if there is a user on that socket then
        If $User_List[$x][0] <> 0 Then
            ;Send the message to all the users.
            TCPSend($User_List[$x][0], $sCommand & Chr(2) & $sString)
        EndIf
    Next
    Return 1
EndFunc   ;==>_SendAll

;
;
;GUI Functions.
;
;
Func _GUICtrlEdit_AppendText($hWnd, $sText)
    If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    Local $struct_MemMap, $struct_String, $sBuffer_pointer, $iLength

    $struct_String = DllStructCreate("char Text[" & StringLen($sText) + 1 & "]")
    $sBuffer_pointer = DllStructGetPtr($struct_String)
    DllStructSetData($struct_String, "Text", $sText)
    _MemInit($hWnd, StringLen($sText) + 1, $struct_MemMap)
    _MemWrite($struct_MemMap, $sBuffer_pointer)
    $iLength = _GUICtrlEdit_GetTextLen($hWnd)
    _GUICtrlEdit_SetSel($hWnd, $iLength, $iLength)
    _SendMessage($hWnd, $EM_REPLACESEL, True, $sBuffer_pointer, 0, "wparam", "ptr")
    _MemFree($struct_MemMap)
EndFunc   ;==>_GUICtrlEdit_AppendText


Func _GUICtrlEdit_ValidateClassName($hWnd)
    _GUICtrlEdit_DebugPrint("This is for debugging only, set the debug variable to false before submitting")
    _WinAPI_ValidateClassName($hWnd, "Edit")
EndFunc   ;==>_GUICtrlEdit_ValidateClassName


Func _GUICtrlEdit_GetTextLen($hWnd)
    If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    Return _SendMessage($hWnd, $WM_GETTEXTLENGTH)
EndFunc   ;==>_GUICtrlEdit_GetTextLen

Func _GUICtrlEdit_SetSel($hWnd, $iStart, $iEnd)
    If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    _SendMessage($hWnd, $EM_SETSEL, $iStart, $iEnd)
EndFunc   ;==>_GUICtrlEdit_SetSel

Func _GUICtrlEdit_DebugPrint($sText, $iLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+======================================================" & @LF & _
            "-->Line(" & StringFormat("%04d", $iLine) & "):" & @TAB & $sText & @LF & _
            "+======================================================" & @LF)
EndFunc   ;==>_GUICtrlEdit_DebugPrint

Func _GUICtrlEdit_LineIndex($hWnd, $iIndex = -1)
    If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    Return _SendMessage($hWnd, $EM_LINEINDEX, $iIndex)
EndFunc   ;==>_GUICtrlEdit_LineIndex

Func _GUICtrlEdit_LineLength($hWnd, $iIndex = -1)
    If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    Local $CharIndex = _GUICtrlEdit_LineIndex($hWnd, $iIndex)
    Return _SendMessage($hWnd, $EM_LINELENGTH, $CharIndex)
EndFunc   ;==>_GUICtrlEdit_LineLength

Func _GUICtrlEdit_GetLine($hWnd, $iLine)
    If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    Local $iLength, $struct_Buffer, $iResult, $struct_String
    
    $iLength = _GUICtrlEdit_LineLength($hWnd, $iLine)
    $struct_Buffer = DllStructCreate("short Len;char Text[" & $iLength + 2 & "]")
    DllStructSetData($struct_Buffer, "Len", $iLength + 2)
    
    $iResult = _SendMessageA($hWnd, $EM_GETLINE, $iLine, DllStructGetPtr($struct_Buffer), 0, "wparam", "ptr")
    
    If $iResult = $EC_ERR Then Return SetError($EC_ERR, $EC_ERR, "")
    
    $struct_String = DllStructCreate("char Text[" & $iLength + 1 & "]", DllStructGetPtr($struct_Buffer))
    Return DllStructGetData($struct_String, "Text")
EndFunc   ;==>_GUICtrlEdit_GetLine

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0
    
    Return $aRet
EndFunc   ;==>SocketToIP

When a second user logs in the program, it freezes and does nothing.

I don't even see it get to the authentication, it's like it gets stuck before authentication....

any ideas? or suggestions?

greatly appreciate it!

Edited by BackStabbed

tolle indicium

Share this post


Link to post
Share on other sites



bump


tolle indicium

Share this post


Link to post
Share on other sites

Bump


tolle indicium

Share this post


Link to post
Share on other sites

help o.o


tolle indicium

Share this post


Link to post
Share on other sites

if you still havent found your answer, check my sig, its a multiclient WORKS that i pieced together from other peoples work, and added many hours of my own time into making it personalized, i havent looked through your script BUT if it freezes im guessing there is something wrong with the accept array

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0