From 0c3037a1fced02cd51b69fc6a30d53e63f8e4ea7 Mon Sep 17 00:00:00 2001 From: Pablo Duboue Date: Sat, 8 Dec 2012 21:26:45 -0500 Subject: [PATCH] First pass at having TorChat2 running with an external tor instance. (This is part of the solution for Issue #12, running TorChat pidgin plugin under TAILS.) It uses two configuration options: "StandaloneTor" : 1, "HiddenServiceName" : "the-hidden-service-name" (The hidden service name is the content of the hostname file for the hidden service without the .onion extension) The tor instance has to include the following entries: HiddenServiceDir /path/to/hidden-service-folder/ HiddenServicePort 11009 127.0.0.1:11009 LongLivedPorts 11009 SocksListenAddress 127.0.0.1:11109 What is missing: being able to specify the ports for the hidden service and socks ports in the UI and maybe some script to create the hidden service first time it is being run. I tested this running under pidgin 2.10.6-2, tor 0.2.3.25-1 and torchat 0.9.9.550-2. --- src/core/tc_tor.pas | 54 +++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/core/tc_tor.pas b/src/core/tc_tor.pas index fbaa0b97..179e81fe 100644 --- a/src/core/tc_tor.pas +++ b/src/core/tc_tor.pas @@ -52,6 +52,7 @@ TTor = class(TProcess) FClient: IClient; FClientListenPort: DWord; FSocksPort: DWord; + FStandaloneTor: Boolean; procedure GenerateTorrc; procedure StartTorProcess; procedure KillIfAlreadyRunning; @@ -77,7 +78,10 @@ constructor TTor.Create(AOwner: TComponent; AClient: IClient; FClient := AClient; FClientListenPort := AClientPort; FSocksPort := FClient.Config.TorPort; - StartTorProcess; + FStandaloneTor := (FClient.Config.GetString('StandaloneTor') = '1') or + (FClient.Config.GetString('StandaloneTor') = '"true"'); + if not FStandaloneTor then + StartTorProcess; end; destructor TTor.Destroy; @@ -259,35 +263,41 @@ function TTor.HiddenServiceName: String; const OnionLength = 16; begin - FileName := ConcatPaths([CurrentDirectory, 'hidden_service', 'hostname']); - SetLength(Result, OnionLength); - try - HostnameFile := TFileStream.Create(FileName, fmOpenRead); - if HostnameFile.Read(Result[1], OnionLength) < OnionLength then + if FStandaloneTor then + Result := FClient.Config.GetString('HiddenServiceName') + else begin + FileName := ConcatPaths([CurrentDirectory, 'hidden_service', 'hostname']); + SetLength(Result, OnionLength); + try + HostnameFile := TFileStream.Create(FileName, fmOpenRead); + if HostnameFile.Read(Result[1], OnionLength) < OnionLength then + Result := ''; + except Result := ''; - except - Result := ''; + end; + if Assigned(HostnameFile) then FreeAndNil(HostnameFile); end; - if Assigned(HostnameFile) then FreeAndNil(HostnameFile); end; procedure TTor.CleanShutdown; begin - {$ifdef unix} - FpKill(Handle, SIGINT); // Tor will exit cleanly on SIGINT - {$else} - {$ifdef windows} - // there are no signals in windows, they also forgot to - // implement any other mechanism for sending Ctrl-C to - // another process. We have to kill it. - TerminateProcess(Handle, 0); - DeleteFile(ConcatPaths([CurrentDirectory, 'tor.pid'])); + if not FStandaloneTor then begin + {$ifdef unix} + FpKill(Handle, SIGINT); // Tor will exit cleanly on SIGINT {$else} - Terminate(0); - DeleteFile(ConcatPaths([CurrentDirectory, 'tor.pid'])); + {$ifdef windows} + // there are no signals in windows, they also forgot to + // implement any other mechanism for sending Ctrl-C to + // another process. We have to kill it. + TerminateProcess(Handle, 0); + DeleteFile(ConcatPaths([CurrentDirectory, 'tor.pid'])); + {$else} + Terminate(0); + DeleteFile(ConcatPaths([CurrentDirectory, 'tor.pid'])); + {$endif} {$endif} - {$endif} - RemovePortFromList(FSocksPort); + RemovePortFromList(FSocksPort); + end; end; end.