Skip to content

Instantly share code, notes, and snippets.

@prof7bit
Created July 10, 2012 22:07
Show Gist options
  • Save prof7bit/3086524 to your computer and use it in GitHub Desktop.
Save prof7bit/3086524 to your computer and use it in GitHub Desktop.
Socks 4a
procedure TBuddy.OnProxyConnect(ASocket: TLSocket);
type
TSocksReqestHeader = packed record
Typ : Byte;
Cmd : Byte;
Port : Word;
Addr4 : DWord;
end;
var
ReqHeader: TSocksReqestHeader;
Req: String;
begin
WriteLn('<~~~~ ', ID, ' connected to Tor, sending Socks4a request');
with ReqHeader do begin
Typ := 4; // Socks 4
Cmd := 1; // CONNECT
Port := ShortHostToNet(11009); // TorChat port
Addr4 := HostToNet(1); // address '0.0.0.1' means: Socks 4a
end;
SetLength(Req, SizeOf(TSocksReqestHeader));
Move(ReqHeader, Req[1], SizeOf(TSocksReqestHeader));
Req := Req + 'TorChat' + #0;
Req := Req + ID + '.onion' + #0;
ASocket.Send(Req[1], Length(Req));
end;
procedure TBuddy.OnProxyReceive(ASocket: TLSocket);
type
PSocks4Response = ^TSocks4Response;
TSocks4Response = packed record
NullByte : Byte;
Status : Byte;
Port : Word;
Addr : DWord;
end;
var
Buf: String;
Response: PSocks4Response;
RightSize: Boolean;
Num: Integer;
Err: String;
C : IHiddenConnection;
begin
Num := ASocket.GetMessage(Buf);
Response := Pointer(Buf);
RightSize := (Num = SizeOf(TSocks4Response));
if RightSize and (Response.Status = 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 RightSize then
Err := SF('%d', [Response.Status])
else
Err := SF('wrong answer from proxy (%d bytes)', [Num]);
WriteLn('<~/~~ ', 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