2019-12-28 02:11:51 +00:00
|
|
|
using Gee;
|
2021-03-21 11:41:27 +00:00
|
|
|
using Dino.Entities;
|
2019-12-28 02:11:51 +00:00
|
|
|
using Xmpp;
|
2021-03-21 11:41:27 +00:00
|
|
|
using Xmpp.Xep;
|
2019-12-28 02:11:51 +00:00
|
|
|
|
2021-03-21 11:41:27 +00:00
|
|
|
private extern const size_t NICE_ADDRESS_STRING_LEN;
|
2019-12-28 02:11:51 +00:00
|
|
|
|
2021-03-21 11:41:27 +00:00
|
|
|
public class Dino.Plugins.Ice.Plugin : RootInterface, Object {
|
2024-04-18 14:41:55 +00:00
|
|
|
private const int64 delay_min = 300; // 10mn
|
|
|
|
private const int64 delay_max = (int64) uint.MAX;
|
|
|
|
|
|
|
|
private class TimerPayload {
|
|
|
|
public Account account { get; set; }
|
|
|
|
public uint timeout_handle_id;
|
|
|
|
|
|
|
|
public TimerPayload(Account account, uint timeout_handle_id) {
|
|
|
|
this.account = account;
|
|
|
|
this.timeout_handle_id = timeout_handle_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private HashMap<XmppStream, TimerPayload> timeouts = new HashMap<XmppStream, TimerPayload>(XmppStream.hash_func, XmppStream.equals_func);
|
|
|
|
|
2019-12-28 02:11:51 +00:00
|
|
|
public Dino.Application app;
|
|
|
|
|
|
|
|
public void registered(Dino.Application app) {
|
2021-03-21 11:41:27 +00:00
|
|
|
Nice.debug_enable(true);
|
2019-12-28 02:11:51 +00:00
|
|
|
this.app = app;
|
2021-03-21 11:41:27 +00:00
|
|
|
app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => {
|
|
|
|
list.add(new Module());
|
|
|
|
});
|
2019-12-28 02:11:51 +00:00
|
|
|
app.stream_interactor.stream_attached_modules.connect((account, stream) => {
|
2021-11-09 21:06:50 +00:00
|
|
|
if (stream.get_module(Socks5Bytestreams.Module.IDENTITY) != null) {
|
|
|
|
stream.get_module(Socks5Bytestreams.Module.IDENTITY).set_local_ip_address_handler(get_local_ip_addresses);
|
|
|
|
}
|
|
|
|
if (stream.get_module(JingleRawUdp.Module.IDENTITY) != null) {
|
|
|
|
stream.get_module(JingleRawUdp.Module.IDENTITY).set_local_ip_address_handler(get_local_ip_addresses);
|
|
|
|
}
|
2019-12-28 02:11:51 +00:00
|
|
|
});
|
2024-04-18 14:41:55 +00:00
|
|
|
app.stream_interactor.stream_negotiated.connect(external_discovery_refresh_services);
|
|
|
|
app.stream_interactor.connection_manager.connection_state_changed.connect(on_connection_state_changed);
|
2019-12-28 02:11:51 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 14:41:55 +00:00
|
|
|
private async void external_discovery_refresh_services(Account account, XmppStream stream) {
|
2021-03-21 11:41:27 +00:00
|
|
|
Module? ice_udp_module = stream.get_module(JingleIceUdp.Module.IDENTITY) as Module;
|
|
|
|
if (ice_udp_module == null) return;
|
|
|
|
Gee.List<Xep.ExternalServiceDiscovery.Service> services = yield ExternalServiceDiscovery.request_services(stream);
|
|
|
|
foreach (Xep.ExternalServiceDiscovery.Service service in services) {
|
|
|
|
if (service.transport == "udp" && (service.ty == "stun" || service.ty == "turn")) {
|
|
|
|
InetAddress ip = yield lookup_ipv4_addess(service.host);
|
|
|
|
if (ip == null) continue;
|
|
|
|
|
|
|
|
if (service.ty == "stun") {
|
|
|
|
debug("Server offers STUN server: %s:%u, resolved to %s", service.host, service.port, ip.to_string());
|
|
|
|
ice_udp_module.stun_ip = ip.to_string();
|
|
|
|
ice_udp_module.stun_port = service.port;
|
|
|
|
} else if (service.ty == "turn") {
|
|
|
|
debug("Server offers TURN server: %s:%u, resolved to %s", service.host, service.port, ip.to_string());
|
|
|
|
ice_udp_module.turn_ip = ip.to_string();
|
|
|
|
ice_udp_module.turn_service = service;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 14:41:55 +00:00
|
|
|
|
|
|
|
if (ice_udp_module.turn_service != null) {
|
|
|
|
DateTime? expires = ice_udp_module.turn_service.expires;
|
|
|
|
if (expires != null) {
|
|
|
|
int64 delay = (expires.to_unix() - new DateTime.now_utc().to_unix()) / 2;
|
|
|
|
|
|
|
|
if (delay >= delay_min && delay <= delay_max) {
|
|
|
|
debug("Next server external service discovery in %lds (because of TURN credentials' expiry time)", (long) delay);
|
|
|
|
|
|
|
|
uint timeout_handle_id = Timeout.add_seconds((uint) delay, () => {
|
|
|
|
on_timeout(stream);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
timeouts[stream] = new TimerPayload(account, timeout_handle_id);
|
|
|
|
timeouts[stream].account = account;
|
|
|
|
timeouts[stream].timeout_handle_id = timeout_handle_id;
|
|
|
|
} else {
|
|
|
|
warning("Bogus TURN credentials' expiry time (delay value = %ld), *not* planning next service discovery", (long) delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 11:41:27 +00:00
|
|
|
if (ice_udp_module.stun_ip == null) {
|
2021-08-03 11:37:36 +00:00
|
|
|
InetAddress ip = yield lookup_ipv4_addess("stun.dino.im");
|
2021-03-21 11:41:27 +00:00
|
|
|
if (ip == null) return;
|
|
|
|
|
2021-08-03 11:37:36 +00:00
|
|
|
debug("Using fallback STUN server: stun.dino.im:7886, resolved to %s", ip.to_string());
|
2021-03-21 11:41:27 +00:00
|
|
|
|
|
|
|
ice_udp_module.stun_ip = ip.to_string();
|
2021-08-03 11:37:36 +00:00
|
|
|
ice_udp_module.stun_port = 7886;
|
2019-12-28 02:11:51 +00:00
|
|
|
}
|
2024-04-18 14:41:55 +00:00
|
|
|
}
|
2024-04-18 14:41:55 +00:00
|
|
|
|
2024-04-18 14:41:55 +00:00
|
|
|
public void on_timeout(XmppStream stream) {
|
|
|
|
if (!timeouts.has_key(stream)) return;
|
|
|
|
TimerPayload pl = timeouts[stream];
|
|
|
|
timeouts.unset(stream);
|
|
|
|
external_discovery_refresh_services.begin(pl.account, stream);
|
|
|
|
}
|
2024-04-18 14:41:55 +00:00
|
|
|
|
2024-04-18 14:41:55 +00:00
|
|
|
public void on_connection_state_changed(Account account, ConnectionManager.ConnectionState state) {
|
|
|
|
if (state == ConnectionManager.ConnectionState.DISCONNECTED) {
|
|
|
|
XmppStream? stream = app.stream_interactor.connection_manager.get_stream(account);
|
|
|
|
if (stream == null) return;
|
|
|
|
if (!timeouts.has_key(stream)) return;
|
2024-04-18 14:41:55 +00:00
|
|
|
|
2024-04-18 14:41:55 +00:00
|
|
|
Source.remove(timeouts[stream].timeout_handle_id);
|
|
|
|
timeouts.unset(stream);
|
2024-04-18 14:41:55 +00:00
|
|
|
}
|
2019-12-28 02:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown() {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
|
2021-03-21 11:41:27 +00:00
|
|
|
private async InetAddress? lookup_ipv4_addess(string host) {
|
|
|
|
try {
|
|
|
|
Resolver resolver = Resolver.get_default();
|
|
|
|
GLib.List<GLib.InetAddress>? ips = yield resolver.lookup_by_name_async(host);
|
|
|
|
foreach (GLib.InetAddress ina in ips) {
|
|
|
|
if (ina.get_family() != SocketFamily.IPV4) continue;
|
|
|
|
return ina;
|
|
|
|
}
|
|
|
|
} catch (Error e) {
|
|
|
|
warning("Failed looking up IP address of %s", host);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|