Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Use a priority queue to expire all >10min-old tor circuit records.
Browse files Browse the repository at this point in the history
Don't just expire any old entries for the site we're browsing -- that
may leave lots of other ones around in memory.
  • Loading branch information
riastradh-brave authored and darkdh committed Jul 6, 2018
1 parent 5657527 commit fbf2a29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
33 changes: 16 additions & 17 deletions brave/browser/net/proxy_resolution/proxy_config_service_tor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,24 @@ ProxyConfigServiceTor::ProxyConfigServiceTor(
}
std::string proxy_url;
if (tor_proxy_map || username.empty()) {
auto found = tor_proxy_map->find(username);
// Clear expired entries.
const base::Time now = base::Time::Now();
const base::Time deadline = now - kTenMins;
const std::pair<base::Time, std::string>* entry;
while (!tor_proxy_map->queue.empty() &&
(entry = &tor_proxy_map->queue.top(), entry->first < deadline)) {
tor_proxy_map->map.erase(entry->second);
tor_proxy_map->queue.pop();
}
// Look up an entry here.
auto found = tor_proxy_map->map.find(username);
std::string password;
if (found == tor_proxy_map->end()) {
if (found == tor_proxy_map->map.end()) {
password = GenerateNewPassword();
tor_proxy_map->emplace(
username,
std::pair<std::string, base::Time>(password, base::Time::Now()));
tor_proxy_map->map.emplace(username, password);
tor_proxy_map->queue.emplace(now, username);
} else {
base::Time entry_ts = found->second.second;
base::TimeDelta duration = base::Time::Now() - entry_ts;
if (duration > kTenMins) {
tor_proxy_map->erase(username);
password = GenerateNewPassword();
tor_proxy_map->emplace(
username,
std::pair<std::string, base::Time>(password, base::Time::Now()));
} else {
password = found->second.first;
}
password = found->second;
}
proxy_url = std::string(scheme_ + "://" + username + ":" + password +
"@" + host_ + ":" + port_);
Expand All @@ -96,7 +95,7 @@ void ProxyConfigServiceTor::TorSetProxy(
if (!service)
return;
if (new_password && tor_proxy_map)
tor_proxy_map->erase(site_url);
tor_proxy_map->map.erase(site_url);
std::unique_ptr<net::ProxyConfigServiceTor>
config(new ProxyConfigServiceTor(tor_proxy, site_url, tor_proxy_map));
service->ResetConfigService(std::move(config));
Expand Down
7 changes: 5 additions & 2 deletions brave/browser/net/proxy_resolution/proxy_config_service_tor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ const char kSocksProxy[] = "socks5";
// Implementation of ProxyConfigService that returns a tor specific result.
class NET_EXPORT ProxyConfigServiceTor : public ProxyConfigService {
public:
// Used to cache <username, <password, timestamp>> of proxies
typedef std::map<std::string, std::pair<std::string, base::Time>> TorProxyMap;
// Used to cache <username, password> of proxies
struct TorProxyMap {
std::map<std::string, std::string> map;
std::priority_queue<std::pair<base::Time, std::string> > queue;
};

explicit ProxyConfigServiceTor(const std::string& tor_proxy,
const std::string& username,
Expand Down

0 comments on commit fbf2a29

Please sign in to comment.