dino/libdino/src/service/connection_manager.vala

340 lines
12 KiB
Vala
Raw Normal View History

2017-03-02 15:37:32 +01:00
using Gee;
using Xmpp;
using Dino.Entities;
namespace Dino {
public class ConnectionManager : Object {
2017-03-02 15:37:32 +01:00
public signal void stream_opened(Account account, XmppStream stream);
public signal void stream_attached_modules(Account account, XmppStream stream);
2017-03-02 15:37:32 +01:00
public signal void connection_state_changed(Account account, ConnectionState state);
public signal void connection_error(Account account, ConnectionError error);
2017-03-02 15:37:32 +01:00
public enum ConnectionState {
CONNECTED,
CONNECTING,
DISCONNECTED
}
private HashMap<Account, Connection> connections = new HashMap<Account, Connection>(Account.hash_func, Account.equals_func);
private HashMap<Account, ConnectionError> connection_errors = new HashMap<Account, ConnectionError>(Account.hash_func, Account.equals_func);
Move to GNetworkMonitor (#236) * Move to GNetworkMonitor Dino currently talks to NetworkManager over DBus to know the state of the network. That doesn't work in a Flatpak sandbox by default though, because Flatpak filters DBus communications and only allows a very small set of things to pass (which are known to be safe). Gio provides an API to know the state of the network (and be notified of changes via a signal): GNetworkMonitor. And GNetworkMonitor works both inside a Flatpak sandbox, and in traditional builds. (in Flatpak it uses what we call a "portal", which are the clean, safe way to let apps exit their sandbox) Fixes #235 * Don't check for network connectivity for now The connectivity check really is the correct thing to do: * network_available means that the computer has network routes to "somewhere". That is, it is connected to a router. * connectivity.FULL means that the computer can access "the Internet". That is, if it is behind a router, that router is connected. As a result, only checking for network_available is not correct. Unfortunately, NetworkManager tends to wait a long time before checking for connectivity. As a result, it is possible that a transient network error leaves NetworkManager thinking that network_available is true but connectivity!=FULL, and it will wait several minutes before realizing that the Internet connexion did come back. During that time, apps checking for connectivity (e.g the whole GNOME desktop) will think they don't have access to the Internet, while apps that don't (e.g Firefox) will access the Internet just fine. Users are understandably confused when that happens. Removing the check for connectivity is an acceptable trade-off in the short-term, until this situation is improved on the NetworkManager side. https://bugzilla.gnome.org/show_bug.cgi?id=792240
2018-01-09 20:39:45 +01:00
private NetworkMonitor? network_monitor;
2017-03-02 15:37:32 +01:00
private ModuleManager module_manager;
2017-04-03 15:09:30 +02:00
public string? log_options;
2017-03-02 15:37:32 +01:00
public class ConnectionError {
public enum Source {
CONNECTION,
SASL,
2018-01-04 21:13:44 +01:00
TLS,
STREAM_ERROR
}
2018-01-04 21:13:44 +01:00
public enum Reconnect {
NOW,
LATER,
NEVER
}
public Source source;
public string? identifier;
2018-01-04 21:13:44 +01:00
public Reconnect reconnect_recomendation { get; set; default=Reconnect.NOW; }
public ConnectionError(Source source, string? identifier) {
this.source = source;
this.identifier = identifier;
}
}
2017-03-02 15:37:32 +01:00
private class Connection {
public string uuid { get; set; }
public XmppStream? stream { get; set; }
2017-03-02 15:37:32 +01:00
public ConnectionState connection_state { get; set; default = ConnectionState.DISCONNECTED; }
public bool acked;
public Connection() {
reset.begin();
}
public async void reset() {
acked = false;
if (stream != null) {
stream.detach_modules();
yield stream.disconnect();
}
stream = null;
uuid = Xmpp.random_uuid();
}
public void make_offline() {
Xmpp.Presence.Stanza presence = new Xmpp.Presence.Stanza();
presence.type_ = Xmpp.Presence.Stanza.TYPE_UNAVAILABLE;
if (stream != null) {
stream.get_module(Presence.Module.IDENTITY).send_presence(stream, presence);
}
}
public async void disconnect_account() {
make_offline();
if (stream != null) {
try {
yield stream.disconnect();
} catch (Error e) {
debug("Error disconnecting stream: %s", e.message);
}
}
2017-03-02 15:37:32 +01:00
}
}
private async void on_network_changed(bool state) {
debug(@"on network changed=$(state)");
if (state) {
check_reconnects();
}
else {
make_offline_all();
}
}
2017-03-02 15:37:32 +01:00
public ConnectionManager(ModuleManager module_manager) {
this.module_manager = module_manager;
Move to GNetworkMonitor (#236) * Move to GNetworkMonitor Dino currently talks to NetworkManager over DBus to know the state of the network. That doesn't work in a Flatpak sandbox by default though, because Flatpak filters DBus communications and only allows a very small set of things to pass (which are known to be safe). Gio provides an API to know the state of the network (and be notified of changes via a signal): GNetworkMonitor. And GNetworkMonitor works both inside a Flatpak sandbox, and in traditional builds. (in Flatpak it uses what we call a "portal", which are the clean, safe way to let apps exit their sandbox) Fixes #235 * Don't check for network connectivity for now The connectivity check really is the correct thing to do: * network_available means that the computer has network routes to "somewhere". That is, it is connected to a router. * connectivity.FULL means that the computer can access "the Internet". That is, if it is behind a router, that router is connected. As a result, only checking for network_available is not correct. Unfortunately, NetworkManager tends to wait a long time before checking for connectivity. As a result, it is possible that a transient network error leaves NetworkManager thinking that network_available is true but connectivity!=FULL, and it will wait several minutes before realizing that the Internet connexion did come back. During that time, apps checking for connectivity (e.g the whole GNOME desktop) will think they don't have access to the Internet, while apps that don't (e.g Firefox) will access the Internet just fine. Users are understandably confused when that happens. Removing the check for connectivity is an acceptable trade-off in the short-term, until this situation is improved on the NetworkManager side. https://bugzilla.gnome.org/show_bug.cgi?id=792240
2018-01-09 20:39:45 +01:00
network_monitor = GLib.NetworkMonitor.get_default();
if (network_monitor != null) {
network_monitor.network_changed.connect(on_network_changed);
2017-03-02 15:37:32 +01:00
}
}
public XmppStream? get_stream(Account account) {
if (get_state(account) == ConnectionState.CONNECTED) {
return connections[account].stream;
2017-03-02 15:37:32 +01:00
}
return null;
}
public ConnectionState get_state(Account account) {
if (connections.has_key(account)){
return connections[account].connection_state;
2017-03-02 15:37:32 +01:00
}
return ConnectionState.DISCONNECTED;
}
public ConnectionError? get_error(Account account) {
if (connection_errors.has_key(account)) {
return connection_errors[account];
}
return null;
}
2018-03-10 19:46:08 +01:00
public Collection<Account> get_managed_accounts() {
return connections.keys;
2017-03-02 15:37:32 +01:00
}
2019-03-15 20:56:19 +01:00
public void connect_account(Account account) {
if (!connections.has_key(account)) {
connections[account] = new Connection();
connect_stream.begin(account);
2017-03-02 15:37:32 +01:00
} else {
check_reconnect(account);
}
}
public void make_offline_all() {
2018-09-16 13:54:47 +02:00
foreach (Account account in connections.keys) {
make_offline(account);
}
}
private void make_offline(Account account) {
connections[account].make_offline();
2017-03-02 15:37:32 +01:00
change_connection_state(account, ConnectionState.DISCONNECTED);
}
public async void disconnect_account(Account account) {
if (connections.has_key(account)) {
2018-09-16 13:54:47 +02:00
make_offline(account);
2021-04-09 23:59:03 +02:00
connections[account].disconnect_account.begin();
connections.unset(account);
2017-03-02 15:37:32 +01:00
}
}
private async void connect_stream(Account account, string? resource = null) {
if (!connections.has_key(account)) return;
debug("[%s] (Maybe) Establishing a new connection", account.bare_jid.to_string());
connection_errors.unset(account);
2017-03-02 15:37:32 +01:00
if (resource == null) resource = account.resourcepart;
XmppStreamResult stream_result;
change_connection_state(account, ConnectionState.CONNECTING);
stream_result = yield Xmpp.establish_stream(account.bare_jid, module_manager.get_modules(account, resource), log_options,
(peer_cert, errors) => {
change_connection_state(account, ConnectionState.DISCONNECTED);
return on_invalid_certificate(account.domainpart, peer_cert, errors); }
);
connections[account].stream = stream_result.stream;
if (stream_result.stream == null) {
if (stream_result.tls_errors != null) {
set_connection_error(account, new ConnectionError(ConnectionError.Source.TLS, null) { reconnect_recomendation=ConnectionError.Reconnect.NEVER});
return;
}
debug("[%s] Could not connect", account.bare_jid.to_string());
change_connection_state(account, ConnectionState.DISCONNECTED);
return;
}
XmppStream stream = stream_result.stream;
2019-03-15 20:56:19 +01:00
debug("[%s] New connection with resource %s: %p", account.bare_jid.to_string(), resource, stream);
2017-03-02 15:37:32 +01:00
2017-08-12 23:14:50 +02:00
stream.attached_modules.connect((stream) => {
stream_attached_modules(account, stream);
2017-03-02 15:37:32 +01:00
change_connection_state(account, ConnectionState.CONNECTED);
// stream.get_module(Xep.Muji.Module.IDENTITY).join_call(stream, new Jid("test@muc.poez.io"), true);
2017-03-02 15:37:32 +01:00
});
2018-08-19 19:56:46 +02:00
stream.get_module(Sasl.Module.IDENTITY).received_auth_failure.connect((stream, node) => {
2018-01-04 21:13:44 +01:00
set_connection_error(account, new ConnectionError(ConnectionError.Source.SASL, null));
});
string connection_uuid = connections[account].uuid;
stream.received_node.connect(() => {
if (connections[account].uuid == connection_uuid) {
} else {
warning("Got node for outdated connection");
}
});
2017-03-02 15:37:32 +01:00
stream_opened(account, stream);
2017-11-11 21:29:13 +01:00
try {
yield stream.loop();
2017-11-11 21:29:13 +01:00
} catch (Error e) {
debug("[%s %p] Connection error: %s", account.bare_jid.to_string(), stream, e.message);
2017-11-11 21:29:13 +01:00
change_connection_state(account, ConnectionState.DISCONNECTED);
connections[account].reset();
2017-11-11 21:29:13 +01:00
StreamError.Flag? flag = stream.get_flag(StreamError.Flag.IDENTITY);
if (flag != null) {
2019-03-15 20:56:19 +01:00
warning(@"[%s %p] Stream Error: %s", account.bare_jid.to_string(), stream, flag.error_type);
set_connection_error(account, new ConnectionError(ConnectionError.Source.STREAM_ERROR, flag.error_type));
2017-11-11 21:29:13 +01:00
2019-03-15 20:56:19 +01:00
if (flag.resource_rejected) {
connect_stream.begin(account, account.resourcepart + "-" + random_uuid());
return;
2018-03-10 19:46:08 +01:00
}
2019-03-15 20:56:19 +01:00
}
ConnectionError? error = connection_errors[account];
if (error != null && error.source == ConnectionError.Source.SASL) {
2018-03-10 19:46:08 +01:00
return;
}
2019-03-15 20:56:19 +01:00
check_reconnect(account);
2017-03-02 15:37:32 +01:00
}
}
private void check_reconnects() {
foreach (Account account in connections.keys) {
2017-03-02 15:37:32 +01:00
check_reconnect(account);
}
}
private void check_ping_reconnect(Xmpp.Xep.Ping.Module identity,
XmppStream stream, Account account) {
identity.send_ping.begin(stream, account.bare_jid.domain_jid, () => {
if (connections[account].stream != stream) return;
connections[account].acked = true;
change_connection_state(account, ConnectionState.CONNECTED);
});
Timeout.add_seconds(10, () => {
if (!connections.has_key(account)) return false;
if (connections[account].stream != stream) return false;
if (connections[account].acked) return false;
// Reconnect. Nothing gets through the stream.
debug("[%s %p] Ping timeouted. Reconnecting", account.bare_jid.to_string(), stream);
change_connection_state(account, ConnectionState.DISCONNECTED);
connections[account].reset();
connect_stream.begin(account);
return false;
});
}
private void check_reconnect(Account account, bool directly_reconnect = false) {
if (!connections.has_key(account)) return;
var cancellable = new Cancellable();
debug(@"account.domainpart=$(account.domainpart)");
2017-03-02 15:37:32 +01:00
try {
var address = new GLib.NetworkAddress(account.domainpart, 5222);
var reachable = network_monitor.can_reach(address, cancellable);
debug(@"can-reach: $(reachable)");
if (reachable) {
Xmpp.Xep.Ping.Module? identity = null;
if (connections[account].connection_state == ConnectionState.CONNECTING) return;
XmppStream? stream = connections[account].stream;
connections[account].acked = false;
if (stream != null
&& (identity = stream.get_module(Xep.Ping.Module.IDENTITY)) != null) {
change_connection_state(account, ConnectionState.CONNECTING);
check_ping_reconnect(identity, stream, account);
}
else {
change_connection_state(account, ConnectionState.DISCONNECTED);
connections[account].reset();
connect_stream.begin(account);
}
2017-03-02 15:37:32 +01:00
}
} catch (Error e) {
print ("Error: %s\n", e.message);
2017-03-02 15:37:32 +01:00
}
2017-03-02 15:37:32 +01:00
}
private void change_connection_state(Account account, ConnectionState state) {
if (connections.has_key(account)) {
connections[account].connection_state = state;
connection_state_changed(account, state);
}
}
2018-01-04 21:13:44 +01:00
private void set_connection_error(Account account, ConnectionError error) {
connection_errors[account] = error;
connection_error(account, error);
2017-03-02 15:37:32 +01:00
}
public static bool on_invalid_certificate(string domain, TlsCertificate peer_cert, TlsCertificateFlags errors) {
if (domain.has_suffix(".onion") && errors == TlsCertificateFlags.UNKNOWN_CA) {
// It's barely possible for .onion servers to provide a non-self-signed cert.
// But that's fine because encryption is provided independently though TOR.
warning("Accepting TLS certificate from unknown CA from .onion address %s", domain);
return true;
}
return false;
}
2017-03-02 15:37:32 +01:00
}
}