Delphi/Pascal'da proxy'ler
Delphi ve Free Pascal, Windows uygulamaları geliştirmek için hâlâ popülerdir. Proxy'lerle çalışma Indy (Delphi standardı), Synapse ve WinINet API kütüphaneleri üzerinden yapılır.
Indy (Internet Direct)
Indy, Delphi'nin ağ programlama için yerleşik kütüphanesidir.
HTTP proxy
uses IdHTTP, IdSSLOpenSSL;
procedure FetchViaProxy;
var
HTTP: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
HTTP := TIdHTTP.Create(nil);
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
HTTP.IOHandler := SSL;
// Proxy yapılandırması
HTTP.ProxyParams.ProxyServer := 'proxy_ip';
HTTP.ProxyParams.ProxyPort := 8080;
HTTP.ProxyParams.ProxyUsername := 'username';
HTTP.ProxyParams.ProxyPassword := 'password';
HTTP.ProxyParams.BasicAuthentication := True;
WriteLn(HTTP.Get('https://httpbin.org/ip'));
finally
SSL.Free;
HTTP.Free;
end;
end;
Indy üzerinden SOCKS5 proxy
uses IdHTTP, IdSocks, IdSSLOpenSSL;
procedure FetchViaSocks5;
var
HTTP: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
Socks: TIdSocksInfo;
begin
HTTP := TIdHTTP.Create(nil);
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
Socks := TIdSocksInfo.Create(nil);
try
// SOCKS5 yapılandırması
Socks.Host := 'proxy_ip';
Socks.Port := 1080;
Socks.Version := svSocks5;
Socks.Username := 'username';
Socks.Password := 'password';
Socks.Authentication := saUsernamePassword;
SSL.TransparentProxy := Socks;
HTTP.IOHandler := SSL;
WriteLn(HTTP.Get('https://httpbin.org/ip'));
finally
Socks.Free;
SSL.Free;
HTTP.Free;
end;
end;
SOCKS4
Socks.Version := svSocks4;
// SOCKS4 kimlik doğrulamayı desteklemez
Socks.Authentication := saNoAuthentication;
Synapse
Synapse, Free Pascal ve Delphi için alternatif bir ağ kütüphanesidir.
Kurulum
synapse.ararat.cz adresinden indirin ve projenize ekleyin.
HTTP proxy
uses httpsend;
procedure FetchViaSynapseProxy;
var
HTTP: THTTPSend;
begin
HTTP := THTTPSend.Create;
try
HTTP.ProxyHost := 'proxy_ip';
HTTP.ProxyPort := '8080';
HTTP.ProxyUser := 'username';
HTTP.ProxyPass := 'password';
if HTTP.HTTPMethod('GET', 'https://httpbin.org/ip') then
WriteLn(HTTP.Document.DataString)
else
WriteLn('Error: ', HTTP.ResultCode);
finally
HTTP.Free;
end;
end;
Synapse üzerinden SOCKS
uses blcksock;
procedure FetchViaSynapseSocks;
var
Sock: TTCPBlockSocket;
begin
Sock := TTCPBlockSocket.Create;
try
Sock.SocksIP := 'proxy_ip';
Sock.SocksPort := '1080';
Sock.SocksUsername := 'username';
Sock.SocksPassword := 'password';
Sock.SocksType := ST_Socks5;
Sock.Connect('httpbin.org', '80');
if Sock.LastError = 0 then begin
Sock.SendString('GET /ip HTTP/1.1' + #13#10 +
'Host: httpbin.org' + #13#10 +
#13#10);
WriteLn(Sock.RecvString(10000));
end;
finally
Sock.Free;
end;
end;
WinINet API (Windows)
Sistem Windows API'sini kullanarak:
uses WinInet;
procedure FetchViaWinINet;
var
hInternet, hConnect: HINTERNET;
Buffer: array[0..4095] of Char;
BytesRead: DWORD;
begin
// Proxy ile aç
hInternet := InternetOpen(
'MyApp',
INTERNET_OPEN_TYPE_PROXY,
PChar('http://proxy_ip:8080'),
nil,
0
);
if hInternet <> nil then begin
// Proxy kimlik doğrulamasını ayarla
InternetSetOption(hInternet, INTERNET_OPTION_PROXY_USERNAME,
PChar('username'), Length('username'));
InternetSetOption(hInternet, INTERNET_OPTION_PROXY_PASSWORD,
PChar('password'), Length('password'));
hConnect := InternetOpenUrl(hInternet,
'https://httpbin.org/ip', nil, 0,
INTERNET_FLAG_RELOAD, 0);
if hConnect <> nil then begin
InternetReadFile(hConnect, @Buffer, SizeOf(Buffer), BytesRead);
WriteLn(Copy(Buffer, 1, BytesRead));
InternetCloseHandle(hConnect);
end;
InternetCloseHandle(hInternet);
end;
end;
Sistem proxy'lerini kullanma
hInternet := InternetOpen(
'MyApp',
INTERNET_OPEN_TYPE_PRECONFIG, // sistem ayarlarını kullan
nil,
nil,
0
);
Proxy rotasyonu
type
TProxyInfo = record
Host: string;
Port: Integer;
Username: string;
Password: string;
end;
var
Proxies: array of TProxyInfo;
ProxyIndex: Integer = 0;
function GetNextProxy: TProxyInfo;
begin
Result := Proxies[ProxyIndex mod Length(Proxies)];
Inc(ProxyIndex);
end;
procedure FetchWithRotation(const URL: string);
var
HTTP: TIdHTTP;
Proxy: TProxyInfo;
begin
Proxy := GetNextProxy;
HTTP := TIdHTTP.Create(nil);
try
HTTP.ProxyParams.ProxyServer := Proxy.Host;
HTTP.ProxyParams.ProxyPort := Proxy.Port;
HTTP.ProxyParams.ProxyUsername := Proxy.Username;
HTTP.ProxyParams.ProxyPassword := Proxy.Password;
try
WriteLn(HTTP.Get(URL));
except
on E: Exception do
WriteLn('Error with proxy ', Proxy.Host, ': ', E.Message);
end;
finally
HTTP.Free;
end;
end;
İpuçları
- Indy, Delphi için standart tercihtir, IDE ile birlikte gelir
- Synapse, Free Pascal ve çapraz platform projeleri içindir
- WinINet yalnızca Windows uygulamaları içindir, sistem yığınını kullanır
- SOCKS için Indy TIdSocksInfo veya Synapse SocksType kullanın
- OpenSSL DLL — HTTPS için libeay32.dll ve ssleay32.dll dosyalarını exe'nizin yanına koymayı unutmayın
Sonuç
Delphi/Pascal, proxy'lerle çalışmak için olgun araçlar sunar. Indy; HTTP, SOCKS4, SOCKS5 ve kimlik doğrulamayı destekleyen en kapsamlı çözümdür. Windows'ta basit görevler için WinINet API, harici bağımlılığı olmayan hızlı bir seçenektir.
