Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Internet (https://www.clubdelphi.com/foros/forumdisplay.php?f=3)
-   -   Servidor proxy con problemas (https://www.clubdelphi.com/foros/showthread.php?t=86500)

FideRosado 22-08-2014 08:03:27

Servidor proxy con problemas
 
Hola amigos,recentemente me he puesto a trabajar en un proxy para mi red, slo que me da problemas, aver si alguno de ustedes ve el error por aca..

es socks 5, y he echo varias pruebas pero no me funciona con las aplicaciones que pas por el..


Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Socks5, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  tid: Cardinal;
  config : TSocks5config;
begin
  config.Port     := 1090;
  config.UserName := pchar('root');
  config.Password := pchar('toor');
  CreateThread(0,0,@StartSocks5,@config,0,tid);
end;

end.

Código Delphi [-]
unit Socks5;
{
+-----------------------------------------------------------+
|name:        uSocks5                                       |
|description: Establishes a SOCKS5 server, supporting AUTH   
|Author:      Hamtaro aka CorVu5                            |
|date:        16.10.09   / released: 24.10.09               |
|History:     first try                                     |
|ToDo:                                                      |
|       [*] Add support for Proxychains (!) & UDP (?)       |
|       [*] Add support for IPv6                            |
+-----------------------------------------------------------+
| This code can be changed or used without any restrictions |
+-----------------------------------------------------------+
}
interface
uses Windows, WinSock;

type
  TSocks5Config = record
    Port     : Dword;
    UserName : String;
    Password : String;
end;
type PSocks5Config = ^TSocks5Config;
type
  TSocks5MethodSel = record
    Version  : Byte;
    nMethods : Byte;
    Methods : array[0..255] of Byte;
end;

type TSocks5Request = record
       ucVersion : byte;
       ucCommand : byte;
       ucRzv : byte;
       ucAtyp : byte;
       dwDestIp  : dword;
       wDestPort : word;
end;

function StartSocks5(conf : PSocks5Config) : Boolean; stdcall;
var
  config : TSocks5Config;
implementation
procedure SocksProc(sock : Cardinal); stdcall;
var
    m : TSocks5MethodSel;
    req : TSocks5Request;
    auth :array[0..600] of Byte;
    buf  :array[0..500] of Byte;
    buffer : array[0..4095] of Byte;//4kb
    recv_len : Integer;
    i : Integer;
    recvsock : TSocket;
    UserName, password : String;
    tunneladdr_in : sockaddr_in;
    tunneldomain : String;
    tunnelsock : TSocket;
    hostent : PHostEnt;
    tv : Ttimeval;
    fset : tfdset;
    self_addr : sockaddr_in;
    self_Len : Integer;
begin
  recvsock := sock;
  if recv(recvsock,m,2,MSG_PEEK) > 0 then begin
    if m.Version = 5 then begin     //it is socks5
      recv(recvsock,m, 2 + m.nMethods,0); //request complete Header
      for i := 0 to m.nMethods - 1 Do begin
        if (m.Methods[i] = 2) then begin           //password auth
          if (config.UserName = '') and (config.Password = '') then begin
            m.nMethods := $00;
            send(recvsock, m,2,0);
            end else begin
            m.nMethods := 2;
            send(recvsock, m,2,0);
            recv(recvsock, auth,SizeOf(auth),0);
            if auth[0] = 1 Then begin
              //get username
              SetString(username,Pchar(@auth[2]),auth[1]);
              //get password
              SetString(password,Pchar(Cardinal(@auth) + 3 + auth[1]),auth[2 + auth[1]]);
              if (config.UserName = UserName) and (config.Password = password) then begin   //auth successful!
                auth[1] := 0;
                send(recvsock,auth,2,0);
              end else begin
                MessageBox(0,'auth fail','fffuuuuuuu-',0);
                auth[1] := $FF; //nothing but fail
                send(recvsock,auth,2,0);
                break;
              end;
            end;
          end;
        end else if (m.Methods[i] = 0) Then begin
          if (config.password = '') and (config.UserName = '') Then begin
            m.nMethods := 0;
            send(recvsock,m,2,0);
          end else begin
            m.nMethods := $FF;
            send(recvsock,m,2,0);
            break;
          end;
        end else if i = m.nMethods then begin
          m.nMethods := $FF;
          send(recvsock,m,2,0);
          Break;
        end;
        recv(recvsock, req, sizeof(Tsocks5Request), MSG_PEEK);
        if  req.ucCommand = 1 then begin        //TCP Verbindung, ok
          Zeromemory(@tunneladdr_in,sizeof(tunneladdr_in));
          if req.ucAtyp = 1 Then begin  //ip4
            recv(recvsock, req, sizeof(Tsocks5Request), 0);
            tunneladdr_in.sin_port := req.wDestPort;
            CopyMemory(@tunneladdr_in.sin_addr,@req.dwDestIp,sizeof(tunneladdr_in.sin_addr));
          end else if req.ucAtyp = 3 Then begin //domain name
            ZeroMemory(@buf,SizeOf(buf));
            recv(recvsock,buf,7 + Byte(req.dwDestIp),0);
            SetString(tunneldomain,PChar(Cardinal(@buf) + 5),Integer(Byte(req.dwDestIp)));
            hostent := gethostbyname(PChar(tunneldomain));
            PInteger(@tunneladdr_in.sin_addr.S_addr)^:=PInteger(HostEnt.h_addr^)^;
            tunneladdr_in.sin_port := htons(Word(Pointer(Cardinal(@buf) + 6 + Byte(req.dwDestIp))^));
          end; //todo: PIv6
          tunneladdr_in.sin_family := AF_INET;
          tunnelsock := socket(PF_INET, SOCK_STREAM, 0);
          if connect(tunnelsock,tunneladdr_in,sizeof(tunneladdr_in)) = 0 Then begin//success!
            req.ucCommand := 0;  //success
          end else begin
            req.ucCommand := 1; //General Failure reporting in
          end;
          req.ucVersion := 5;
          req.ucRzv := 0;
          req.ucAtyp := 1;
          ZeroMemory(@self_addr,SizeOf(sockaddr_in));
          self_Len := SizeOf(sockaddr_in);
          getsockname(tunnelsock,self_addr,self_len);
          CopyMemory(@req.dwDestIp,@self_addr.sin_addr,sizeof(self_addr.sin_addr));
          req.wDestPort := self_addr.sin_port;
          send(recvsock,req,10,0);
          //now tunneling everything!
          tv.tv_sec := 5;
          while 1 =1 Do begin
            //waiting for incoming data
            FD_ZERO(fset);
            FD_SET(recvsock,fset);
            FD_SET(tunnelsock,fset);
            if select(0,@fset,nil,nil,nil) <> SOCKET_ERROR Then begin
              if FD_ISSET(tunnelsock,fset) THEN begin //data on the recvsock
                ZeroMemory(@buffer,sizeof(buffer));
//                      MessageBoxa(0,'Data from the tunnelsock!','FF',0);
                recv_len := recv(tunnelsock, buffer,sizeof(buffer),0);
                if recv_len = SOCKET_ERROR Then break; //error?
//                      messagebox(0,PChar('tunnel' + #13#10 + pchar(@buffer)),0,0);
                send(recvsock,buffer,recv_len,0);
              end;
              if FD_ISSET(recvsock,fset) THEN begin //data on the recvsock
                ZeroMemory(@buffer,sizeof(buffer));
//                      MessageBoxa(0,'Data from the recvsock!','FF',0);
                recv_len := recv(recvsock, buffer,sizeof(buffer),0);
                if recv_len = SOCKET_ERROR Then break; //error?
//                      messagebox(0,PChar('recv' + #13#10 + pchar(@buffer)),0,0);
                send(tunnelsock,buffer,recv_len,0);
              end;
            end;
            Sleep(150); //zzZZzzZZZZzz
          end;
        end;
        Break;
      end;
    end;
  end;
//  MessageBox(0,PChar('Error Code: ' + inttostr(WSAGetLastError)),'Error!',0);
  closesocket(recvsock);
  closesocket(tunnelsock);
end;

function StartSocks5(conf : PSocks5Config) : Boolean; stdcall;
var
  wsaData : TWSAData;
  sock    : TSOCKET;
  sockaddr: SockAddr_in;
  conn    : Integer;
  client  : TSockAddr;
  tid : Cardinal;
  size : Integer;
begin
  result := False;
  Move(conf^,config,SizeOf(TSocks5Config));
  WSAStartup($101, wsaData);
  sock := socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  ZeroMemory(@SockAddr, sizeof(SockAddr));
  sockaddr.sin_family := AF_INET;
  sockaddr.sin_port   := htons(config.Port);
  sockaddr.sin_addr.S_addr   := INADDR_ANY;
  if (bind  (sock  ,sockaddr,SizeOf(sockaddr)) = 0) AND
     (listen(sock,SOMAXCONN)                   = 0) then begin
      while 1 = 1 Do begin
        size := SizeOf(client);
        conn := accept(sock,@client,@size);
        if conn <> SOCKET_ERROR then  begin
          CreateThread(nil,0,@SocksProc,Pointer(conn),0,tid);
        end;
        Sleep(100);
      end;
  end;

end;

end.

FideRosado 08-09-2014 17:42:21

respuesta
 
ya he tarequeado los proxy que hay en la red, los proy, pero ninguno me ayuda en lo que necesito, estoy necesitando uno ke pueda servir para http,https,sock5, alguien tiene algun proy o guia creado para hacer uno...

FideRosado 14-10-2014 04:51:16

respuesta
 
holaa. por favor alguien que me pueda ayudar con eeste proy, ya que ando botao en el tema. el caso es que tengo una red , y pues controlo usuarios por un programa creado a mi forma en delphi, los tengo en un listvew, y pues queria hacer que cuando uno este activo el programa proxy que se comunica con la aplicacion permita las conexiones de ese cliente en especifico. algo cencillo.. en teoria. yo prove unos demos de indy9 y me sirve para http pero no para https (443) y socks5, y eso pues e s muy importante para mi y por supuesto para muchos que han intentado hacer un servidor proxy.. intentemos hacer uno entre todos..!!


La franja horaria es GMT +2. Ahora son las 21:07:01.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi