It is possible to start Tor with the following arguments (or parameters in Tor's config file):
--ControlPort auto --ControlPortWriteToFile <TEMPORARY_FILE_PATH>>
So Tor writes to
<TEMPORARY_FILE_PATH> file the following:
and you just read it in your program. This sounds obvious but if I'm not missing something it is not.
So I think the following steps are necessary to read the correct port number:
- Make sure that
<TEMPORARY_FILE_PATH>does not exist [to eliminate risk that you read some old value].
- Start Tor process with the arguments
- Wait until
<TEMPORARY_FILE_PATH>file exists [the file may exist but its content may not be flushed just yet now]
<TEMPORARY_FILE_PATH>file for reading and read a single line (ending with
- When the line starts with
PORT=, you can parse the port -> success & break from loop.
I'm aware that you can start Tor and just wait few hunder milliseconds, read the file and you'll get most likely the correct port number. However, I want to read always the correct port number.
My question is: What is the recommended (and correct) procedure to read the control port number?