Skip to content

Commit

Permalink
VENOM-302: Add system proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
naxuroqa committed Apr 8, 2018
1 parent e8ef547 commit ced9296
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 40 deletions.
106 changes: 73 additions & 33 deletions src/tox/ToxSession.vala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace Venom {
private ToxSessionIO iohandler;
private GLib.HashTable<uint32, IContact> friends;

public ToxSessionImpl(ToxSessionIO iohandler, IDhtNodeDatabase node_database, ISettingsDatabase settings_database, ILogger logger) {
public ToxSessionImpl(ToxSessionIO iohandler, IDhtNodeDatabase node_database, ISettingsDatabase settings_database, ILogger logger) throws Error {
this.dht_node_database = node_database;
this.settings_database = settings_database;
this.logger = logger;
Expand All @@ -148,26 +148,20 @@ namespace Venom {
}

if (settings_database.enable_proxy) {
if (settings_database.enable_custom_proxy) {
options.proxy_type = ProxyType.SOCKS5;
options.proxy_host = settings_database.custom_proxy_host;
options.proxy_port = (uint16) settings_database.custom_proxy_port;
} else {
//FIXME system proxy currently not supported
}
init_proxy(options);
}

// create handle
var error = ToxCore.ErrNew.OK;
handle = new ToxCore.Tox(options, ref error);
if (error == ErrNew.PROXY_BAD_HOST || error == ErrNew.PROXY_BAD_PORT || error == ErrNew.PROXY_NOT_FOUND) {
logger.e("Proxy could not be used: " + error.to_string());
options.proxy_type = ProxyType.NONE;
handle = new ToxCore.Tox(options, ref error);
}
if (error != ToxCore.ErrNew.OK) {
logger.f("Could not create tox instance: " + error.to_string());
assert_not_reached();
var message = "Proxy could not be used: " + error.to_string();
logger.f(message);
throw new ToxError.GENERIC(message);
} else if (error != ToxCore.ErrNew.OK) {
var message = "Could not create tox instance: " + error.to_string();
logger.f(message);
throw new ToxError.GENERIC(message);
}

handle.callback_self_connection_status(on_self_connection_status_cb);
Expand Down Expand Up @@ -202,6 +196,70 @@ namespace Venom {
logger.d("ToxSession destroyed.");
}

private void init_proxy(ToxCore.Options options) {
if (settings_database.enable_custom_proxy) {
options.udp_enabled = false;
options.proxy_type = ProxyType.SOCKS5;
options.proxy_host = settings_database.custom_proxy_host;
options.proxy_port = (uint16) settings_database.custom_proxy_port;
logger.i("Using custom socks5 proxy: socks5://%s:%u.".printf(options.proxy_host, options.proxy_port));
return;
} else {
string[] proxy_strings = {};
ProxyResolver proxy_resolver = ProxyResolver.get_default();
try {
proxy_strings = proxy_resolver.lookup("socks://tox.im");
} catch (Error e) {
logger.e("Error when looking up proxy settings: " + e.message);
return;
}

Regex proxy_regex = null;
try {
proxy_regex = new GLib.Regex("^(?P<protocol>socks5)://((?P<user>[^:]*)(:(?P<password>.*))?@)?(?P<host>.*):(?P<port>.*)");
} catch (GLib.Error e) {
logger.f("Error creating tox uri regex: " + e.message);
return;
}

foreach (var proxy in proxy_strings) {
if (proxy.has_prefix("socks5:")) {
GLib.MatchInfo info = null;
if (proxy_regex != null && proxy_regex.match(proxy, 0, out info)) {
options.udp_enabled = false;
options.proxy_type = ProxyType.SOCKS5;
options.proxy_host = info.fetch_named("host");
options.proxy_port = (uint16) int.parse(info.fetch_named("port"));
logger.i("Using socks5 proxy found in system settings: socks5://%s:%u.".printf(options.proxy_host, options.proxy_port));
return;
} else {
logger.i("socks5 proxy does not match regex: " + proxy);
}
}
}

logger.i("No usable proxy found in system settings, connecting directly.");
}
}

private void init_dht_nodes() {
var nodeFactory = new DhtNodeFactory();
dht_nodes = dht_node_database.getDhtNodes(nodeFactory);
logger.d("Items in dht node list: %u".printf(dht_nodes.length()));
if (dht_nodes.length() == 0) {
logger.d("Node database empty, populating from static database.");
var nodeDatabase = new JsonWebDhtNodeDatabase(logger);
var nodes = nodeDatabase.getDhtNodes(nodeFactory);
foreach (var node in nodes) {
dht_node_database.insertDhtNode(node.pub_key, node.host, node.port, node.is_blocked, node.maintainer, node.location);
}
dht_nodes = dht_node_database.getDhtNodes(nodeFactory);
if (dht_nodes.length() == 0) {
logger.e("Node initialisation from static database failed.");
}
}
}

public virtual unowned GLib.HashTable<uint32, IContact> get_friends() {
return friends;
}
Expand Down Expand Up @@ -630,24 +688,6 @@ namespace Venom {
return ret;
}

private void init_dht_nodes() {
var nodeFactory = new DhtNodeFactory();
dht_nodes = dht_node_database.getDhtNodes(nodeFactory);
logger.d("Items in dht node list: %u".printf(dht_nodes.length()));
if (dht_nodes.length() == 0) {
logger.d("Node database empty, populating from static database.");
var nodeDatabase = new JsonWebDhtNodeDatabase(logger);
var nodes = nodeDatabase.getDhtNodes(nodeFactory);
foreach (var node in nodes) {
dht_node_database.insertDhtNode(node.pub_key, node.host, node.port, node.is_blocked, node.maintainer, node.location);
}
dht_nodes = dht_node_database.getDhtNodes(nodeFactory);
if (dht_nodes.length() == 0) {
logger.e("Node initialisation from static database failed.");
}
}
}

public void @lock() {
mutex.@lock();
}
Expand Down
3 changes: 2 additions & 1 deletion src/tox/ToxSessionThread.vala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ namespace Venom {
}
logger.d(" %s".printf(dht_node.to_string()));
var bootstrap_err = ToxCore.ErrBootstrap.OK;
if (!session.handle.bootstrap(dht_node.host, (uint16) dht_node.port, Tools.hexstring_to_bin(dht_node.pub_key), ref bootstrap_err)) {
session.handle.bootstrap(dht_node.host, (uint16) dht_node.port, Tools.hexstring_to_bin(dht_node.pub_key), ref bootstrap_err);
if (bootstrap_err != ToxCore.ErrBootstrap.OK) {
logger.e("Connecting to node %s failed: %s".printf(dht_node.to_string(), bootstrap_err.to_string()));
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/view/ApplicationWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,26 @@ namespace Venom {
notification_listener = new NotificationListenerImpl(logger);
notification_listener.clear_notifications();

var session_io = new ToxSessionIOImpl(logger);
session = new ToxSessionImpl(session_io, node_database, settings_database, logger);
session_listener = new ToxAdapterListenerImpl(logger, user_info);
session_listener.attach_to_session(session);
friend_listener = new ToxAdapterFriendListenerImpl(logger, contacts, conversations, notification_listener);
friend_listener.attach_to_session(session);
conference_listener = new ToxAdapterConferenceListenerImpl(logger, contacts, conversations, notification_listener);
conference_listener.attach_to_session(session);
filetransfer_listener = new ToxAdapterFiletransferListenerImpl(logger, transfers, notification_listener);
filetransfer_listener.attach_to_session(session);

settings_database.bind_property("enable-send-typing", friend_listener, "show-typing", BindingFlags.SYNC_CREATE);
settings_database.bind_property("enable-urgency-notification", notification_listener, "show-notifications", BindingFlags.SYNC_CREATE);
settings_database.bind_property("enable-tray", status_icon, "visible", BindingFlags.SYNC_CREATE);

try {
var session_io = new ToxSessionIOImpl(logger);
session = new ToxSessionImpl(session_io, node_database, settings_database, logger);
session_listener.attach_to_session(session);
friend_listener.attach_to_session(session);
conference_listener.attach_to_session(session);
filetransfer_listener.attach_to_session(session);
} catch (Error e) {
logger.e("Could not create tox instance: " + e.message);
}

status_icon.activate.connect(present);
delete_event.connect(on_delete_event);
focus_in_event.connect(on_focus_in_event);
Expand Down

0 comments on commit ced9296

Please sign in to comment.