Sign in to follow this  
Followers 0
platon0

improvement program, tcp...please

2 posts in this topic

hi everyone!!

just start the adventure with AutoIt and I have a request to you.

I found on this forum scripts to connect to a client - server, but it is strange with password protection.

I would like to get rid of it, but my knowledge as this moment does not allow me to do this.

could someone help me with this?

client

Global $MAX_SIZE=2*1048576
TCPStartup()
Global $pass=InputBox("Security", "Please select a password for connections","" , "*M")
If @error Then Exit
$file=FileOpenDialog("File to send" , ""  ,"All files(*.*)" , 1)
If @error Then Exit
$size=FileGetSize($file)
If $size=0 Then Exit MsgBox(16 , "ERROR" , "Bad file")
$name=_file_getname($file)
$ip=InputBox("Address" , "ip",@IPAddress1, " M")
$_sock=TCPConnect($ip,8081)
If @error Then Exit MsgBox(16,"Wsa:"&@error , "Unable to connect to the host!")
TCPSend($_sock,$pass)
ToolTip("Sending pass",0,40)
Do
$recv=TCPRecv($_sock,1000)
If @error Then Exit MsgBox(16 , "WSA:"&@error , "Lost connection!")
Sleep(1)
Until $recv<>""
If StringLeft($recv,2)<>"OK" Then Exit MsgBox(16 , "ERROR" , "Bad password:"&$recv)
$recv=StringTrimLeft($recv,2)
ToolTip("Authed, waiting" , 0 , 40)
If $recv="" Then
Do
  $recv=TCPRecv($_sock,1000)
  If @error Then Exit MsgBox(16 , "WSA:"&@error , "Lost connection!")
  Sleep(1)
Until $recv<>""
EndIf
If $recv<>"FILE_DATA" Then Exit MsgBox(16 , "Error" , "Weird!"&@CRLF&"got:'"&$recv&"' from the socket")
ToolTip("sending file data" , 0 , 40)
TCPSend($_sock,$name&":"&$size)
Do
$recv=TCPRecv($_sock,1000)
If @error Then Exit MsgBox(16 , "WSA:"&@error , "Lost connection!")
Sleep(1)
Until $recv<>""
If $recv<>"START_UL" Then Exit MsgBox(16 , "ERROR" , "weird!, got:"&$recv&" from the socket")
ToolTip("sending file!",0,40)
$return=False
$fhandle=FileOpen($file,16)
$bytes=0
While Not $return
$data=FileRead($fhandle,$MAX_SIZE)
If @error Then $return=True
$bytes+=TCPSend($_sock,$data)
ToolTip(Round($bytes*100/$size,2)&"%",0,40)
WEnd
FileClose($fhandle)

Func _file_getname($file)
$reg=StringRegExp($file,"(.)+\\((.)+)?",3)
If Not IsArray($reg) Then Return $file
If UBound($reg)<2 Then Exit MsgBox(16 , @error , "you can't send dirs!")
Return $reg[1]
EndFunc

server

TCPStartup()
Opt("TrayIconDebug" , 1)
Global $MAX_SIZE=4*1048576;1 meg
Global $TRUSTED=False
Global $SAVE_DIR=@ScriptDir&"\recv"
$_L_SOCK=TCPListen(@IPAddress1,8081)
If @error Then Exit MsgBox(16  , "WSA:"&@error , "Unable to hook port!")
Global $pass=InputBox("Security", "Please select a password for connections","" , "*M")
_listen()

Func _listen()
ToolTip("listening" , 0 , 0)
While 1
  $_tcp_acc=TCPAccept($_L_SOCK)
  If $_tcp_acc<>-1 Then
   _start_recv($_tcp_acc)
  EndIf
  Sleep(1)
WEnd
EndFunc
Func _start_recv($_sock)
While True
  $_recv=TCPRecv($_sock,$MAX_SIZE)
  If @error Then Return
  If $_recv<>"" Then
   If Not $TRUSTED Then
    If Not auth($_sock,$_recv) Then Return TCPCloseSocket($_sock)
   EndIf
  EndIf
  If $TRUSTED Then
   $file=_get_file_data($_sock)
   If @error Then
    $TRUSTED=False
    Return
   EndIf
   $location=FileSaveDialog("Save "&$file[0],$SAVE_DIR,"Files(*."&_get_ext($file[0])&")",16,$file[0])
   If @error Then
    $TRUSTED=False
    Return TCPCloseSocket($_sock)
   EndIf
   $fhandle=FileOpen($location,18)
   _start_download($_sock)
   Local $downloading=True,$bytes=0
   While $downloading
    $_data=TCPRecv($_sock,$MAX_SIZE,1)
    If @error Then
     FileClose($fhandle)
     FileDelete($location)
     $TRUSTED=False
     Return
    EndIf
    $bytes+=BinaryLen($_data)
    FileWrite($fhandle,$_data)
    If $bytes=$file[1] Then $downloading=False
    ToolTip(Round($bytes*100/$file[1],2)&"%",0,0)
   WEnd
   TCPSend($_sock,"GOT_IT")
   ToolTip("done!" , 0 , 0)
   FileClose($fhandle)
   TCPCloseSocket($_sock)
   $TRUSTED=False
  EndIf
  Sleep(1)
WEnd
EndFunc
Func auth($_sock,$_data)
If $_data<>$pass Then
  ToolTip("sender has sent bad pass!",0,0)
  TCPSend($_sock,"BAD_PASS")
  Return 0
Else
  TCPSend($_sock,"OK")
  ToolTip("sender has sent good pass!",0,0)
  $TRUSTED=True
  Return 1
EndIf
EndFunc
Func _get_ext($file)
$reg=StringRegExp($file,"(.)+\.((.)+)?",3)
If Not IsArray($reg) Then Exit MsgBox(16 , "Error" , "Bad file name: "&$file)
if UBound($reg)<2 Then Exit MsgBox(16 ,"Error" , "Bad file name: "&$file)
Return $reg[1]
EndFunc
Func _start_download($_sock)
TCPSend($_sock,"START_UL")
EndFunc
Func _get_file_data($_sock)
TCPSend($_sock,"FILE_DATA")
ToolTip("getting file data!" , 0 , 0)
Local $_recv="",$data[2]
Do
  $_recv=TCPRecv($_sock,1000)
  If @error Then Return SetError(2)
  Sleep(1)
Until $_recv<>""
$_data=StringSplit($_recv,":")
If $_data[0]<2 Then Return SetError(1)
$data[0]=$_data[1]
$data[1]=$_data[2]
ToolTip("")
Return $data
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Removing password authentication from a TCP socket connection is a very bad idea.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

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