Crée un proxy server SOCKS v 5,4,4a multi os

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

Crée un proxy server SOCKS v 5,4,4a multi os

Message par celtic88 »

bonjour les pures :),
bon c'est quoi un serveur proxy SOCKS ? : https://fr.wikipedia.org/wiki/SOCKS

ça fait un ans que j'ai crée ce code pour sécurisé mes connections vers internet et surfer en mode anonyme sur le net 8) ....

je partage le code pour peut être que quelqu'un trouve ca outil ou pour le développer plus :wink: .

le proxy support tous les versions "SOCKS v 5,4,4a", et c'est très simple a utiliser :)


code :

Code : Tout sélectionner

; By Celtic88 (c) 2016
;about Proxy Socks ====> https://en.wikipedia.org/wiki/SOCKS
;Work on Version pb demo ;)

Structure TSocks5Request;Socks 5 Request;
  Version.a             ;SOCKS version, 1 byte (0x05 for this version)
  Command.a             ; command code, 1 byte://0x01 = establish a TCP/IP stream connection//0x02 = establish a TCP/IP port binding//0x03 = associate a UDP port
  reserved.a            ;reserved
  addresstype.a         ;address type, 1 byte://0x01 = IPv4 address//0x03 = Domain name//0x04 = IPv6 address
  IpAndPort.a[#MAX_PATH+2];Request Ip and port in max size
EndStructure              ;

Structure TSocks5authenticationRequest;;Socks 5 authentication Request
  Version.a                           ;version number, 1 byte (must be 0x01)
  AuthenticationLoginAndPass.a[(#MAX_PATH*2)+2] ;Request authentication login and password in max size
EndStructure

Structure TSocks5Methods;Socks 5 Methods
  Version.a             ;SOCKS version, 1 byte (0x05 for this version)
  Methods.a             ;authentication methods supported are numbered as follows://0x00: No authentication//0x01: GSSAPI[13]//0x02: Username/password[14]//0x03–0x7F: methods assigned by IANA[15]//0x80–0xFE: methods reserved For private use
  lenMethods.a          ;authentication methods, variable length, 1 byte per method supported
EndStructure

Structure TSocks4Request
  Version.a;SOCKS version number, 1 byte, must be 0x04 for this version
  Command.a;command code, 1 byte://0x01 = establish a TCP/IP stream connection//0x02 = establish a TCP/IP port binding
  IpAndPort.a[6];Request Ip and port 
  sInfo.a       ;the user ID string
EndStructure

;________________________________________________________________________________________________________________________
; Name...........: Proxy_Socks_4_4a_5
; Description ...: Get Client Request and connect to Remote server
; Parameters ....: LocalConnectionID - Client connection ID
;                                *ProxySocksMemWork : Point to Client Request
;                                SocksUser : Proxy username
;                                ProxySocksPassword  : Proxy Password
;                                Socks5NegotiationLevel : The level of authentication  , code return:
;                                                   -1 : accepted authentication methods
;                                                   -2 : authentication Successfully, Start the connection

; Return values .: Success - Remote server connection id or Socks5NegotiationLevel
;                  Failure - Returns 0
; Author ........: Celtic88
;_________________________________________________________________________________________________________________________


Procedure Proxy_Socks_4_4a_5(LocalConnectionID.i,*ProxySocksMemWork,SocksUser.s,ProxySocksPassword.s,Socks5NegotiationLevel.a)
  Protected IP.s ="",Port.u=0,UserName.s,Password.s,IsSocks_4A.b=0,RemoteConnectionID.i=0,*Version.Ascii,LenUser.l=0,LenPass.l=0,LenIP.l=0,S5FixSize.l=OffsetOf(TSocks5Request\IpAndPort)
  Protected *TSocks4Request.TSocks4Request,*TSocks5Methods.TSocks5Methods,*TSocks5Request.TSocks5Request,*TSocks5authenticationRequest.TSocks5authenticationRequest
  
  *Version = *ProxySocksMemWork
  
  If *Version\a = $04 ; is a proxy Socks 4x
    *TSocks4Request=*ProxySocksMemWork
    With *TSocks4Request
      
      Select \Command ; get command
        Case $01      ;establish a TCP/IP stream connection
                      ;Ok
        Case $02      ; 0x02 = establish a TCP/IP port binding :::::: currently not supported
      EndSelect
      
      Port = (\IpAndPort[0]) * 256 ;Get Port
      Port + (\IpAndPort[1])     
      IP = Str(\IpAndPort[2]) + "." + Str(\IpAndPort[3]  ) + "." ;Get Ip
      IP + Str(\IpAndPort[4]) + "." 
      If IP ="0.0.0." And (\IpAndPort[5]  ) <> $00:IsSocks_4A = 1:EndIf ; If first three bytes of DSTIP is "0" and the last byte to a non-zero value ::: is a proxy Socks 4A
      IP + Str(\IpAndPort[5]  ) 
      
      UserName = PeekS(@\sInfo,-1,#PB_UTF8 );Peek user name
      If IsSocks_4A
        IP=""
        IP = PeekS(@\sInfo+1+Len(UserName),-1,#PB_UTF8 ) ; if is a proxy Socks 4A get the ip after NULL byte terminating USER ID
      EndIf
      
      \Version=0 ;null byte
      If SocksUser = "" Or SocksUser =UserName ; check username
        If IP <> "" And \Command = $01         ;  check if all is ok
          \Command = $5A                       ; 0x5A = request granted
          RemoteConnectionID=OpenNetworkConnection(IP,Port); Connect to remote server
        EndIf
        If Not RemoteConnectionID
          \Command = $5b; if not connected send 0x5B = request rejected or failed
        EndIf
      Else
        \Command = $5d; request failed because client's identd could not confirm the user ID string in the request
      EndIf
      
      SendNetworkData(LocalConnectionID, *TSocks4Request,8); send Request
    EndWith
    
  ElseIf *Version\a= $05 Or Socks5NegotiationLevel =-1 ; else if is a proxy Socks 5 or Socks5NegotiationLevel is -2
    If Socks5NegotiationLevel =0
      ;Get Connection method
      *TSocks5Methods = *ProxySocksMemWork
      If *TSocks5Methods\Methods <> $02 Or *TSocks5Methods\lenMethods <> $02:*TSocks5Methods\Methods =$FF:EndIf; support only 0x02: Username/password
      SendNetworkData(LocalConnectionID, *TSocks5Methods,2)
      If *TSocks5Methods\Methods = $02 :ProcedureReturn -1 :EndIf ; return next level Negotiation
      
    ElseIf Socks5NegotiationLevel =-1
      ;Get authentication
      *TSocks5authenticationRequest=*ProxySocksMemWork
      With *TSocks5authenticationRequest
        If \Version = $01 ; if the Method is accepted
          LenUser = \AuthenticationLoginAndPass[0]  
          UserName = PeekS(@\AuthenticationLoginAndPass[1],LenUser,#PB_UTF8 ) ;user
          LenPass = \AuthenticationLoginAndPass[1+LenUser] 
          Password = PeekS(@\AuthenticationLoginAndPass[1+LenUser+1],LenPass,#PB_UTF8 ); pass
          \Version=$05
          \AuthenticationLoginAndPass[0] =$00 ; ok authentication
          If SocksUser="":UserName="":EndIf:If ProxySocksPassword="":Password="":EndIf
          If UserName <> SocksUser Or Password <> ProxySocksPassword ; check username and Password
            \AuthenticationLoginAndPass[0] =$FF                      ; send authentication failed and close connection
          EndIf
          SendNetworkData(LocalConnectionID, *TSocks5authenticationRequest,2)
          If \AuthenticationLoginAndPass[0]  = $00:ProcedureReturn -2:EndIf; if ok return next level Negotiation
        EndIf
      EndWith
      
    ElseIf Socks5NegotiationLevel =-2
      ;Get ip and port
      *TSocks5Request=*ProxySocksMemWork
      With *TSocks5Request
        Select \Command 
          Case $01;establish a TCP/IP stream connection
                  ;Ok
          Case $02;establish a TCP/IP port binding :: currently not supported
          Case $03;associate a UDP port :: currently not supported
        EndSelect
        
        Select \addresstype
          Case $01 ; is a ipv4
            IP = Str(\IpAndPort[0]  ) + "." + Str(\IpAndPort[1]  ) + "."
            IP + Str(\IpAndPort[2]  ) + "." + Str(\IpAndPort[3]  )
            Port = (\IpAndPort[4] ) * 256
            Port + (\IpAndPort[5]  )     
            S5FixSize +6
          Case $03 ;is a Domain name
            LenIP = \IpAndPort[0]  
            IP = PeekS(@\IpAndPort[1],LenIP,#PB_UTF8 )
            Port = (\IpAndPort[LenIP+1] ) * 256
            Port + (\IpAndPort[LenIP+2]  )     
            S5FixSize +LenIP+2
          Case $04 ; is a ipv6 ; currently not supported
            For LenIP = 0 To 15
              IP + Str(\IpAndPort[LenIP] ) + "."
            Next
            Port = (\IpAndPort[16] ) * 256
            Port + (\IpAndPort[17]  )     
            S5FixSize +16+2
        EndSelect
        
        If  \Command =$01
          If IP <> "" 
            If \addresstype = $04
              \Command =$08; fail
            Else
              RemoteConnectionID=OpenNetworkConnection(IP,Port)
              If Not RemoteConnectionID
                \Command = $03; fail
              Else
                \Command= $00;request granted
              EndIf
              
            EndIf
          Else
            \Command =$01; fail
          EndIf
        Else
          \Command =$07; fail
        EndIf
        
        SendNetworkData(LocalConnectionID, *TSocks5Request,S5FixSize)
      EndWith
    EndIf
  EndIf
  
  ProcedureReturn RemoteConnectionID; return Remote server connection id
EndProcedure

salutations,,,
.....i Love Pb :)
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

Re: Crée un proxy server SOCKS v 5,4,4a multi os

Message par celtic88 »

exemple:

Code : Tout sélectionner

EnableExplicit

InitNetwork()

IncludeFile "Proxy Socks Protocol.Pbi"

Structure ProxySocks
  RemoteServerID.i
  LocalServerID.i
  Socks5NegotiationLevel.b
EndStructure

Global NewList ProxySocksList.ProxySocks()

Global ProxySocksPort.u=888
Global ProxySocksUser.s=""
Global ProxySocksPassword.s=""
Global *ProxySocksWorkMem = AllocateMemory(1024*64)

Procedure ProxySocksListFind(ConnId)
  ForEach ProxySocksList()
    If ProxySocksList()\LocalServerID=ConnId: ProcedureReturn 1: EndIf
  Next
  ProcedureReturn 0
EndProcedure

Procedure ProxySocks_Start(ProxySocksPort)
  If CreateNetworkServer(0, ProxySocksPort)
    Protected SEvent,ClientID,ReceiveLen,Connect,UseDelay
    
    Repeat
      UseDelay=1
      SEvent = NetworkServerEvent()
      If SEvent
        UseDelay=0
        ClientID = EventClient()
        
        Select SEvent
          Case #PB_NetworkEvent_Connect
            AddElement(ProxySocksList())
            ProxySocksList()\LocalServerID=ClientID
            
          Case #PB_NetworkEvent_Data
            ReceiveLen=ReceiveNetworkData(ClientID, *ProxySocksWorkMem, 1024*65)
            ProxySocksListFind(ClientID)
            If ProxySocksList()\RemoteServerID
              SendNetworkData(ProxySocksList()\RemoteServerID,*ProxySocksWorkMem,ReceiveLen)
            Else
              Connect=Proxy_Socks_4_4a_5(ClientID,*ProxySocksWorkMem,ProxySocksUser,ProxySocksPassword,ProxySocksList()\Socks5NegotiationLevel)
              If Connect > 0
                ProxySocksList()\RemoteServerID=Connect
              ElseIf Connect=0
                CloseNetworkConnection(ProxySocksList()\LocalServerID)
                DeleteElement(ProxySocksList())
              Else
                ProxySocksList()\Socks5NegotiationLevel=Connect
              EndIf
            EndIf
            
          Case #PB_NetworkEvent_Disconnect
            ProxySocksListFind(ClientID)
            If ProxySocksList()\RemoteServerID:CloseNetworkConnection(ProxySocksList()\RemoteServerID):EndIf
            DeleteElement(ProxySocksList())
            
        EndSelect
        
      EndIf
      
      ForEach ProxySocksList()
        If ProxySocksList()\RemoteServerID
          Select NetworkClientEvent(ProxySocksList()\RemoteServerID) 
            Case #PB_NetworkEvent_Data
              UseDelay=0
              ReceiveLen=ReceiveNetworkData(ProxySocksList()\RemoteServerID, *ProxySocksWorkMem, 1024*64)
              SendNetworkData(ProxySocksList()\LocalServerID,*ProxySocksWorkMem,ReceiveLen)
            Case #PB_NetworkEvent_Disconnect
              CloseNetworkConnection(ProxySocksList()\LocalServerID)
              DeleteElement(ProxySocksList())
          EndSelect
        EndIf
      Next  
      
      If UseDelay=1:Delay(10):EndIf
    ForEver
  EndIf
EndProcedure

ProxySocks_Start(ProxySocksPort)
.....i Love Pb :)
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Crée un proxy server SOCKS v 5,4,4a multi os

Message par Kwai chang caine »

Ce qui est fou, c'est qu'avant j'essayais de comprendre, la plupart du temps sans succès, les divers codes que les copains avaient la générosité de partager...
Mais avec toi, même plus j’essaie de les comprendre, mais de pouvoir un jour les utiliser tellement ils sont puissants :lol:

En tout cas merci à nouveau, du partage de ton don 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Répondre