Jump to content
Tuts 4 You

[PureBasic] Reverse TCP Proxy Server HELP!


0xNOP

Recommended Posts

Posted (edited)

@Teddy Rogers (tagging Teddy since I know he has used PureBasic in the past, in fact, thanks to him, I got fevered to learn and code in this nice language.)

 

Hello everyone, I have been wanting to do a proxy server for a game I have, and so far I've only gotten to work, but it's very broken, I know I'm doing a horrible job handling memory buffers and writing/reading to/from Client to Server and vice-versa, but I have gotten this far on my own, now I need help from you guys as I can't make it work, and I don't know how I could have a better approach at reading/sending data to and from the client and server and vice-versa... I know there could be better ways but I'm just stuck...

OpenConsole()
ConsoleTitle ("----- Proxy Server")


If InitNetwork() = 0
  PrintN("Can't initialize the network !")
  End
EndIf

LSPort = 5999
*LocalBuffer = AllocateMemory(4096) ; I think this should be just enough
*RemoteBuffer = AllocateMemory(4096); I think this should be just enough

Procedure SendData(SocketID, *Buffer)
  bytes_sent = SendNetworkData(SocketID, *Buffer, SizeOf(*Buffer))
  If bytes_sent <= 0
    ; no more data (or connection error if -1)
  Else
    ; process extracted data
    PrintN(Str(Buffer))
  EndIf
EndProcedure

Procedure.i ConnectToRealServer()
  ConnectionID = OpenNetworkConnection("192.243.47.138", 5999)
  ret.i
  
  If ConnectionID
    PrintN("Connected to Real Server!")
    ret = ConnectionID
  Else
    ret = -1
  EndIf
  
  ProcedureReturn ret
EndProcedure

RealServerID.i

If CreateNetworkServer(0, LSPort)
  
  PrintN("Server created (Port "+Str(LSPort)+").")
  
  Repeat
    
    SEvent = NetworkServerEvent()
    
    If SEvent
      
      ClientID = EventClient()
      
      Select SEvent
          
        Case #PB_NetworkEvent_Connect
          PrintN("A client has connected !")
          RealServerID = ConnectToRealServer()
          
        Case #PB_NetworkEvent_Data
          
          PrintN("Local Client "+Str(ClientID)+" has sent us a packet!")
          Repeat
            bytes_read = ReceiveNetworkData (ClientID, *RemoteBuffer, SizeOf(*RemoteBuffer))
            If bytes_read <= 0
              ; no more data (or connection error if -1)
            Else
              ; process extracted data
              SendData(RealServerID, *RemoteBuffer)
              Text$ = PeekS(*RemoteBuffer, SizeOf(*RemoteBuffer), #PB_Ascii)
              PrintN("[C]->[S]: " + Text$)
            EndIf
          ForEver
          
          Repeat
            bytes_read = ReceiveNetworkData (RealServerID, *LocalBuffer, SizeOf(*LocalBuffer))
            If bytes_read <= 0
              ; no more data (or connection error if -1)
            Else
              ; process extracted data
              SendData(ClientID, *LocalBuffer)
            EndIf
          ForEver
          
          
        Case #PB_NetworkEvent_Disconnect
          PrintN("Client "+Str(ClientID)+" has closed the connection...")
          Quit = 1
          
      EndSelect
    EndIf
    
  ForEver
  
  CloseNetworkServer(0)
Else
  PrintN("Can't create the server (port in use ?).")
EndIf

 

Edited by 0xNOP

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
×
×
  • Create New...