Jump to content
Sign in to follow this  
Zibit

TCP functions help

Recommended Posts

Zibit

Hi!

Could you help me with my script? it is not working properly.

Defenition of my script:

its a packet logger.

packet scheme clients <-> logger <-> server

but the logger isnt connecting to the server.

#include <Array.au3>
#include <File.au3>
#include <TCP.au3>

Func InitLogger()
Global $CLIENTS[99][9999]
Global Const $SERVER_IP = IniRead("KOPPSettings.ini", "Network", "SERVER_IP", "127.0.0.1")
Global Const $SERVER_PORT = IniRead("KOPPSettings.ini", "Network", "SERVER_PORT", 30001)
Global Const $CONTROLLER_IP = IniRead("KOPPSettings.ini", "Network", "CONTROLLER_IP", "127.0.0.1")
Global Const $CONTROLLER_PORT = IniRead("KOPPSettings.ini", "Network", "CONTROLLER_PORT", 12000)
Global Const $PACKET_TIME = IniRead("KOPPSettings.ini", "Packets", "PACKET_TIME_DIFF", "300")
Global Const $MAX_SPEEDPACKETS = IniRead("KOPPSettings.ini", "Packets", "MAX_SPEEDPACKETS", "50")
Global Const $NEWCLIENT_SLEEP = IniRead("KOPPSettings.ini", "Packets", "NEWCLIENT_SLEEP", "500")
Global $SOCKETS[9999]
Global $GUI = False
$hServer = _TCP_Server_Create($CONTROLLER_PORT, $CONTROLLER_IP)
_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient")
_TCP_RegisterEvent($hServer, $TCP_RECEIVE, "Recive")
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Discon")
ReloadBa
ns()
GUI()
SetText("Running Controller: " & $hServer & @CRLF & "IP: " & $CONTROLLER_IP & @CRLF & "Port: " & $CONTROLLER_PORT)
EndFunc


Func NewClient($socket, $error)
#cs
$CLIENTS[x][0] = Client -> controller socket
$CLIENTS[x][1] = controller -> Server socket
$CLIENTS[x][2] = Client IP
$CLIENTS[x][3] = Last Packet
$CLIENTS[x][4] = Last Packet Time
$CLIENTS[x][5] = Amount of OverSpeeded Packets
ClientRecive = Data sent: Server -> controller -> client
ClientDiscon = Server x controller -> client
#ce
if Banned(_TCP_Server_ClientIP($socket)) = True Then
SetText("(Blocked)New Client: " & _TCP_Server_ClientIP($socket))
_TCP_Server_DisconnectClient($socket)
Return False
EndIf
SetText("New Client: " & _TCP_Server_ClientIP($socket))
$SOCKETS[0] = $SOCKETS[0] + 1
$SOCKETS[$SOCKETS[0]] = $socket
$CLIENTS[0][0] = $CLIENTS[0][0] + 1
$CLIENTS[$CLIENTS[0][0]][0] = $socket
$CLIENTS[$CLIENTS[0][0]][1] = _TCP_Client_Create($SERVER_IP, $SERVER_PORT)
$CLIENTS[$CLIENTS[0][0]][2] = _TCP_Server_ClientIP($socket)
$CLIENTS[$CLIENTS[0][0]][3] = ""
$CLIENTS[$CLIENTS[0][0]][4] = TimerInit()
$CLIENTS[$CLIENTS[0][0]][5] = 0
_TCP_RegisterEvent($CLIENTS[$CLIENTS[0][0]][1], $TCP_RECEIVE, "ClientRecive")
_TCP_RegisterEvent($CLIENTS[$CLIENTS[0][0]][1], $TCP_DISCONNECT, "ClientDiscon")
_TCP_RegisterEvent($CLIENTS[$CLIENTS[0][0]][1], $TCP_CONNECT, "ClientCon")
SetText("Creating New Client: " & $CLIENTS[$CLIENTS[0][0]][1])
Sleep($NEWCLIENT_SLEEP)
EndFunc

Func ClientCon($socket, $error)
if $error Then
_TCP_Server_DisconnectClient($socket)
Else
$CLIENTS[$CLIENTS[0][0]][1] = $socket
EndIf
ConsoleWrite($CLIENTS[$CLIENTS[0][0]][1] & @CRLF)
EndFunc

