Verschlüsselter Chat

  • jaja, das gibts schon, aber ich habs trotzdem mal gemacht

    Erstmal: das ist (wenn man von diversen auto-login tools absieht (warcraft 3, wow), mein erstes richtiges programm in autoit! Um mir den spaß net zu verderben, habe ich nicht in anderen hier im board verfügbaren Quellcodes geguckt, sondern von vorn herein nur mit hilfe des tutorials und der F1 hilfe von Scite das Programm geschrieben!

    Könnt ihr vielleicht mal drübergucken, ob ihr bugs findet, oder etwas wirklich effektiver gestalten könnt?


    Beim Server stellt ihr die Einstellungen wie Port und Passwort im code ein, der client fragt beim ersten start danach!
    mit /name ändert ihr euren namen im chat, default ist euer windows benutzername!

    Es wird obj_Dictonary.au3 gebraucht, das findet ihr hier: https://autoit.de/index.php?page…30174#post30174


    Optionen Menü ist in arbeit :)

    Server:

    Spoiler anzeigen
    [autoit]

    #cs
    ~~
    Encrypted Chat Tool, made by Nimos
    ~~
    ICQ: 693~8~141
    http://www.nimos.6x.to
    ~~
    #ce

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; Includes:
    #include <GUIConstants.au3>
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Include <GuiEdit.au3>
    #include <ScrollBarConstants.au3>
    #include <String.au3>
    #include <StaticConstants.au3>
    #Include <Array.au3>
    #include <objDictonary.au3>
    ;~

    [/autoit] [autoit][/autoit] [autoit]

    ; Variablen
    Global $msg
    Global $recv
    Global $names = _objDictCreate("text")
    ;~

    [/autoit] [autoit][/autoit] [autoit]

    ; Konfiguration
    Global $password = "test"
    Global $nPORT = 2000 ; Der Serverport über den gesendet wird
    ;~

    [/autoit] [autoit][/autoit] [autoit]

    _Main()

    [/autoit] [autoit][/autoit] [autoit]

    Func _Main()

    [/autoit] [autoit][/autoit] [autoit]

    ; GUI Erstellung
    #Region ### START Koda GUI section ### Form=c:\dokumente und einstellungen\nimos\eigene dateien\lcoh200\form1.kxf
    $Form1_1 = GUICreate("Nimos Chat Server, Running at " & @IPAddress1, 427, 290, 222, 97)
    GUISetIcon("C:\Programme\BOINC\BOINCGUIApp.ico")
    Global $edit = GUICtrlCreateEdit("", 16, 16, 385, 217, $ES_READONLY)
    GUICtrlSetData(-1, "")
    GUICtrlSetTip(-1, "Dies is das Output Fenster")
    $input = GUICtrlCreateInput("", 16, 253, 321, 21)
    $button = GUICtrlCreateButton("Senden", 344, 248, 75, 33, 0)
    GUICtrlSetBkColor(-1, 0xFFFF00)
    GUISetState(@SW_SHOW)
    Dim $Form1_1_AccelTable[1][2] = [["{ENTER}", $button]]
    GUISetAccelerators($Form1_1_AccelTable)
    GUICtrlSetData($edit, "Warte auf Teilnehmer... " & @CRLF & GUICtrlRead($edit))
    #EndRegion ### END Koda GUI section ###
    ;~

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    TCPStartUp()
    $MainSocket = TCPListen( @IPAddress1, $nPORT)
    If $MainSocket = -1 Then Exit
    ; Aufbauen der Verbindung, Exit wenn Fehler
    Dim $ConnectedSocket[1]
    While 1

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $newsock = TCPAccept($MainSocket)
    For $number In $ConnectedSocket
    If $newsock = $number Then
    $newsock = -1
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ; Wenn ein neuer Client connected, prüfen, ob er schon in der Verbindungsliste ist
    ; und wenn nicht, dann

    [/autoit] [autoit][/autoit] [autoit]

    If $newsock <> -1 Then
    $send_text = "<> Console <> Eingehende Verbindung von " & SocketToIP($newsock) & "."
    SoundPlay(@WindowsDir & "\media\chimes.wav",0)
    _ArrayAdd($ConnectedSocket, $newsock)
    For $user In $ConnectedSocket
    TCPSend($user, _StringEncrypt(1, $send_text, $password))
    Next
    _Output($send_text)
    $newsock = -1

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ; Hinzufügen des neuen Clients ins Array $ConnectedSocket und allen Clients die Nachricht schreiben,
    ; das eine neue Eingehende Verbindung entstanden ist.

    [/autoit] [autoit][/autoit] [autoit]

    $nMsg = GUIGetMsg() ; Daten von der GUI abfragen...

    [/autoit] [autoit][/autoit] [autoit]

    Switch $nMsg

    [/autoit] [autoit][/autoit] [autoit]

    Case $GUI_EVENT_CLOSE ; Was passiert wenn "Schließen" gedrückt wird
    Exit
    Case $button ; Was passiert beim drücken des "Senden" Buttons

    [/autoit] [autoit][/autoit] [autoit]

    $send_text = "<> Console <> " & GUICtrlRead($input) ; Timestamp hinzufügen
    GUICtrlSetData($input, "") ; Input Feld clearen

    [/autoit] [autoit][/autoit] [autoit]

    For $user In $ConnectedSocket
    TCPSend($user, _StringEncrypt(1, $send_text, $password)) ; Verschlüsselten Text abschicken
    Next

    [/autoit] [autoit][/autoit] [autoit]

    _Output($send_text)
    _GUICtrlEdit_Scroll($edit, $SB_PAGEDOWN) ; Text in Output Feld eintragen und runterscrollen

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    EndSwitch

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    For $user In $ConnectedSocket

    [/autoit] [autoit][/autoit] [autoit]

    $recv = TCPRecv( $user, 2048 ) ; Nach einkommenden Nachrichten für jeden verbundenen Client scannen

    [/autoit] [autoit][/autoit] [autoit]

    If $recv <> "" Then ; Wenn es eine Nachricht gibt...

    [/autoit] [autoit][/autoit] [autoit]

    $recv_decrypted = _StringEncrypt(0, $recv, $password) ; ...wird sie entschlüsselt...
    $recv_split = StringSplit($recv_decrypted, "") ; ...in einzelne Buchstaben unterteilt...

    [/autoit] [autoit][/autoit] [autoit]

    If $recv_split[1] = "/" Then ; ...und geprüft ob es ein /text Kommando ist!
    $recv_split = StringSplit($recv_decrypted, " ") ; Dann wird es in einzelne Worte aufgeteilt

    [/autoit] [autoit][/autoit] [autoit]

    Switch $recv_split[1] ; Und Überprüft welches Kommando es ist.
    Case "/name" ; /name Kommando
    If _ObjDictGetValue($names, $user) = -1 Then ; Gibt es bereits einen Namen für den Socket?
    _ObjDictAdd($names, $user, $recv_split[2]) ; Nein? Dann lege einen an und...

    [/autoit] [autoit][/autoit] [autoit]

    $send_text = _ObjDictGetValue($names, $user) & " hat den Channel betreten!"
    ; ...und sende diesen an alle Clients!

    [/autoit] [autoit][/autoit] [autoit]

    For $user In $ConnectedSocket
    TCPSend($user, _StringEncrypt(1, $send_text, $password))
    Next

    [/autoit] [autoit][/autoit] [autoit]

    _Output($send_text)

    [/autoit] [autoit][/autoit] [autoit]

    Else ; Es gibt bereits einen Namen...
    $oldname = _ObjDictGetValue($names, $user)

    [/autoit] [autoit][/autoit] [autoit]

    _ObjDictSetValue($names, $user, $recv_split[2])

    [/autoit] [autoit][/autoit] [autoit]

    $send_text = $oldname & " ändert seinen Namen in " & _ObjDictGetValue($names, $user) & "."
    For $user In $ConnectedSocket
    TCPSend($user, _StringEncrypt(1, $send_text, $password))
    Next
    _Output($send_text)

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    Else

    [/autoit] [autoit][/autoit] [autoit]

    $name = _ObjDictGetValue($names, $user)
    If $name <> -1 Then
    $send_text = $name & ": " & $recv_decrypted
    _Output($send_text)

    [/autoit] [autoit][/autoit] [autoit]

    For $user In $ConnectedSocket
    TCPSend($user, _StringEncrypt(1, $send_text, $password))
    Next
    EndIf
    EndIf
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If @error Then
    If $user > 0 Then
    $name = _ObjDictGetValue($names, $user)
    $send_text = $name & " hat die Verbindung beendet!"
    _Output($send_text)

    [/autoit] [autoit][/autoit] [autoit]

    For $user In $ConnectedSocket
    TCPSend($user, _StringEncrypt(1, $send_text, $password))
    Next

    [/autoit] [autoit][/autoit] [autoit]

    _ArrayDelete($ConnectedSocket, _ArraySearch($ConnectedSocket, $user))
    EndIf
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    WEnd
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    [/autoit] [autoit][/autoit] [autoit]

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    [/autoit] [autoit][/autoit] [autoit]

    $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

    [/autoit] [autoit][/autoit] [autoit]

    $sockaddr = 0

    [/autoit] [autoit][/autoit] [autoit]

    Return $aRet
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Output($message)

    [/autoit] [autoit][/autoit] [autoit]

    $send_text = "[" & @HOUR & ":" & @MIN & "] " & $message

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlSetData($edit,GUICtrlRead($edit) & $send_text& @CRLF )
    _GUICtrlEdit_Scroll($edit, $SB_PAGEDOWN)
    EndFunc

    [/autoit]

    Client

    Spoiler anzeigen
    [autoit]

    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_icon=..\..\..\..\Programme\BOINC\BOINCGUIApp.ico
    #AutoIt3Wrapper_Compression=3
    #AutoIt3Wrapper_Res_Comment=My Thingy
    #AutoIt3Wrapper_Res_Description=ICQ: 693-8-141
    #AutoIt3Wrapper_Res_LegalCopyright=Nimos
    #AutoIt3Wrapper_Res_Language=1031
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    #include <GUIConstants.au3>
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Include <GuiEdit.au3>
    #include <ScrollBarConstants.au3>
    #include <String.au3>
    #include <StaticConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $server = IniRead("settings.ini", "connection", "server", -1)

    [/autoit] [autoit][/autoit] [autoit]

    If $server = -1 Then
    $server = InputBox("Server angeben!", "Dies ist der erste Start, geben sie den Server an!", "SERVER:PORT", " M")
    IniWrite("settings.ini", "connection", "server", $server)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $username = IniRead("settings.ini", "misc", "username", @UserName)

    [/autoit] [autoit][/autoit] [autoit]

    $password = IniRead("settings.ini", "connection", "password", -1)

    [/autoit] [autoit][/autoit] [autoit]

    If $password = -1 Then
    $password = InputBox("Passwort angeben!", "Jetzt geben sie bitte das Passwort an!", "", " M")
    IniWrite("settings.ini", "connection", "passwort", $password)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    TCPStartUp()
    Dim $ConnectedSocket = -1
    $server_split = StringSplit($server, ":")
    Dim $IPADDRESS = TCPNameToIP($server_split[1])
    Dim $PORT = $server_split[2]

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #Region ### START Koda GUI section ### Form=c:\dokumente und einstellungen\nimos\eigene dateien\lcoh200\form1.kxf
    $Form1_1 = GUICreate("Nimos Chat Client", 427, 305, 222, 97)
    $edit = GUICtrlCreateEdit("", 16, 16, 385, 217, $ES_READONLY)
    GUICtrlSetData(-1, "")
    GUICtrlSetTip(-1, "Dies is das Output Fenster")
    $input = GUICtrlCreateInput("", 16, 253, 321, 21)
    $button = GUICtrlCreateButton("Senden", 344, 248, 75, 33, 0)
    GUICtrlSetBkColor(-1, 0xFFFF00)
    GUISetState(@SW_SHOW)
    Dim $Form1_1_AccelTable[1][2] = [["{ENTER}", $button]]
    GUISetAccelerators($Form1_1_AccelTable)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    $MenuDatei = GUICtrlCreateMenu("Datei")
    $MenuReconnect = GUICtrlCreateMenuItem("Reconnect", $MenuDatei)
    $MenuBeenden = GUICtrlCreateMenuItem("Beenden", $MenuDatei)
    $MenuExtras = GUICtrlCreateMenu("Extras")
    $MenuOptionen = GUICtrlCreateMenuItem("Optionen", $MenuExtras)

    [/autoit] [autoit][/autoit] [autoit]

    _Output("Willkommen bei Nimos Chat Client!")
    _Output("Baue sichere Verbindung zu " & $IPADDRESS & ":" & $PORT & " auf...")
    Do
    $ConnectedSocket = TCPConnect($IPADDRESS,$PORT)
    If @error Then
    Sleep(5000)
    _Output("Der Host " & $server & " reagiert nicht, Neuer Versuch!")
    EndIf
    Until $ConnectedSocket <> -1
    _Output("Verbindungsaufbau Erfolgreich!" & @CRLF)
    Dim $szData

    [/autoit] [autoit][/autoit] [autoit]


    TCPSend($ConnectedSocket, _StringEncrypt(1, "/name " & $username, $password))
    $begin = TimerInit()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $MenuOptionen
    MsgBox(0, "Error", "Dieses Feature ist noch nicht verfügbar!")
    Case $MenuReconnect
    MsgBox(0, "Error", "Dieses Feature ist noch nicht verfügbar!")
    Case $MenuBeenden
    MsgBox(0, "Error", "Dieses Feature ist noch nicht verfügbar!")
    Case $GUI_EVENT_CLOSE
    Exit
    Case $button
    If GUICtrlRead ($input) <> "" Then
    If TimerDiff($begin) > 500 Then

    $send_text = _StringEncrypt(1, GUICtrlRead($input), $password)
    TCPSend($ConnectedSocket, $send_text)

    If @error Then ExitLoop

    GUICtrlSetData($input, "")
    $begin = TimerInit()
    EndIf
    EndIf
    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    $recv = TCPRecv( $ConnectedSocket, 2048 )
    _GUICtrlEdit_Scroll($edit, $SB_PAGEDOWN)
    If $recv <> "" Then
    _GUICtrlEdit_Scroll($edit, $SB_PAGEDOWN)
    $recv = "[" & @HOUR & ":" & @MIN & "] " & _StringEncrypt(0, $recv, $password)
    SoundPlay(@WindowsDir & "\media\chimes.wav",0)
    If WinGetTitle("[active]") <> "Nimos Chat Client" Then
    TrayTip( "", "", 10, 16)
    TrayTip ( "Neue Nachricht", $recv, 10, 16 )
    EndIf
    GUICtrlSetData($edit, GUICtrlRead($edit) & $recv & @CRLF)
    _GUICtrlEdit_Scroll($edit, $SB_PAGEDOWN)
    _GUICtrlEdit_Scroll($edit, $SB_PAGEDOWN)
    EndIf
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    ;~ Func _Optionen()
    ;~ #include <ButtonConstants.au3>
    ;~ #include <EditConstants.au3>
    ;~ #include <GUIConstantsEx.au3>
    ;~ #include <StaticConstants.au3>
    ;~ #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    ;~ #Region ### START Koda GUI section ### Form=
    ;~ $Form1 = GUICreate("Optionen - Nimos Chat Client", 459, 284, 255, 131)
    ;~ $Group1 = GUICtrlCreateGroup(" Verbindung ", 24, 32, 409, 97)
    ;~ $Input1 = GUICtrlCreateInput("Input1", 40, 88, 129, 21)
    ;~ $Input2 = GUICtrlCreateInput("Input2", 240, 88, 129, 21)
    ;~ $Label1 = GUICtrlCreateLabel("Server: ", 32, 64, 41, 17)
    ;~ $Label2 = GUICtrlCreateLabel("Passwort ( Muss bei allen gleich sein! )", 232, 64, 185, 17)
    ;~ GUICtrlCreateGroup("", -99, -99, 1, 1)
    ;~ $Group2 = GUICtrlCreateGroup(" Sonstiges ", 24, 160, 409, 73)
    ;~ $Input3 = GUICtrlCreateInput("Input3", 40, 200, 121, 21)
    ;~ $Label3 = GUICtrlCreateLabel("Name:", 32, 184, 35, 17)
    ;~ GUICtrlCreateGroup("", -99, -99, 1, 1)
    ;~ $Button1 = GUICtrlCreateButton("OK", 360, 256, 73, 17, 0)
    ;~ $Button2 = GUICtrlCreateButton("Abbrechen", 272, 256, 73, 17, 0)
    ;~ GUISetState(@SW_SHOW)
    ;~ #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    ;~ While 1
    ;~ $nMsg = GUIGetMsg()
    ;~ Switch $nMsg
    ;~ Case $GUI_EVENT_CLOSE
    ;~ Exit

    [/autoit] [autoit][/autoit] [autoit]

    ;~ EndSwitch
    ;~ WEnd
    ;~ Return 1
    ; EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Output($message)

    $send_text = "[" & @HOUR & ":" & @MIN & "] " & $message

    GUICtrlSetData($edit,GUICtrlRead($edit) & $send_text & @CRLF )
    _GUICtrlEdit_Scroll($edit, $SB_PAGEDOWN)
    EndFunc

    [/autoit]