如何编程实现Sock5的代理服务,最好有源码(delphi的)!(300分马上给) (300分)

  • 主题发起人 主题发起人 海天子
  • 开始时间 开始时间

海天子

Unregistered / Unconfirmed
GUEST, unregistred user!
油箱wclhjh@163.net,收到或贴出给分!!
 
下载地址: http://www.playicq.com/dispdocnew.php?id=8864
user:52free pass:123456
请不要修改密码

unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
RXShell, Menus, StdCtrls, ExtCtrls, ScktComp;
type
session_record=record
Used:boolean;
SS_Handle:integer;
CSocket:Tclientsocket;
Lookingup:boolean;
LookupTime:integer;
Request:boolean;
request_str:string;
client_connected:boolean;
remote_connected:boolean;
end;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
ClientSocket1: TClientSocket;
Timer1: TTimer;
Label1: TLabel;
Edit1: TEdit;
Memo1: TMemo;
PopupMenu1: TPopupMenu;
menuopen: TMenuItem;
menuclose: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
RxTrayIcon1: TRxTrayIcon;
N3: TMenuItem;
N4: TMenuItem;
btopen: TButton;
btclose: TButton;
Button3: TButton;
bthide: TButton;
menuhide: TMenuItem;
Button1: TButton;
procedure AppException(sender:TObject;e:Exception);
procedure menuopenClick(Sender: TObject);
procedure menucloseClick(Sender: TObject);
procedure menuexit(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ClientSocket1Write(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure Timer1Timer(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure RxTrayIcon1Click(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ClientSocket1Lookup(Sender: TObject;
Socket: TCustomWinSocket);
procedure bthideClick(Sender: TObject);
procedure menuhideClick(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connecting(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
Server_Enabled:boolean;
session:array of session_record;
sessions:integer;
LookupTimeOut:integer;
InvalidRequests:integer;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.AppException (sender:TObject;E:Exception);
begin
//
end;
procedure TForm1.menuopenClick(Sender: TObject);
begin
serversocket1.active:=true;
end;

procedure TForm1.menucloseClick(Sender: TObject);
begin
serversocket1.Active :=false;
server_enabled:=false;
menuopen.Enabled :=true;
btopen.Enabled :=true;
menuclose.Enabled :=false;
btclose.Enabled :=false;
end;

procedure TForm1.menuexit(Sender: TObject);
begin
application.Terminate ;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
server_enabled:=false;
sessions:=0;
invalidrequests:=0;
LookupTimeOut:=60000;
timer1.Enabled :=true;
menuopen.Enabled :=false;
btopen.Enabled :=false;
menuclose.Enabled :=true;
btclose.Enabled :=true;
serversocket1.Port :=988;
serversocket1.Active :=true;
//application.OnActionExecute :=appexception;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
timer1.Enabled :=false;
if server_enabled then serversocket1.Active:=false;
end;

procedure TForm1.ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
server_enabled:=true;
menuopen.Enabled :=false;
btopen.Enabled :=false;
menuclose.Enabled :=true;
btclose.Enabled :=true;
end;

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var i,j:integer;
begin
j:=-1;
for i:=1 to sessions do
if not session[i-1].Used and not session[i-1].CSocket.active then
begin
j:=i-1;
session[j].Used :=true;
break;
end
else
if not session[i-1].Used and session[i-1].CSocket.active then session[j].CSocket .active:=false ;
if j=-1 then
begin
j:=sessions;
inc(sessions);
setlength(session,sessions);
session[j].Used :=true;
session[j].CSocket :=Tclientsocket.Create (nil);
session[j].CSocket.onconnect:=clientsocket1connect;
session[j].CSocket.ondisconnect:=clientsocket1disconnect;
session[j].CSocket.onerror:=clientsocket1error;
session[j].CSocket.onread:=clientsocket1read;
session[j].CSocket .onwrite:=clientsocket1write;
session[j].CSocket .onlookup:=clientsocket1lookup;
session[j].CSocket .onconnecting:=clientsocket1connecting;
session[j].lookingup:=false;
end;
session[j].SS_Handle :=socket.SocketHandle ;
session[j].Request :=false;
session[j].client_connected :=true;
session[j].remote_connected :=false;
edit1.Text :=inttostr(sessions);
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var i,j,k:integer;
begin
for i:=1 to sessions do
if (session[i-1].SS_Handle =socket.sockethandle) and session[i-1].Used then
begin
session[i-1].client_connected :=false;
if session[i-1].remote_connected then
session[i-1].CSocket.active:=false
else
session[i-1].Used :=false;
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[j-i].Used then break;
inc(k);
end;
if k>0 then
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.Text :=inttostr(sessions);
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var i:integer;
begin
for i:=1 to sessions do
if (session[i-1].Csocket.socket.sockethandle=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].CSocket.tag:=socket.sockethandle;
session[i-1].remote_connected :=true;
session[i-1].Lookingup :=false;
break;
end;
end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j,k:integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle ) and session[i-1].Used then
begin
session[i-1].remote_connected :=false;
if not session[i-1].client_connected then
session[i-1].Used :=false
else
for k:=1 to serversocket1.Socket.ActiveConnections do
if (serversocket1.Socket.Connections [k-1].SocketHandle =session[i-1].SS_Handle ) and session[i-1].Used then
begin
serversocket1.Socket.Connections[k-1].Close ;
break;
end;
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[j-i].Used then
break;
inc(k);
end;
if k>0 then
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
i,j,k:integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle ) and session[i-1].Used then
begin
socket.Close ;
session[i-1].remote_connected :=false;
if not session[i-1].client_connected then
session[i-1].Used :=false
else
for k:=1 to serversocket1.Socket.ActiveConnections do
if (serversocket1.Socket.Connections[k-1].SocketHandle =session[i-1].SS_Handle ) and session[i-1].Used then
begin
serversocket1.Socket.Connections [k-1].Close;
break;
end;
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[j-i].Used then
break;
inc(k);
end;
errorcode:=0;
if k>0 then
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.Text :=inttostr(sessions);
end;

procedure TForm1.ClientSocket1Write(Sender: TObject;
Socket: TCustomWinSocket);
var i:integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle ) and session[i-1].Used then
begin
if session[i-1].Request then
begin
socket.SendText(session[i-1].request_str);
session[i-1].Request :=false;
end;
break;
end;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j,rec_bytes:integer;
rec_buffer:array[0..2047] of char;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.tag=socket.SocketHandle ) and session[i-1].used then
begin
rec_bytes:=socket.ReceiveBuf (rec_buffer,2048);
for j:=1 to serversocket1.Socket.ActiveConnections do
if serversocket1.Socket.Connections [j-1].SocketHandle =session[i-1].SS_Handle then
begin
serversocket1.Socket.Connections [j-1].SendBuf (rec_buffer,rec_bytes);
break;
end;
break;
end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
i,j:integer;
begin
for i:=1 to sessions do
if session[i-1].Used and session[i-1].Lookingup then
begin
inc(session[i-1].LookupTime );
if session[i-1].LookupTime >lookuptimeout then
begin
session[i-1].Lookingup :=false;
session[i-1].CSocket.active:=false;
for j:=1 to serversocket1.socket.activeconnections do
if serversocket1.Socket.Connections[j-1].sockethandle=session[i-1].ss_handle then
begin
serversocket1.socket.connections[j-1].close;
break;
end;
end;
end;

end;


procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var i,j,k:integer;
begin
for i:=1 to sessions do
if (session[i-1].SS_Handle=socket.sockethandle) and session[i-1].Used then
begin
session[i-1].client_connected:=false;
if session[i-1].remote_connected then
session[i-1].CSocket.active:=false
else
session[i-1].Used:=false;
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[j-i].Used then
break;
inc(k);
end;
if k>0 then
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
errorcode:=0;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
tmp,line,host:string;
i,j,port:integer;
begin
for i:=1 to sessions do
if session[i-1].Used and (session[i-1].SS_Handle=socket.sockethandle) then
begin
session[i-1].request_str:=socket.receivetext;
tmp:=session[i-1].request_str;
memo1.lines.add(tmp);
j:=pos(char(13)+char(10),tmp);
while j>0 do begin
line:=copy(tmp,1,j-1);
delete(tmp,1,j+1);
j:=pos('Host',line);
if j>0 then
begin
delete(line,1,j+5);
j:=pos(':',line);
if j>0 then
begin
host:=copy(line,1,j-1);
delete(line,1,j-1);
try
port:=strtoint(line);
except
port:=80
end;
end
else
begin
host:=trim(line);
port:=80;
end;
if not session[i-1].remote_connected then
begin
session[i-1].request:=true;
session[i-1].CSocket.host:=host;
session[i-1].CSocket.port:=port;
session[i-1].CSocket.active:=true;
session[i-1].lookingup:=true;
session[i-1].lookuptime:=0;
end
else
session[i-1].Csocket.socket.sendtext(session[i-1].request_str);
break;
end;
j:=pos(char(13)+char(10),tmp);
end;
break;
end;
end;
procedure TForm1.N4Click(Sender: TObject);
begin
form1.Show;
end;

procedure TForm1.RxTrayIcon1Click(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
form1.Show;
end;

procedure TForm1.ClientSocket1Lookup(Sender: TObject;
Socket: TCustomWinSocket);
begin
//
end;

procedure TForm1.bthideClick(Sender: TObject);
begin
form1.Hide ;
end;

procedure TForm1.menuhideClick(Sender: TObject);
begin
form1.Hide ;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
if form1.WindowState =wsminimized then showmessage('aaaa');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
form1.WindowState :=wsminimized;
end;

procedure TForm1.ClientSocket1Connecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
//
end;

end.
 
To tt8:我拷完了,可以把密码改回了,我先试试,如果合我的要求,我一定会给你分的!先谢谢!

我简单看了一下,好像你贴出的不是Sock5代理,是http代理, 我要的是Sock5代理!
 
这个不是我的帐号,是52free贡献出来的,只要不改密码就好

你可以到www.playicq.com搜索其他代码看看。
 
斑竹! tt8 这么辛苦! 以后给分应该有他的份!!
 
我上学的时候写过一个,用的完成端口
有很多BUG
http://kuga.51.net/download/index.htm
现在正打算重新写一个,基础类库已经做好了
 
to gxcooo:
谢谢了。
 
to gxcooo
学习一下 完成端口!

我写过一个免费socks5代理服务器,
支持欺骗模式,主要用来破解某些加密的 url

发送邮件到 mastercn@163.com ,我好发给你
 
to lich:
可以给我一份吗?谢谢!
我已经发给你邮件了。或者你直接发到这个邮箱也可以:
goofree@163.com
再次感谢!
 
http://www.tech521.com/techData/data/1613.asp

我也有份 呵呵
 
SOCK5的一段测试程序。
unit Unit1;

interface

uses
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);
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);
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.
 
后退
顶部