ipsn / go-libtor

Self-contained Tor from Go
BSD 3-Clause "New" or "Revised" License
544 stars 46 forks source link

Restart Tor #20

Closed aayushsinha44 closed 4 years ago

aayushsinha44 commented 4 years ago

I am using tor and generating onion url and associating it with a http server. Whenever my internet connection is lost the tor circuits break. I want to restart everything at this point. For doing so, i stop my http server and close my onion and tor by tor.Close() and onion.Close(). After that i try to repeat all the process from start to start tor, onion and http service. At this moment i am unable to connect to tor. Everyting context time out occurs. If i close the program and re-run it seems to work fine. But when i try to restart within the running program it doesnot works.

Is there some issue of tor.Close() or onion.Close()? I am unable to figure this out.

How to restart existing running tor service without stop existing running program?

karalabe commented 4 years ago

I honestly haven't the slightest clue what might be wrong, but this is a use case I want to actively use myself too in the following days, so I'll get back to you if I figure anything out.

karalabe commented 4 years ago

Just hit this myself too. Funky, will try to figure it out. Seems that the tor bootstrap never finishes when restarting. At least it never gives back control to the caller, even though it says "100%" booted. I've tried nuking the datadir and restarting like that and it also doesn't seem to work, so it might be something off either in the lib or in the control lib.

karalabe commented 4 years ago

I think the (or an) issue is in the controller lib: https://github.com/cretz/bine/blob/62912bff1c531f4cb731cc86d38e78bc94aea2fd/tor/tor.go#L370

Onion creation uses tor.EnableNetwork(ctx, true), blocking until the circuit bootstraps itself. However, if the network was enabled already, the above line bail out of the method, even if waiting was requested and the circuit is not yet done. Not sure if this is the cause for the hang, but it's a weird race. Will try to fix and see.

karalabe commented 4 years ago

I knew until now that the Tor library is not capable of running two instances in the same process. Unfortunately it also seems that it's not capable of restarting itself without restarting the entire process. My hunch is that some global variable is ruined on shutdown.

Either way, Tor does support enabling and siabling networking, so you can get away with not killing the in-process Tor, just shutting the network off and back on and it should work:

You can enable networking on request via.

tor.EnableNetwork(context.Background(), false)

Turning off the networking is not exposed in the bine library, but you can do it via:

tor.Control.SetConf(control.KeyVals("DisableNetwork", "1")...)

The lack of the latter is tracked in https://github.com/cretz/bine/issues/41