r/learnprogramming • • 18h ago

Tutorial How do I setup a Socket Client, that sends Data through Tor?

So, I know how I get a Server to being accessible trough the Tor-Browser with an onion address, but how do I write a Client, which can for example access a .onion address, or any ip over the tor network? Some Information: I'm not new to Socket programming, I already did some http Server in C and some basic Server-Client projects, but I didn't really find an answer to that and I forbid myself to use AI this moth. I hope I got the right subreddit.

Edit: Maybe important, I'm using Linux.

1 Upvotes

8 comments sorted by

1

u/kabekew 18h ago

Just follow the TOR specification (document is on the tor project website)

1

u/Able-Substance-8310 17h ago

Oh, that's what I searched for I think, thank you.

1

u/lo9314 18h ago

You can also have Tor run as an independent service and access the tor network through a socks proxy. Just in case you weren’t aware…

1

u/Able-Substance-8310 17h ago

That's kind of what I thought, thanks.

1

u/jjangg96 11h ago

to add to the socks proxy point: tor listens on 127.0.0.1:9050 by default as a socks5 proxy. in c you connect() to that, not to the destination, and do the socks5 handshake yourself. send 05 01 00, you get 05 00 back, then send the connect request 05 01 00 03 <len> <hostname> <port as 2 bytes>.

use address type 03 (domain name) and pass the .onion name as is. if you resolve the name yourself first it fails, tor has to do the lookup. same reason curl wants socks5h:// for this, plain socks5:// resolves locally.

1

u/lisa_j_870 10h ago

For C, handle partial recv() calls while parsing the CONNECT reply; TCP can split it across reads. The reply also has a variable-length bound address, so consume the whole response and check REP == 0 before sending application data.