Skip to content

Commit

Permalink
Use bundled torrc
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Dec 3, 2021
1 parent 9fb6ea9 commit 11d338a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 27 deletions.
11 changes: 11 additions & 0 deletions browser/tor/brave_tor_client_updater_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ IN_PROC_BROWSER_TEST_F(BraveTorClientUpdaterTest, TorClientInstalls) {
base::FilePath executable_path =
g_brave_browser_process->tor_client_updater()->GetExecutablePath();
ASSERT_TRUE(PathExists(executable_path));
base::FilePath torrc_path =
g_brave_browser_process->tor_client_updater()->GetTorrcPath();
ASSERT_TRUE(PathExists(torrc_path));
}

// Load the Tor client updater extension and verify that we can launch
Expand All @@ -131,8 +134,16 @@ IN_PROC_BROWSER_TEST_F(BraveTorClientUpdaterTest, TorClientLaunches) {
base::FilePath executable_path =
g_brave_browser_process->tor_client_updater()->GetExecutablePath();
ASSERT_TRUE(PathExists(executable_path));
base::FilePath torrc_path =
g_brave_browser_process->tor_client_updater()->GetTorrcPath();
ASSERT_TRUE(PathExists(torrc_path));

base::CommandLine cmd_line(executable_path);
cmd_line.AppendArg("--ignore-missing-torrc");
cmd_line.AppendArg("-f");
cmd_line.AppendArgPath(torrc_path);
cmd_line.AppendArg("--defaults-torrc");
cmd_line.AppendArgPath(torrc_path);
base::Process tor_client_process =
base::LaunchProcess(cmd_line, base::LaunchOptions());
ASSERT_TRUE(tor_client_process.IsValid());
Expand Down
1 change: 1 addition & 0 deletions components/services/tor/public/interfaces/tor.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "mojo/public/mojom/base/file_path.mojom";

struct TorConfig {
mojo_base.mojom.FilePath binary_path;
mojo_base.mojom.FilePath torrc_path;
mojo_base.mojom.FilePath tor_data_path;
mojo_base.mojom.FilePath tor_watch_path;
};
Expand Down
10 changes: 2 additions & 8 deletions components/services/tor/tor_launcher_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ void TorLauncherImpl::Launch(mojom::TorConfigPtr config,
base::CommandLine args(config->binary_path);
args.AppendArg("--ignore-missing-torrc");
args.AppendArg("-f");
args.AppendArg("/nonexistent");
args.AppendArgPath(config->torrc_path);
args.AppendArg("--defaults-torrc");
args.AppendArg("/nonexistent");
args.AppendArg("--SocksPort");
args.AppendArg("auto");
args.AppendArgPath(config->torrc_path);
base::FilePath tor_data_path = config->tor_data_path;
if (!tor_data_path.empty()) {
if (!base::DirectoryExists(tor_data_path))
Expand All @@ -75,12 +73,8 @@ void TorLauncherImpl::Launch(mojom::TorConfigPtr config,
base::CreateDirectory(tor_watch_path_);
args.AppendArg("--pidfile");
args.AppendArgPath(tor_watch_path_.AppendASCII("tor.pid"));
args.AppendArg("--controlport");
args.AppendArg("auto");
args.AppendArg("--controlportwritetofile");
args.AppendArgPath(tor_watch_path_.AppendASCII("controlport"));
args.AppendArg("--cookieauthentication");
args.AppendArg("1");
args.AppendArg("--cookieauthfile");
args.AppendArgPath(tor_watch_path_.AppendASCII("control_auth_cookie"));
}
Expand Down
43 changes: 27 additions & 16 deletions components/tor/brave_tor_client_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,30 @@ namespace tor {

namespace {

base::FilePath InitExecutablePath(const base::FilePath& install_dir) {
std::pair<base::FilePath, base::FilePath> InitTorPath(
const base::FilePath& install_dir) {
base::FilePath executable_path;
base::FilePath torrc_path;
base::FileEnumerator traversal(install_dir, false,
base::FileEnumerator::FILES,
FILE_PATH_LITERAL("tor-*"));
for (base::FilePath current = traversal.Next(); !current.empty();
current = traversal.Next()) {
base::FileEnumerator::FileInfo file_info = traversal.GetInfo();
if (!RE2::FullMatch(file_info.GetName().MaybeAsASCII(),
"tor-\\d+\\.\\d+\\.\\d+\\.\\d+-\\w+-brave-\\d+"))
continue;
executable_path = current;
break;
if (RE2::FullMatch(file_info.GetName().MaybeAsASCII(),
"tor-\\d+\\.\\d+\\.\\d+\\.\\d+-\\w+-brave-\\d+"))
executable_path = current;
else if (file_info.GetName().MaybeAsASCII() == "tor-torrc")
torrc_path = current;

if (!executable_path.empty() && !torrc_path.empty())
break;
}

if (executable_path.empty()) {
LOG(ERROR) << "Failed to locate Tor client executable in "
if (executable_path.empty() || torrc_path.empty()) {
LOG(ERROR) << "Failed to locate Tor client executable or torrc in "
<< install_dir.value().c_str();
return base::FilePath();
return std::make_pair(base::FilePath(), base::FilePath());
}

#if defined(OS_POSIX)
Expand All @@ -57,11 +62,11 @@ base::FilePath InitExecutablePath(const base::FilePath& install_dir) {
if (!base::SetPosixFilePermissions(executable_path, 0755)) {
LOG(ERROR) << "Failed to set executable permission on "
<< executable_path.value().c_str();
return base::FilePath();
return std::make_pair(base::FilePath(), base::FilePath());
}
#endif // defined(OS_POSIX)

return executable_path;
return std::make_pair(executable_path, torrc_path);
}

void DeleteDir(const base::FilePath& path) {
Expand Down Expand Up @@ -169,16 +174,22 @@ void BraveTorClientUpdater::RemoveObsoleteFiles() {
task_runner_->PostTask(FROM_HERE, base::BindOnce(&DeleteFile, tor_log));
}

void BraveTorClientUpdater::SetExecutablePath(const base::FilePath& path) {
executable_path_ = path;
void BraveTorClientUpdater::SetTorPath(
const std::pair<base::FilePath, base::FilePath>& paths) {
executable_path_ = paths.first;
torrc_path_ = paths.second;
for (Observer& observer : observers_)
observer.OnExecutableReady(path);
observer.OnExecutableReady(paths.second);
}

base::FilePath BraveTorClientUpdater::GetExecutablePath() const {
return executable_path_;
}

base::FilePath BraveTorClientUpdater::GetTorrcPath() const {
return torrc_path_;
}

base::FilePath BraveTorClientUpdater::GetTorDataPath() const {
DCHECK(!user_data_dir_.empty());
return user_data_dir_.Append(FILE_PATH_LITERAL("tor"))
Expand All @@ -196,8 +207,8 @@ void BraveTorClientUpdater::OnComponentReady(const std::string& component_id,
const std::string& manifest) {
base::PostTaskAndReplyWithResult(
GetTaskRunner().get(), FROM_HERE,
base::BindOnce(&InitExecutablePath, install_dir),
base::BindOnce(&BraveTorClientUpdater::SetExecutablePath,
base::BindOnce(&InitTorPath, install_dir),
base::BindOnce(&BraveTorClientUpdater::SetTorPath,
weak_ptr_factory_.GetWeakPtr()));
}

Expand Down
6 changes: 5 additions & 1 deletion components/tor/brave_tor_client_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <memory>
#include <string>
#include <utility>

#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
Expand Down Expand Up @@ -54,6 +55,7 @@ class BraveTorClientUpdater : public BraveComponent {
void Unregister();
void Cleanup();
base::FilePath GetExecutablePath() const;
base::FilePath GetTorrcPath() const;
base::FilePath GetTorDataPath() const;
base::FilePath GetTorWatchPath() const;
scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() {
Expand All @@ -80,11 +82,13 @@ class BraveTorClientUpdater : public BraveComponent {
static void SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
const std::string& component_base64_public_key);
void SetExecutablePath(const base::FilePath& path);
// <tor executable, torrc>
void SetTorPath(const std::pair<base::FilePath, base::FilePath>&);

scoped_refptr<base::SequencedTaskRunner> task_runner_;
bool registered_;
base::FilePath executable_path_;
base::FilePath torrc_path_;
base::ObserverList<Observer> observers_;
PrefService* local_state_;
base::FilePath user_data_dir_;
Expand Down
1 change: 1 addition & 0 deletions components/tor/tor_launcher_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void TorLauncherFactory::LaunchTorProcess(const tor::mojom::TorConfig& config) {
}

DCHECK(!config.binary_path.empty());
DCHECK(!config.torrc_path.empty());
DCHECK(!config.tor_data_path.empty());
DCHECK(!config.tor_watch_path.empty());
config_ = config;
Expand Down
9 changes: 7 additions & 2 deletions components/tor/tor_profile_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ void TorProfileServiceImpl::OnExecutableReady(const base::FilePath& path) {
}

void TorProfileServiceImpl::LaunchTor() {
tor::mojom::TorConfig config(GetTorExecutablePath(), GetTorDataPath(),
GetTorWatchPath());
tor::mojom::TorConfig config(GetTorExecutablePath(), GetTorrcPath(),
GetTorDataPath(), GetTorWatchPath());
tor_launcher_factory_->LaunchTorProcess(config);
}

Expand All @@ -158,6 +158,11 @@ base::FilePath TorProfileServiceImpl::GetTorExecutablePath() const {
: base::FilePath();
}

base::FilePath TorProfileServiceImpl::GetTorrcPath() const {
return tor_client_updater_ ? tor_client_updater_->GetTorrcPath()
: base::FilePath();
}

base::FilePath TorProfileServiceImpl::GetTorDataPath() const {
return tor_client_updater_ ? tor_client_updater_->GetTorDataPath()
: base::FilePath();
Expand Down
1 change: 1 addition & 0 deletions components/tor/tor_profile_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class TorProfileServiceImpl : public TorProfileService,
void LaunchTor();

base::FilePath GetTorExecutablePath() const;
base::FilePath GetTorrcPath() const;
base::FilePath GetTorDataPath() const;
base::FilePath GetTorWatchPath() const;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SocksPort auto
ControlPort auto
CookieAuthentication 1
3 changes: 3 additions & 0 deletions test/data/tor-client-updater/tor-client-updater-mac/tor-torrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SocksPort auto
ControlPort auto
CookieAuthentication 1
3 changes: 3 additions & 0 deletions test/data/tor-client-updater/tor-client-updater-win/tor-torrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SocksPort auto
ControlPort auto
CookieAuthentication 1

0 comments on commit 11d338a

Please sign in to comment.