torsocks command not working on Mac

torsocks commands doesn't work on Mac. For example if I run:

torsocks http https://check.torproject.org/api/ip

It returns:

1546862950 PERROR torsocks[6434]: socks5 libc connect: Connection refused (in socks5_connect() at socks5.c:202) 1546862950 PERROR torsocks[6434]: socks5 libc connect: Connection refused (in socks5_connect() at socks5.c:202)

http: error: ConnectionError: HTTPSConnectionPool(host='check.torproject.org', port=443): Max retries exceeded with url: /api/ip (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 4] Non-recoverable failure in name resolution',)) while doing GET request to URL: https://check.torproject.org/api/ip

The url is valid because when I do curl https://check.torproject.org/api/ip, it returns a valid response.


I don't know much about Tor but I managed to get rid of the error by starting tor beforehand:

  • brew install tor torsocks
  • cp /usr/local/etc/tor/torrc.sample /usr/local/etc/tor/torrc
  • Uncomment ControlPort 9051 and CookieAuthentication 1 in /usr/local/etc/tor/torrc and change CookieAuthentication 1 to CookieAuthentication 0.
  • brew services start tor

I'm using it successfully as follows:

  • brew install openssl
  • torify /usr/local/opt/openssl/bin/openssl s_client -quiet -crlf -connect example.com:443