unit DblPxyTcp;interfaceuses SysUtils, Classes, blckSock, synsock;type TDblProxyTcpSocket = class(TTCPBlockSocket) public constructor Create; procedure Connect(IP, Port: string); override; procedure Listen; override; end;implementationconstructor TDblProxyTcpSocket.Create; begin inherited Create; end;procedure TDblProxyTcpSocket.Connect(IP, Port: string); var x: integer; b: boolean; begin x := 0; if FSocksIP <> '' then x := x + 1; if FHTTPTunnelIP <> '' then x := x + 2; case x of 1: SocksDoConnect(IP, Port); 2: HTTPTunnelDoConnect(IP, Port); 3: begin HTTPTunnelDoConnect(FSocksIP, FSocksPort); if FLastError = 0 then begin b := SocksOpen; if b then b := SocksRequest(1, IP, Port); if b then b := SocksResponse; if not b and (FLastError = 0) then FLastError := WSASYSNOTREADY; FSocksLocalIP := FSocksResponseIP; FSocksLocalPort := FSocksResponsePort; FSocksRemoteIP := IP; FSocksRemotePort := Port; end; ExceptCheck; DoStatus(HR_Connect, IP + ':' + Port); end; else inherited Connect(IP, Port); end; if FSslEnabled then if FLastError = 0 then SSLDoConnect else begin x := FLastError; SSLEnabled := False; FLastError := x; end; end;procedure TDblProxyTcpSocket.Listen; var b: Boolean; Sip, SPort, SC: string; begin if FSocksIP = '' then begin SockCheck(synsock.Listen(FSocket, SOMAXCONN)); GetSins; end else begin Sip := GetLocalSinIP; if Sip = cAnyHost then Sip := LocalName; SPort := IntToStr(GetLocalSinPort); if FHTTPTunnelIP = '' then begin SC := FSocksIP; FSocksIP := ''; inherited Connect(SC, FSocksPort); FSocksIp := SC; end else HTTPTunnelDoConnect(FSocksIP, FSocksPort); b := SocksOpen; if b then b := SocksRequest(2, Sip, SPort); if b then b := SocksResponse; if not b and (FLastError = 0) then FLastError := WSANO_RECOVERY; FSocksLocalIP := FSocksResponseIP; if FSocksLocalIP = cAnyHost then FSocksLocalIP := FSocksIP; FSocksLocalPort := FSocksResponsePort; FSocksRemoteIP := ''; FSocksRemotePort := ''; end; ExceptCheck; DoStatus(HR_Listen, ''); end;end.
procedure TFormSocks.FormCreate(Sender: TObject); begin //update view Memo1.Lines.Clear; Edit1.Text:='10.10.2.51'; Edit2.Text:='1080'; Edit3.Text:='10.12.13.66'; Edit4.Text:='23'; // Edit5.Text:=''; // Edit6.Text:=''; Edit6.PasswordChar:='*'; end;procedure TFormSocks.FormShow(Sender: TObject); var WSAData:TWSAData; begin //init Socket if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then begin //初始化失败 Memo1.lines.add('Init Failed'); exit; end else Memo1.lines.add('Init Success'); end;procedure TFormSocks.FormClose(Sender: TObject; var Action: TCloseAction); begin //release winsock WSACleanUP(); end;procedure TFormSocks.btnConnectClick(Sender: TObject); var clisock:TSocket; tarsocksrv:TSockAddr; saproxy:TSockAddr; pSocksAddr:PChar; Re:integer; ErrCode:Integer; AuthSucc:boolean; ConSucc:boolean; begin //1.create a client socket Memo1.Lines.Add('Creating a socket'); clisock:=socket(AF_INET,SOCK_STREAM,0); //valid check if(clisock=INVALID_SOCKET) then begin Memo1.lines.Add('Error on create socket'); Exit; end; Memo1.Lines.add('Success on create socket'); //2.connect to socks5 server Memo1.lines.Add('Connecting to socks server'); ZeroMemory(@saproxy,sizeof(saproxy)); saproxy.sin_family := AF_INET; //set proxy infomation GetMem(pSocksAddr,Length(Edit1.Text)+1); ZeroMemory(pSocksAddr,Length(Edit1.Text)+1); StrPCopy(pSocksAddr,Edit1.Text); saproxy.sin_addr.S_addr :=inet_addr(pSocksAddr); FreeMem(pSocksAddr); saproxy.sin_port := htons(strtoint(Edit2.Text)); Re:=connect(clisock,saproxy,sizeof(saproxy)); if Re = SOCKET_ERROR then begin ErrCode:=WSAGetLastError(); Memo1.Lines.Add('Err on Connect to socks5 Errcode:' +IntToStr(ErrCode)); Exit; end; Memo1.Lines.Add('Connect to socks5 server success'); //3.prepare Auth Memo1.Lines.Add('Enter Auth'); if CheckBox1.Checked then AuthSucc:=Auth(clisock,AUTH_REQU) else AuthSucc:=Auth(clisock,AUTH_NO); if not AuthSucc then begin Memo1.Lines.Add('Auth Failed'); Memo1.Lines.Add('Close socket'); CloseSocket(clisock); Exit; end; Memo1.Lines.Add('Auth finish'); //4. link to target Memo1.Lines.Add('Link to target ip'); ZeroMemory(@tarsocksrv,Sizeof(tarsocksrv)); //prepare Getmem(psocksaddr,length(edit3.text)+1); zeromemory(psocksaddr,length(edit3.text)+1); strpcopy(psocksaddr,edit3.text); tarsocksrv.sin_addr.s_addr := inet_addr(pSocksAddr); tarsocksrv.sin_port := htons(strtoint(edit4.text)); tarsocksrv.sin_family := AF_INET; //connect consucc:=socksconnect(clisock,tarsocksrv); if consucc then memo1.Lines.Add('conncct succ') else memo1.Lines.add('failed'); //5 use this socket pipe to communicat //recv(clisock,buf,127,0); //close socket Memo1.lines.Add('Closing socket'); Re:= closesocket(clisock); //shutdown(clisock,SD_BOTH); end;
function TFormSocks.socksconnect(skt: TSocket; target: TSockAddr): boolean; var buf:array[0..1023]of byte; re:integer; begin //preapre buf[0] := SOCKS_VER5; buf[1] := CMD_CONNECT; buf[2] := RSV_DEFAULT; buf[3] := ATYP_IPV4; //copy data copymemory(@buf[4],@target.sin_addr,4); copymemory(@buf[8],@target.sin_port,2); //communicate re:=send(skt,buf,10,0); if re=-1 then begin result:= false; exit; end; re :=recv(skt,buf,1024,0); if re=-1 then begin result:=false; exit; end; if buf[1]<>REP_SUCCESS then begin result:=false; exit; end; result:=true; end;function TFormSocks.Auth(skt: tsocket; bauth: byte): boolean; var buf:array[0..256]of byte; re:integer; i:integer; usr:pchar; pwd:pchar; begin getmem(usr, length(edit5.text)+1); zeromemory(usr, length(edit5.text)+1); strpcopy(usr,edit5.text); getmem(pwd, length(edit6.text)+1); zeromemory(pwd, length(edit6.text)+1); strpcopy(pwd,edit6.text); case bauth of AUTH_NO: begin buf[0] := SOCKS_VER5; buf[1] := $01; buf[2] := $00; re := send(skt, buf, 3, 0); if re=-1 then begin result:=false; exit; end; re:=recv(skt,buf,257,0); if re<2 then begin result:=false; exit; end; if buf[1]<>AUTH_NO then begin result:=false; exit; end; result:=true; end; AUTH_REQU: begin buf[0] := SOCKS_VER5; buf[1] := $02; buf[2] := $00; buf[3] := $02; re := send(skt, buf, 4, 0); if (re=-1)then begin result:=false; exit; end; ZeroMemory(@buf,257); re := recv(skt, buf, 257, 0); if (re < 2) then begin result:=false; exit; end; if (buf[1] <> AUTH_REQU) then begin result:=false; exit; end; zeromemory(@buf,257); buf[0] := $01; //current version of subnegotiation buf[1] := length(edit5.text); //length of username for i:=0 to buf[1]-1 do buf[2+i]:=ord(usr[i]); buf[2+length(edit5.text)]:=length(edit6.text); for i:=0 to buf[2+length(edit5.text)]-1 do buf[3+length(edit5.text)+i]:=ord(pwd[i]); re := send(skt, buf,length(edit5.text)+length(edit6.text)+3,0); if (re=-1) then begin result:=false; exit; end; re := recv(skt, buf, 257, 0); if (buf[1] <> $00) then begin result:=FALSE; exit; end; result:= TRUE; end; else result:=false; end; freemem(usr); freemem(pwd); end;end.
TDblProxyTcpSocket = class(TTCPBlockSocket)
public
constructor Create;
procedure Connect(IP, Port: string); override;
procedure Listen; override;
end;implementationconstructor TDblProxyTcpSocket.Create;
begin
inherited Create;
end;procedure TDblProxyTcpSocket.Connect(IP, Port: string);
var x: integer;
b: boolean;
begin
x := 0;
if FSocksIP <> '' then x := x + 1;
if FHTTPTunnelIP <> '' then x := x + 2;
case x of
1: SocksDoConnect(IP, Port);
2: HTTPTunnelDoConnect(IP, Port);
3: begin
HTTPTunnelDoConnect(FSocksIP, FSocksPort);
if FLastError = 0 then
begin
b := SocksOpen;
if b then b := SocksRequest(1, IP, Port);
if b then b := SocksResponse;
if not b and (FLastError = 0) then FLastError := WSASYSNOTREADY;
FSocksLocalIP := FSocksResponseIP;
FSocksLocalPort := FSocksResponsePort;
FSocksRemoteIP := IP;
FSocksRemotePort := Port;
end;
ExceptCheck;
DoStatus(HR_Connect, IP + ':' + Port);
end;
else
inherited Connect(IP, Port);
end;
if FSslEnabled then
if FLastError = 0 then
SSLDoConnect
else
begin
x := FLastError;
SSLEnabled := False;
FLastError := x;
end;
end;procedure TDblProxyTcpSocket.Listen;
var
b: Boolean;
Sip, SPort, SC: string;
begin
if FSocksIP = '' then
begin
SockCheck(synsock.Listen(FSocket, SOMAXCONN));
GetSins;
end
else
begin
Sip := GetLocalSinIP;
if Sip = cAnyHost then Sip := LocalName;
SPort := IntToStr(GetLocalSinPort);
if FHTTPTunnelIP = '' then
begin
SC := FSocksIP;
FSocksIP := '';
inherited Connect(SC, FSocksPort);
FSocksIp := SC;
end
else HTTPTunnelDoConnect(FSocksIP, FSocksPort);
b := SocksOpen;
if b then
b := SocksRequest(2, Sip, SPort);
if b then
b := SocksResponse;
if not b and (FLastError = 0) then
FLastError := WSANO_RECOVERY;
FSocksLocalIP := FSocksResponseIP;
if FSocksLocalIP = cAnyHost then
FSocksLocalIP := FSocksIP;
FSocksLocalPort := FSocksResponsePort;
FSocksRemoteIP := '';
FSocksRemotePort := '';
end;
ExceptCheck;
DoStatus(HR_Listen, '');
end;end.
只有参考资料可以免费:)访问
http://lysoft.7u7.net
获得相关DOC文档
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock;
const
SOCKS_VER5=$05; AUTH_NO=$00 ;//no authentication required
AUTH_REQU=$02 ;//method=0x02: username/password CMD_CONNECT=$01 ;
RSV_DEFAULT=$00 ;
ATYP_DN=$03 ;
REP_SUCCESS=$00;
ATYP_IPV4=$01;type
TFormSocks = class(TForm)
GroupBox1: TGroupBox;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
CheckBox1: TCheckBox;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
btnConnect: TButton;
Memo1: TMemo;
Label5: TLabel;
Label6: TLabel;
Edit5: TEdit;
Label7: TLabel;
Edit6: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnConnectClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function socksconnect(skt:TSocket;target:TSockAddr):boolean;
function Auth(skt:tsocket;bauth:byte):boolean;
end;var
FormSocks: TFormSocks;implementation{$R *.DFM}
procedure TFormSocks.FormCreate(Sender: TObject);
begin
//update view
Memo1.Lines.Clear;
Edit1.Text:='10.10.2.51';
Edit2.Text:='1080';
Edit3.Text:='10.12.13.66';
Edit4.Text:='23';
// Edit5.Text:='';
// Edit6.Text:='';
Edit6.PasswordChar:='*';
end;procedure TFormSocks.FormShow(Sender: TObject);
var
WSAData:TWSAData;
begin
//init Socket
if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
begin
//初始化失败
Memo1.lines.add('Init Failed');
exit;
end
else
Memo1.lines.add('Init Success');
end;procedure TFormSocks.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//release winsock
WSACleanUP();
end;procedure TFormSocks.btnConnectClick(Sender: TObject);
var
clisock:TSocket;
tarsocksrv:TSockAddr;
saproxy:TSockAddr;
pSocksAddr:PChar;
Re:integer;
ErrCode:Integer;
AuthSucc:boolean;
ConSucc:boolean;
begin
//1.create a client socket
Memo1.Lines.Add('Creating a socket');
clisock:=socket(AF_INET,SOCK_STREAM,0);
//valid check
if(clisock=INVALID_SOCKET) then
begin
Memo1.lines.Add('Error on create socket');
Exit;
end;
Memo1.Lines.add('Success on create socket');
//2.connect to socks5 server
Memo1.lines.Add('Connecting to socks server');
ZeroMemory(@saproxy,sizeof(saproxy));
saproxy.sin_family := AF_INET;
//set proxy infomation
GetMem(pSocksAddr,Length(Edit1.Text)+1);
ZeroMemory(pSocksAddr,Length(Edit1.Text)+1);
StrPCopy(pSocksAddr,Edit1.Text);
saproxy.sin_addr.S_addr :=inet_addr(pSocksAddr);
FreeMem(pSocksAddr);
saproxy.sin_port := htons(strtoint(Edit2.Text));
Re:=connect(clisock,saproxy,sizeof(saproxy));
if Re = SOCKET_ERROR then
begin
ErrCode:=WSAGetLastError();
Memo1.Lines.Add('Err on Connect to socks5 Errcode:'
+IntToStr(ErrCode));
Exit;
end;
Memo1.Lines.Add('Connect to socks5 server success');
//3.prepare Auth
Memo1.Lines.Add('Enter Auth');
if CheckBox1.Checked then
AuthSucc:=Auth(clisock,AUTH_REQU)
else
AuthSucc:=Auth(clisock,AUTH_NO);
if not AuthSucc then
begin
Memo1.Lines.Add('Auth Failed');
Memo1.Lines.Add('Close socket');
CloseSocket(clisock);
Exit;
end;
Memo1.Lines.Add('Auth finish');
//4. link to target
Memo1.Lines.Add('Link to target ip');
ZeroMemory(@tarsocksrv,Sizeof(tarsocksrv));
//prepare
Getmem(psocksaddr,length(edit3.text)+1);
zeromemory(psocksaddr,length(edit3.text)+1);
strpcopy(psocksaddr,edit3.text);
tarsocksrv.sin_addr.s_addr := inet_addr(pSocksAddr);
tarsocksrv.sin_port := htons(strtoint(edit4.text));
tarsocksrv.sin_family := AF_INET;
//connect
consucc:=socksconnect(clisock,tarsocksrv);
if consucc then
memo1.Lines.Add('conncct succ')
else
memo1.Lines.add('failed');
//5 use this socket pipe to communicat
//recv(clisock,buf,127,0);
//close socket
Memo1.lines.Add('Closing socket');
Re:= closesocket(clisock); //shutdown(clisock,SD_BOTH);
end;
function TFormSocks.socksconnect(skt: TSocket; target: TSockAddr): boolean;
var
buf:array[0..1023]of byte;
re:integer;
begin
//preapre
buf[0] := SOCKS_VER5;
buf[1] := CMD_CONNECT;
buf[2] := RSV_DEFAULT;
buf[3] := ATYP_IPV4;
//copy data
copymemory(@buf[4],@target.sin_addr,4);
copymemory(@buf[8],@target.sin_port,2);
//communicate
re:=send(skt,buf,10,0);
if re=-1 then
begin
result:= false;
exit;
end;
re :=recv(skt,buf,1024,0);
if re=-1 then
begin
result:=false;
exit;
end;
if buf[1]<>REP_SUCCESS then
begin
result:=false;
exit;
end;
result:=true;
end;function TFormSocks.Auth(skt: tsocket; bauth: byte): boolean;
var
buf:array[0..256]of byte;
re:integer;
i:integer;
usr:pchar;
pwd:pchar;
begin
getmem(usr, length(edit5.text)+1);
zeromemory(usr, length(edit5.text)+1);
strpcopy(usr,edit5.text); getmem(pwd, length(edit6.text)+1);
zeromemory(pwd, length(edit6.text)+1);
strpcopy(pwd,edit6.text);
case bauth of
AUTH_NO:
begin
buf[0] := SOCKS_VER5;
buf[1] := $01;
buf[2] := $00;
re := send(skt, buf, 3, 0);
if re=-1 then
begin
result:=false;
exit;
end;
re:=recv(skt,buf,257,0);
if re<2 then
begin
result:=false;
exit;
end;
if buf[1]<>AUTH_NO then
begin
result:=false;
exit;
end;
result:=true;
end;
AUTH_REQU:
begin
buf[0] := SOCKS_VER5;
buf[1] := $02;
buf[2] := $00;
buf[3] := $02;
re := send(skt, buf, 4, 0);
if (re=-1)then
begin
result:=false;
exit;
end;
ZeroMemory(@buf,257);
re := recv(skt, buf, 257, 0);
if (re < 2) then
begin
result:=false;
exit;
end;
if (buf[1] <> AUTH_REQU) then
begin
result:=false;
exit;
end;
zeromemory(@buf,257); buf[0] := $01; //current version of subnegotiation
buf[1] := length(edit5.text); //length of username
for i:=0 to buf[1]-1 do
buf[2+i]:=ord(usr[i]);
buf[2+length(edit5.text)]:=length(edit6.text);
for i:=0 to buf[2+length(edit5.text)]-1 do
buf[3+length(edit5.text)+i]:=ord(pwd[i]);
re := send(skt, buf,length(edit5.text)+length(edit6.text)+3,0);
if (re=-1) then
begin
result:=false;
exit;
end;
re := recv(skt, buf, 257, 0);
if (buf[1] <> $00) then
begin
result:=FALSE;
exit;
end;
result:= TRUE;
end; else
result:=false;
end; freemem(usr);
freemem(pwd);
end;end.