Hallo alle miteinander,
Aufgrund der Gewitter heute, hab ich mich mal hingesetzt um irgendwas zu skripten... Und dabei kam dann folgender Netter Chat bei heraus:
Client:
Spoiler anzeigen
#Region Main Options
#NoTrayIcon
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
TCPStartup()
[/autoit] [autoit][/autoit] [autoit]Opt('GUICloseOnESC', False)
Opt('GUIOnEventMode', False)
#EndRegion Main Options
#Region Declaration
Global Const $Addr = '127.0.0.1', $Port = 8000, $sBreak = Chr(13) & Chr(10)
Global $Socket = -1, $iSend = 0, $bRecv = '', $bSend = Binary('')
Global $iLogin = False, $Timeout = -1, $iDelay = 1, $iLines = 1
#EndRegion Declaration
While True
Global $Socket = TCPConnect($Addr, $Port)
While $Socket <> -1
#Region Login Gui
Global $sUsername, $sPassword
Global $iLoginForm = GUICreate('Login', 214, 138, -1, -1)
GUISetFont(10, 400, 0, 'Arial')
GUICtrlCreateGroup('User settings', 4, 0, 205, 105)
GUICtrlCreateLabel('Username:', 12, 24, 67, 20)
GUICtrlCreateLabel('Password:', 12, 52, 65, 20)
Global $inpUser = GUICtrlCreateInput('', 80, 24, 121, 24)
Global $inpPass = GUICtrlCreateInput('', 80, 52, 121, 24, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL))
Global $chkSave = GUICtrlCreateCheckbox('Save', 80, 80, 97, 17)
Global $btnRegister = GUICtrlCreateButton('Register', 53, 108, 75, 25, $WS_GROUP)
Global $btnLogin = GUICtrlCreateButton('Login', 132, 108, 75, 25, $WS_GROUP)
If IniRead('C:\Settings.ini', 'Settings', 'Save', 'No') = 'Yes' Then
GUICtrlSetData($inpUser, IniRead('C:\Settings.ini', 'Settings', 'Username', ''))
GUICtrlSetData($inpPass, BinaryToString(IniRead('C:\Settings.ini', 'Settings', 'Password', '')))
GUICtrlSetState($chkSave, $GUI_CHECKED)
GUICtrlSetState($btnRegister, $GUI_DISABLE)
EndIf
GUISetState()
#EndRegion Login Gui
#Region 'Insert-Login-Infos'
Do
If GUIGetMsg() = -3 Then Exit
If GUICtrlRead($inpUser) <> $sUsername Then Global $sUsername = GUICtrlRead($inpUser)
If GUICtrlRead($inpPass) <> $sPassword Then Global $sPassword = GUICtrlRead($inpPass)
#Region Register / Login
If $sUsername <> '' And $sPassword <> '' Then
Switch GUIGetMsg()
Case $btnRegister
Global $iRegister = False
GUICtrlSetState($inpUser, $GUI_DISABLE)
GUICtrlSetState($inpPass, $GUI_DISABLE)
GUICtrlSetState($btnLogin, $GUI_DISABLE)
GUICtrlSetState($btnRegister, $GUI_DISABLE)
Global $bUsername = StringToBinary($sUsername)
Global $bPassword = StringToBinary($sPassword)
Global $bPacket = Binary('0x09') & $bUsername & Binary('0x00') & $bPassword & Binary('0x00')
Global $iSend = TCPSend($Socket, $bPacket)
If $iSend > 0 Then
Global $Timer = TimerInit(), $bRecv = Binary('')
Do
$bRecv = TCPRecv($Socket, 16, True)
If $bRecv <> '' Then
If $bRecv = Binary('0x09') Then $iRegister = True
EndIf
Until TimerDiff($Timer) >= 2 ^ 12 Or $iRegister = True
GUICtrlSetState($inpUser, $GUI_ENABLE)
GUICtrlSetState($inpPass, $GUI_ENABLE)
GUICtrlSetState($btnLogin, $GUI_ENABLE)
EndIf
If $iRegister = True Then
MsgBox(0, 'Registration successfully!', 'Congratulation! You could now login with your account!')
EndIf
If $iRegister = False Then
MsgBox(16, 'Registration error!', 'The registration has failed!')
GUICtrlSetState($btnRegister, $GUI_ENABLE)
EndIf
Case $btnLogin
ExitLoop
EndSwitch
EndIf
#EndRegion Register / Login
Until False
GUICtrlSetState($inpUser, $GUI_DISABLE)
GUICtrlSetState($inpPass, $GUI_DISABLE)
GUICtrlSetState($btnLogin, $GUI_DISABLE)
GUICtrlSetState($btnRegister, $GUI_DISABLE)
If GUICtrlGetState($chkSave) = $GUI_CHECKED Then MsgBox(0, '', 'Checked!')
GUICtrlSetData($btnLogin, 'Login...')
#EndRegion 'Insert-Login-Infos'
#Region Verify Login
Global $bPacket = Binary('0x01') & StringToBinary($sUsername) & Binary('0x00') & StringToBinary($sPassword) & Binary('0x00'), $Timeout = TimerInit()
Do
Global $iSend = TCPSend($Socket, $bPacket)
If $iSend > 0 Then
Global $Timer = TimerInit(), $bRecv = ''
Do
$bRecv &= TCPRecv($Socket, 512, True)
Sleep($iDelay)
Until TimerDiff($Timer) > 2048
If BinaryMid($bRecv, 1, 1) = 0x01 And BinaryLen($bRecv) = 2 Then
GUICtrlSetData($btnLogin, 'Verify...')
Global $bKey = BinaryMid($bRecv, 2, 1)
Global $iSend = TCPSend($Socket, Binary('0x01'))
If $iSend > 0 Then
$iLogin = True
EndIf
Else
If $bRecv = 0x08 Then
GUIDelete($iLoginForm)
Global $iSend = TCPSend($Socket, Binary('0x07'))
MsgBox(16, 'Login error!', 'User information does not match or you are already logged in!')
ExitLoop 2
EndIf
EndIf
EndIf
[/autoit] [autoit][/autoit] [autoit]Sleep($iDelay)
Until $iLogin = True Or TimerDiff($Timeout) = 2 ^ 16
If BitAnd(GUICtrlRead($chkSave), $GUI_CHECKED) Then
IniWrite('C:\Settings.ini', 'Settings', 'Save', 'Yes')
IniWrite('C:\Settings.ini', 'Settings', 'Username', $sUsername)
IniWrite('C:\Settings.ini', 'Settings', 'Password', StringToBinary($sPassword))
Else
IniWrite('C:\Settings.ini', 'Settings', 'Save', 'No')
EndIf
If WinExists($iLoginForm) Then GUIDelete($iLoginForm)
#EndRegion Verify Login
#Region Main
If $iLogin = True Then
#Region Create Lobby Gui
Global $LobbyForm = GUICreate("Lobby", 541, 313, -1, -1)
GUISetFont(10, 400, 0, "Arial")
Global $txtHistory = GUICtrlCreateEdit("", 4, 5, 409, 276, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
Global $lstUsers = GUICtrlCreateList("", 416, 4, 121, 279)
Global $txtSend = GUICtrlCreateInput("", 4, 284, 409, 24)
GUICtrlSetLimit($txtSend, 128)
GUICtrlSetCursor($txtSend, 5)
Global $btnUnselect = GUICtrlCreateButton("Unselect", 416, 284, 121, 25, $WS_GROUP)
GUISetState()
#EndRegion Create Lobby Gui
Global $Timeout = TimerInit(), $bRecv = '', $iSend = 0
Do
#Region User actions
If WinActive($LobbyForm) Then
Global $aCall = DllCall('user32.dll', "short", "GetAsyncKeyState", "int", '0x0d')
If BitAND($aCall[0], 0x8000) <> 0 Then
Global $bTemp = StringToBinary(GUICtrlRead($txtSend))
If $bTemp <> '' Then
#Region Messenger
Global $bUser = StringToBinary(GUICtrlRead($lstUsers))
If $bUser <> '' Then
Global $bPacket = Binary('0x05') & $bUser & Binary('0x00') & $bTemp & Binary('0x00')
GUICtrlSetData($txtHistory, GUICtrlRead($txtHistory) & BinaryToString($bUser) & ' (To): ' & BinaryToString($bTemp) & $sBreak)
$iLines += 1
DllCall('user32.dll', 'lresult', 'SendMessageW', 'hwnd', GUICtrlGetHandle($txtHistory), 'uint', 0xB6, 'wparam', 0, 'lparam', ($iLines + 1))
Else
Global $bPacket = Binary('0x04') & $bTemp & Binary('0x00')
EndIf
#EndRegion Messenger
$bSend &= $bPacket
GUICtrlSetData($txtSend, '')
Do
Global $aCall = DllCall('user32.dll', "short", "GetAsyncKeyState", "int", '0x0d')
Until BitAND($aCall[0], 0x8000) = 0
EndIf
EndIf
EndIf
#EndRegion User actions
Global $bRecv = TCPRecv($Socket, 512, True)
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]#Region Parser
If $bRecv <> '' Then
Global $bData = Binary('')
For $iPos = 1 To BinaryLen($bRecv) Step True
$bData &= Binary('0x' & Hex(BitXOR(BinaryMid($bRecv, $iPos, 1), $bKey), 2))
Next
Global $iLen = BinaryLen($bData)
For $iMain = 1 To $iLen Step 1
Global $bTemp = Binary(''), $bMid = BinaryMid($bData, $iMain, 1)
Switch $bMid
Case 0x02 ; Received Ping
$bSend &= Binary('0x03')
$Timeout = TimerInit()
Case 0x04, 0x05 ; Received a Msg (0x04 = Global; 0x05 = Private)
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bUser = $bTemp
Global $sUser = BinaryToString($bUser)
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bMsg = $bTemp
Global $sMsg = BinaryToString($bMsg)
Switch $bMid
Case 0x04
GUICtrlSetData($txtHistory, GUICtrlRead($txtHistory) & $sUser & ' (All): ' & $sMsg & $sBreak)
Case 0x05
GUICtrlSetData($txtHistory, GUICtrlRead($txtHistory) & $sUser & ' (From): ' & $sMsg & $sBreak)
EndSwitch
$iLines += 1
DllCall('user32.dll', 'lresult', 'SendMessageW', 'hwnd', GUICtrlGetHandle($txtHistory), 'uint', 0xB6, 'wparam', 0, 'lparam', ($iLines + 1))
ExitLoop 2
Else
$bTemp &= $tMid
EndIf
Next
Else
$bTemp &= $tMid
EndIf
Next
Case 0x06, 0x07 ; User logged in/out
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bUser = $bTemp
Global $sUser = BinaryToString($bUser)
Switch $bMid
Case 0x06
GUICtrlSendMsg($lstUsers, 0x0180, 0, $sUser)
Case 0x07
GUICtrlSendMsg($lstUsers, 0x0182, GUICtrlSendMsg($lstUsers, 0x01A2, -1, $sUser), 0)
EndSwitch
ExitLoop
Else
$bTemp &= $tMid
EndIf
Next
EndSwitch
If GUIGetMsg() = -3 Then Exit
Next
EndIf
#EndRegion Parser
#Region Sender
Global $bTemp = Binary('')
If $bSend <> '' Then
For $iPos = 1 To BinaryLen($bSend) Step True
$bTemp &= Binary('0x' & Hex(BitXOR(BinaryMid($bSend, $iPos, 1), $bKey), 2))
If GUIGetMsg() = -3 Then Exit
Next
Global $iSend = TCPSend($Socket, $bTemp)
If $iSend <= 0 Then Exit
EndIf
Global $bSend = Binary(''), $bRecv = Binary('')
#EndRegion Sender
#Region Gui Events
Switch GUIGetMsg()
Case -3
Exit
Case $btnUnselect
GUICtrlSendMsg($lstUsers, 0x0186, -1, 0)
EndSwitch
#EndRegion Gui Events
Until $iLogin = False Or $Socket = -1 Or TimerDiff($Timeout) >= 2 ^ 14
Else
MsgBox(16, 'Login error!', 'An error accured while login! Check your login informations and try again!')
EndIf
#EndRegion Main
#Region Reset
MsgBox(16, 'Unknown error!', 'The connection to the server has been closed.')
Global $Socket = TCPCloseSocket($Socket), $bRecv = '', $iSend = 0, $bSend = Binary('')
Global $iLogin = False, $Timeout = -1
#EndRegion Reset
ExitLoop
WEnd
#Region Connection Problems
If IsDeclared('LobbyForm') Then
If WinExists($LobbyForm) Then GUIDelete($LobbyForm)
EndIf
If $Socket = -1 Then
Global $iAnswer = MsgBox(20, 'Connection error!', 'Cannot connect to the server! Do you want to try it again?')
If $iAnswer = 7 Then Exit
EndIf
#EndRegion Connection Problems
Sleep($iDelay)
WEnd
Server:
Spoiler anzeigen
#Region Main Options
#NoTrayIcon
#AutoIt3Wrapper_Change2CUI=y
TCPStartup()
#EndRegion Main Options
#Region Declaration
Global Const $sBreak = Chr(13) & Chr(10), $iDelay = 1, $Port = 8000
Global $Listen = TCPListen('', $Port, 1)
Global $iSocks = 16, $bRecv = Binary('')
Global $Socket[$iSocks + 1][3], $bKey[$iSocks + 1], $bSend[$iSocks + 1], $bAll = Binary('')
Global $Timer[$iSocks + 1], $Timeout[$iSocks + 1]
; Reset all arrays:
For $iCur = 0 To $iSocks
$Socket[$iCur][0] = -1 ; Raw socket
$Socket[$iCur][1] = False ; Login state
$Socket[$iCur][2] = Binary('') ; bUsername
$bSend[$iCur] = Binary('') ; Send buffer
$bKey[$iCur] = Binary('') ; Encryption key var
Next
#EndRegion Declaration
#Region Main
While True
For $iSock = 0 To $iSocks
If $Socket[$iSock][0] = -1 Then
$Socket[$iSock][0] = TCPAccept($Listen)
Else
If $Socket[$iSock][1] = True Then
#Region Chat
If TimerDiff($Timer[$iSock]) >= 2 ^ 12 Then
$bSend[$iSock] &= Binary('0x02')
$Timer[$iSock] = TimerInit()
EndIf
Global $bRecv = TCPRecv($Socket[$iSock][0], 512, True)
If $bRecv <> '' Then
Global $bData = Binary('')
For $iPos = 1 To BinaryLen($bRecv) Step True
$bData &= Binary('0x' & Hex(BitXOR(BinaryMid($bRecv, $iPos, 1), $bKey[$iSock]), 2))
Next
Global $iLen = BinaryLen($bData), $bMid, $tMid = Binary('')
#Region Parser
For $iMain = 1 To $iLen Step 1
Global $bTemp = Binary(''), $bMid = BinaryMid($bData, $iMain, 1)
If $bData <> '' Then ConsoleWrite($bData & $sBreak)
Switch $bMid
Case 0x03 ; Received Pong
$Timeout[$iSock] = TimerInit()
Case 0x04 ; Received a global Msg
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bMsg = $bTemp
$bAll &= Binary('0x04') & $Socket[$iSock][2] & Binary('0x00') & $bMsg & Binary('0x00')
ExitLoop
Else
$bTemp &= $tMid
EndIf
Next
Case 0x05 ; Received a private Msg
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bUser = $bTemp
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bMsg = $bTemp
For $iTemp = 0 To $iSocks Step True
If $Socket[$iTemp][2] = $bUser Then
$bSend[$iTemp] &= Binary('0x05') & $Socket[$iSock][2] & Binary('0x00') & $bMsg & Binary('0x00')
EndIf
Next
ExitLoop 2
Else
$bTemp &= $tMid
EndIf
Next
Else
$bTemp &= $tMid
EndIf
Next
Case 0x07 ; User logged out
$bAll &= Binary('0x07') & $Socket[$iSock][2] & Binary('0x00')
EndSwitch
Next
#EndRegion Parser
EndIf
If TimerDiff($Timeout[$iSock]) >= 2 ^ 14 Then
$bAll &= Binary('0x07') & $Socket[$iSock][2] & Binary('0x00')
TCPCloseSocket($Socket[$iSock][0])
$Socket[$iSock][0] = -1
$Socket[$iSock][1] = False
$Socket[$iSock][2] = Binary('')
EndIf
#EndRegion Chat
ElseIf $Socket[$iSock][1] = False Then
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]#Region Login Step One
Global $iSuccess = False, $tMid = Binary('')
Global $bRecv = TCPRecv($Socket[$iSock][0], 512, True)
If $bRecv <> '' Then
Global $bData = $bRecv
Global $iLen = BinaryLen($bData)
Global $tMid = BinaryMid($bData, 1, 1)
Global $bTemp = Binary('')
If $tMid = 0x01 Then
#Region Login Part
Global $bTemp = Binary('')
For $iMain = 2 To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bUsername = $bTemp
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bPassword = $bTemp
If IniRead('C:\Usertable.ini', StringLower(BinaryToString($bUsername)), 'Password', '') = $bPassword Then
$iSuccess = True
For $iTemp = 0 To $iSocks Step True
If $Socket[$iTemp][2] = $bUsername Then $iSuccess = False
Next
EndIf
#Region Testing Login
If $iSuccess = False Then
Global $iSend = TCPSend($Socket[$iSock][0], Binary('0x08'))
If $iSend <= 0 Or $iSend >= 0 Then
TCPCloseSocket($Socket[$iSock][0]) ; Close the socket directly
$Socket[$iSock][0] = -1
$Socket[$iSock][1] = False
$Socket[$iSock][2] = Binary('')
EndIf
Else
#Region Key Creation
Global $iRandom = Random(0, 255, 1)
$bKey[$iSock] = Binary('0x' & Hex($iRandom, 2))
Global $bPacket = Binary('0x01') & $bKey[$iSock]
Global $iSend = TCPSend($Socket[$iSock][0], $bPacket)
$Socket[$iSock][1] = Default
$Socket[$iSock][2] = $bUsername
$Timer[$iSock] = TimerInit()
$Timeout[$iSock] = TimerInit()
#EndRegion Key Creation
EndIf
#EndRegion Testing Login
ExitLoop 2
Else
$bTemp &= $tMid
EndIf
Next
Else
$bTemp &= $tMid
EndIf
Next
#EndRegion Login Part
ElseIf $tMid = 0x09 Then
[/autoit] [autoit][/autoit] [autoit]#Region Register
For $iMain = 2 To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bUsername = $bTemp
Global $bTemp = Binary('')
For $iMain = ($iMain + 1) To $iLen Step 1
$tMid = BinaryMid($bData, $iMain, 1)
If $tMid = 0x00 Then
Global $bPassword = $bTemp
Global $iRegister = True
Global $aRet = IniReadSectionNames('C:\Usertable.ini')
If IsArray($aRet) Then
For $iSearch = 1 To $aRet[0] Step 1
If $aRet[$iSearch] = $bUsername Then
$iRegister = False
EndIf
Next
Else
$iRegister = True
EndIf
If $iRegister Then
Global $iSend = TCPSend($Socket[$iSock][0], Binary('0x09'))
IniWrite('C:\Usertable.ini', StringLower(BinaryToString($bUsername)), 'Password', $bPassword)
EndIf
ExitLoop 2
Else
$bTemp &= $tMid
EndIf
Next
Else
$bTemp &= $tMid
EndIf
Next
#EndRegion Register
EndIf
EndIf
#EndRegion Login Step One
ElseIf $Socket[$iSock][1] = Default Then
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]#Region Login Step Two
Global $bRecv = TCPRecv($Socket[$iSock][0], 512, True)
If $bRecv = 0x01 And BinaryLen($bRecv) = 1 Then
$Socket[$iSock][1] = True
$bAll &= Binary('0x06') & $Socket[$iSock][2] & Binary('0x00')
$bSend[$iSock] = Binary('')
For $iTemp = 0 To $iSocks Step True
If $Socket[$iTemp][0] <> -1 And $Socket[$iTemp][1] = True And $Socket[$iTemp][2] <> '' And $Socket[$iTemp][2] <> $Socket[$iSock][2] Then
$bSend[$iSock] &= Binary('0x06') & $Socket[$iTemp][2] & Binary('0x00')
EndIf
Next
EndIf
#EndRegion Login Step Two
EndIf
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]#Region Sender
Global $bNext = Binary('')
For $iTemp = 0 To $iSocks Step True
If $Socket[$iTemp][0] <> -1 And $Socket[$iTemp][1] = True Then
If $bAll <> '' Or $bSend[$iTemp] <> '' Then
Global $bTemp = $bSend[$iTemp] & $bAll, $bPacket = Binary('')
For $iPos = 1 To BinaryLen($bTemp) Step True
$bPacket &= Binary('0x' & Hex(BitXOR(BinaryMid($bTemp, $iPos, 1), $bKey[$iTemp]), 2))
Next
Global $iSend = TCPSend($Socket[$iTemp][0], $bPacket)
If $iSend <= 0 Then
$bNext &= Binary('0x07') & $Socket[$iTemp][2] & Binary('0x00')
TCPCloseSocket($Socket[$iTemp][0])
$Socket[$iTemp][0] = -1
$Socket[$iTemp][1] = False
$Socket[$iTemp][2] = Binary('')
EndIf
EndIf
EndIf
$bSend[$iTemp] = Binary('')
Next
Global $bAll = Binary('')
$bAll &= $bNext
#EndRegion Sender
EndIf
Global $bRecv = Binary('')
Next
[/autoit] [autoit][/autoit] [autoit]Sleep($iDelay)
WEnd
#EndRegion Main
Zugegeben, er ist nicht ganz so variabel... Aber wichtig war mir, das man wenigstens damit chatten kann!
Features:
-Registrieren
-All-Chat
-Private-Chat
Ein großer Nachteil ist, das ich noch keine Admin Benutzer o.ä. eingefügt habe, auch vom Server aus kann man niemanden kicken, da er schlichtweg unkontrollierbar ist
Um den Server Port und die IP einzurichten müsst ihr in beiden Scripten in der Region "Declaration", die Variablen "$Port" und "Addr" so ändern, wie ihr sie gerne haben möchtet! So wie die Codes oben parametriert sind, funktioniert es ausschließlich auf dem local host!
Viel spaß damit! Ich hoffe der Code ist lehrreich <.<
Mit freundlichen Grüßen,
Darknoop