Skip to content

Instantly share code, notes, and snippets.

@prof7bit
Created June 4, 2012 10:31
Show Gist options
  • Save prof7bit/2867664 to your computer and use it in GitHub Desktop.
Save prof7bit/2867664 to your computer and use it in GitHub Desktop.
socks4a - not complicated at all :-)
procedure TBuddy.InitiateConnect;
begin
WriteLn('TBuddy.InitiateConnect() ' + ID);
with FLnetClient do begin
OnConnect := @Self.OnProxyConnect;
OnReceive := @Self.OnProxyReceive;
OnDisconnect := @Self.OnProxyDisconect;
OnError := @Self.OnProxyError;
end;
FLnetClient.Connect(Client.TorHost, Client.TorPort);
FConnecting := True;
end;
procedure TBuddy.OnProxyConnect(ASocket: TLSocket);
var
REQ: String;
begin
writeln('TBuddy.OnProxyConnect() ', ID, ' connected to Tor, sending SOCKS4 request');
SetLength(REQ, 8);
REQ[1] := #4; // Socks 4
REQ[2] := #1; // CONNECT command
PWord(@REQ[3])^ := ShortHostToNet(11009); // TorChat port
PDWord(@REQ[5])^ := HostToNet(1); // address '0.0.0.1' means: Socks 4a
REQ := REQ + 'TorChat' + #0;
REQ := REQ + ID + '.onion' + #0;
ASocket.Send(REQ[1], Length(REQ));
end;
procedure TBuddy.OnProxyReceive(ASocket: TLSocket);
var
ANS: String;
Num: Integer;
Err: String;
C : IHiddenConnection;
begin
NUM := ASocket.GetMessage(ANS);
if (Num = 8) and (ANS[2] = #90) then begin
writeln('TBuddy.OnProxyReceive() ', ID, ' socks4a connection established');
// remove the event methods, THiddenConnection will install its own
FLnetClient.OnReceive := nil;
FLnetClient.OnDisconnect := nil;
FLnetClient.OnError := nil;
C := THiddenConnection.Create(Client, ASocket, Self);
SetOutgoing(C);
FConnecting := False;
end
else begin
if Num = 8 then
Err := IntToStr(Ord((ANS[2])))
else
Err := 'wrong answer from proxy (' + IntToStr(Num) + ' bytes)';
writeln('TBuddy.OnProxyReceive() ', ID, ' socks4a connection failed: ', Err);
ASocket.Disconnect();
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment