Jump to content

TCP Port Mapping / Proxy server


 Share

Recommended Posts

Hi

I am new to this forum and Autoit (so be gentle with me :) ).

Case:

I want to write an application which will monitor(tcp listne) on ports 10000 to 10500. If any client request appears at any port (example: 10100) the application should start listning at port 20100(which is 10100 + 10000 =20100 ).

Then transfer any data comming in at port 10100 <--> 20100

so

10000 <--> 20000

10001 <--> 20001

10002 <--> 20002

10003 <--> 20003

'

'

'

'

10500 <-->2005

I have got so far. I have modified the Proxy example in the forum to make it do what i want.

TCPStartup()
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GUIEdit.au3>
#include <Array.au3>
Opt("OnExitFunc" , "quit")
Opt("GUIOnEventMode" , 1)
Opt("TrayIconDebug" , 1)

Global $DO_DEBUG=0, $forward, $ss, $sock, $socket
#region - GUI Create
;creates GUI console for the proxy server
GUICreate('',500,300)
;creates reporting area within the GUI window 
$DEBUG=GUICtrlCreateEdit("" , 0 , 0 , 800 , 600)
GUICtrlSetLimit(-1 , 2000000000000000)
GUISetOnEvent($GUI_EVENT_CLOSE,"quit")
GUISetState()

$socket=TCPListen(@IPAddress1,10000)
_GUICtrlEdit_AppendText($DEBUG , @IPAddress1&":10000"&@CRLF)
If @error Then Exit MsgBox(16 , "Error" , "Could not bind client 1")

;listne to second client
$socket2=TCPListen(@IPAddress1,20000)
_GUICtrlEdit_AppendText($DEBUG , @IPAddress1&":20000"&@CRLF)
If @error Then Exit MsgBox(16 , "Error" , "Could not bind client 2")

$sock=-1
$sock2=-1
$cl1connected=0
$cl2connected=0
while 1
    $msg=""
    
    ;_GUICtrlEdit_AppendText($DEBUG , "] Client1 or Client2  not Connected"&@CRLF)
    if $sock=-1 then
        $sock=TCPAccept($socket)
        $msg=$msg&"Waiting for Client 1 "&@CRLF     
    else
        $msg=$msg&"Client 1 Conected"&@CRLF
    endif   
    
    if $sock2=-1 then
        $sock2=TCPAccept($socket2)
        $msg=$msg&"Waiting for client 2 "&@CRLF     
    Else
        $recv2=TCPRecv($sock2,400000)
        _GUICtrlEdit_AppendText($DEBUG , $recv2&@CRLF)

        $msg=$msg&"Client 2 Conected"&@CRLF     
    endif   
    
    if $sock=-1 or $sock2=-1 Then
        _GUICtrlEdit_SetText($DEBUG , $msg&@CRLF)
    Else
        If $DO_DEBUG Then _GUICtrlEdit_AppendText($DEBUG , "] Client1 and client 2 Connected"&@CRLF)
        $recv=""
        $recv2=""
        Do
            $recv=TCPRecv($sock,400000)
            $error=@error

            $recv2=TCPRecv($sock2,400000)
            $error=@error
            
            $timeout=TimerInit()
            if $recv<>"" Then
                TCPSend($sock2 , $recv)
                $timeout=TimerInit(); got data reset timeout timer  
                _GUICtrlEdit_AppendText($DEBUG , "Client1:->"&$recv&@CRLF)              
            endif
            if $recv2<>"" Then
                TCPSend($sock , $recv2) 
                $timeout=TimerInit(); got data reset timeout timer          
                _GUICtrlEdit_AppendText($DEBUG , "Client2:->"&$recv2&@CRLF)                             
            endif           
            if $error Then
                TCPCloseSocket($sock)
                TCPCloseSocket($sock2)
                $sock=0
                $sock2=0
            endif   
        Until $error Or TimerDiff($timeout)>5000; data full or socket closed or timeout reached
    endif
    Sleep(100)  
WEnd    


;quit program
Func quit()
    TCPShutdown()
    Exit
EndFunc

There are few problems in the code.

1) It only monitors 1 port

2) if any client is disconneced the socket is still open

Any help will be appreciated

HP

Link to comment
Share on other sites

1. Rewrite it to monitor multiple ports

That is the question.

If already a data transfer is taking place in a Do loop how do I monitor other 500 ports without altering the speed of the existing connection?

Do
            $recv=TCPRecv($sock,400000)
            $error=@error

            $recv2=TCPRecv($sock2,400000)
            $error=@error
            
            $timeout=TimerInit()
            if $recv<>"" Then
                TCPSend($sock2 , $recv)
                $timeout=TimerInit(); got data reset timeout timer  
                _GUICtrlEdit_AppendText($DEBUG , "Client1:->"&$recv&@CRLF)              
            endif
            if $recv2<>"" Then
                TCPSend($sock , $recv2) 
                $timeout=TimerInit(); got data reset timeout timer          
                _GUICtrlEdit_AppendText($DEBUG , "Client2:->"&$recv2&@CRLF)                             
            endif           
            if $error Then
                TCPCloseSocket($sock)
                TCPCloseSocket($sock2)
                $sock=0
                $sock2=0
            endif   
        Until $error Or TimerDiff($timeout)>5000; data full or socket closed or timeout reached

2. Close the socket when a client disconnects

If its not too much trouble, a Sample snippet would be great.

Please write a question to make both of our lifes easier.

I asumed that my "Problem" will be considered as a question.

My bad.

Thankz for you reply

Link to comment
Share on other sites

If already a data transfer is taking place in a Do loop how do I monitor other 500 ports without altering the speed of the existing connection?

That's really impossible. You have to stop doing data transfers in loops, and make the come from a backbuffer (variable). I like to think I am good enough at things like these to share scripts about this subject, that's why I have a HTTP server that does exactly this: http://www.autoitscript.com/forum/index.php?showtopic=68851

If its not too much trouble, a Sample snippet would be great.

When a client disconnects, a @error value will be set after calling TCPRecv or TCPSend. The socket is actually already disconnected, but the problem is in your script which doesn't handle it correctly. This means that you don't have to call TCPCloseSocket. If you follow the example posted above, I keep a list of connected clients and simply remove the client from the list on disconnect.

Sorry for being so up front the first time. A problem can be tackled in so many ways, with so many interpretations.. If you have more problems, please come back.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...