Func Discon($socket, $error)
$a = _ArraySearch($SOCKETS, $socket)
if $a <> -1 Then
SetText("Client Disconnected: " & $CLIENTS[$a][2])
_TCP_Server_DisconnectClient($CLIENTS[$a][1])
$SOCKETS[$a] = ""
$CLIENTS[$a][0] = ""
$CLIENTS[$a][1] = ""
$CLIENTS[$a][2] = ""
EndIf
EndFunc

Func ClientDiscon($socket, $error)
$a = _ArraySearch($SOCKETS, $socket)
if $a <> -1 Then
_TCP_Server_DisconnectClient($CLIENTS[$a][0])
$SOCKETS[$a] = ""
$CLIENTS[$a][0] = ""
$CLIENTS[$a][1] = ""
$CLIENTS[$a][2] = ""
EndIf
EndFunc

Func Recive($socket, $data, $error)
$a = _ArraySearch($SOCKETS, $socket)
if $a <> -1 Then
FileWriteLine("PacketLogger.log", "[Server->Client]"&$CLIENTS[$a][2]&": "&$data)
if $GUI = True Then SetText("/Server->Client\ "&$data)
_TCP_Send($CLIENTS[$a][0], $data)
EndIf
EndFunc

Func ClientRecive($socket, $data, $error)
;Recive From Client -> Controller
if StringStripWS($data, 8) = "" Then Return False
$a = _ArraySearch($SOCKETS, $socket)
if $a <> -1 Then
FileWriteLine("PacketLogger.log", "[Client->Server]"&$CLIENTS[$a][2]&": "&$data)
if packet($data, $socket) = True then 
_TCP_Send($CLIENTS[$a][1], $data)
if $GUI = True Then SetText("/Client->Server\ "&$data)
Else
if $GUI = True Then SetText("/Client->Server\ Blocked Packet: "&$data)
EndIf
EndIf
EndFunc

Func GUI()
if $GUI = False Then
Global $gui = GUICreate("Log", 200, 300)
Global $edit = GUICtrlCreateEdit("", 10, 10, 180, 280, 0x0800)
GUISetState()
$GUI = True
Else
$GUI = False
if IsDeclared("edit") Then GUICtrlDelete($edit)
GUIDelete()
EndIf
SetText("Welcome to Maakera's Packet Logger!")
EndFunc

Func SetText($line)
if $GUI = True Then
$read = GUICtrlRead($edit)
GUICtrlSetData($edit, "["&@HOUR&":"&@MIN&":"&@SEC&"] "&@CRLF&String($line)&@CRLF&"---------------------------------------------------------"&$read&@CRLF)
EndIf
EndFunc

Func Packet($packet, $socket)
$a = _ArraySearch($SOCKETS, $socket)
if $a <> -1 Then
if $CLIENTS[$a][3] = $packet Then
if TimerDiff($CLIENTS[$a][4]) < $PACKET_TIME Then 
$CLIENTS[$a][5] = $CLIENTS[$a][5] + 1
if $CLIENTS[$a][5] >= $MAX_SPEEDPACKETS Then 
SetText("Banned IP: " & $CLIENTS[$a][2])
FileWriteLine("Bans.tsf", $CLIENTS[$a][2])
_TCP_Server_DisconnectClient($CLIENTS[$a][0])
ReloadBans()
EndIf
Return False
EndIf
EndIf
$CLIENTS[$a][3] = $packet
$CLIENTS[$a][4] = TimerInit()
Return True
EndIf
Return False
EndFunc

Func Banned($ip)
$a = _ArraySearch($BANNED, $ip)
if $a <> -1 Then
Return True
EndIf
Return False
EndFunc

Func ReloadBans()
Global $BANNED[9999]
$BANNED[0] = 0
if FileExists("Bans.tsf") Then
$count = _FileCountLines("Bans.tsf")
$i = 0
Do
$i = $i + 1
$BANNED[0] = $BANNED[0] + 1
$BANNED[$i] = FileReadLine("Bans.tsf", $i)
until $i >= $count
EndIf
EndFunc

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  

×