From 565089e1c82ecab8bc6ef1127e7f4f78a92438a1 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Tue, 8 Nov 2022 16:58:36 +0100 Subject: [PATCH] feat: Make moxxmpp_socket Flutter independent --- README.md | 4 ++-- moxxmpp_socket/.flutter-plugins | 4 ---- moxxmpp_socket/.flutter-plugins-dependencies | 1 - moxxmpp_socket/README.md | 11 ++++++++++- moxxmpp_socket/lib/src/record.dart | 8 ++++++++ moxxmpp_socket/lib/src/rfc_2782.dart | 4 ++-- moxxmpp_socket/lib/src/socket.dart | 17 ++++++++++++++--- moxxmpp_socket/pubspec.yaml | 7 ++----- 8 files changed, 38 insertions(+), 18 deletions(-) delete mode 100644 moxxmpp_socket/.flutter-plugins delete mode 100644 moxxmpp_socket/.flutter-plugins-dependencies create mode 100644 moxxmpp_socket/lib/src/record.dart diff --git a/README.md b/README.md index bd8cc24..466334c 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ This package contains the actual XMPP code that is platform-independent. ### moxxmpp_socket `moxxmpp_socket` contains the implementation of the `BaseSocketWrapper` class that -allows the user to resolve SRV records and thus support XEP-0368. Due to how DNS -resolution is implemented, Flutter is required. +implements the RFC6120 connection algorithm and XEP-0368 direct TLS connections, +if a DNS implementation is given, and supports StartTLS. ## License diff --git a/moxxmpp_socket/.flutter-plugins b/moxxmpp_socket/.flutter-plugins deleted file mode 100644 index 4822cf8..0000000 --- a/moxxmpp_socket/.flutter-plugins +++ /dev/null @@ -1,4 +0,0 @@ -# This is a generated file; do not edit or check into version control. -moxdns=/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns-0.1.4/ -moxdns_android=/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_android-0.1.4/ -moxdns_linux=/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_linux-0.1.4/ diff --git a/moxxmpp_socket/.flutter-plugins-dependencies b/moxxmpp_socket/.flutter-plugins-dependencies deleted file mode 100644 index a648992..0000000 --- a/moxxmpp_socket/.flutter-plugins-dependencies +++ /dev/null @@ -1 +0,0 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[],"android":[{"name":"moxdns_android","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_android-0.1.4/","native_build":true,"dependencies":[]}],"macos":[],"linux":[{"name":"moxdns_linux","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_linux-0.1.4/","native_build":true,"dependencies":[]}],"windows":[],"web":[]},"dependencyGraph":[{"name":"moxdns","dependencies":["moxdns_android","moxdns_linux"]},{"name":"moxdns_android","dependencies":["moxdns"]},{"name":"moxdns_linux","dependencies":["moxdns"]}],"date_created":"2022-11-08 14:21:52.966343","version":"3.3.3"} \ No newline at end of file diff --git a/moxxmpp_socket/README.md b/moxxmpp_socket/README.md index 99115ab..5444782 100644 --- a/moxxmpp_socket/README.md +++ b/moxxmpp_socket/README.md @@ -1,6 +1,15 @@ # moxxmpp_socket -A socket for moxxmpp that does SRV resolution. +A socket for moxxmpp that implements the connection algorithm as specified by +[RFC6210](https://xmpp.org/rfcs/rfc6120.html) and [XEP-0368](https://xmpp.org/extensions/xep-0368.html), +while also supporting StartTLS and direct TLS. + +In order to make this package independent of Flutter, I removed DNS SRV resolution from +the package. The `TCPSocketWrapper` contains a method called `srvQuery` that can be +overridden by the user. It takes the domain to query and a DNSSEC flag and is expected +to return the list of SRV records, encoded by `MoxSrvRecord` objects. To perform the +resolution, one can use any DNS library. A Flutter plugin implementing SRV resolution +is, for example, [moxdns](https://codeberg.org/moxxy/moxdns). ## License diff --git a/moxxmpp_socket/lib/src/record.dart b/moxxmpp_socket/lib/src/record.dart new file mode 100644 index 0000000..f9ce996 --- /dev/null +++ b/moxxmpp_socket/lib/src/record.dart @@ -0,0 +1,8 @@ +/// A data class to represent a DNS SRV record. +class MoxSrvRecord { + MoxSrvRecord(this.priority, this.weight, this.target, this.port); + final int priority; + final int weight; + final int port; + final String target; +} diff --git a/moxxmpp_socket/lib/src/rfc_2782.dart b/moxxmpp_socket/lib/src/rfc_2782.dart index f04ceeb..7ea6777 100644 --- a/moxxmpp_socket/lib/src/rfc_2782.dart +++ b/moxxmpp_socket/lib/src/rfc_2782.dart @@ -1,7 +1,7 @@ -import 'package:moxdns/moxdns.dart'; +import 'package:moxxmpp_socket/src/record.dart'; /// Sorts the SRV records according to priority and weight. -int srvRecordSortComparator(SrvRecord a, SrvRecord b) { +int srvRecordSortComparator(MoxSrvRecord a, MoxSrvRecord b) { if (a.priority < b.priority) { return -1; } else { diff --git a/moxxmpp_socket/lib/src/socket.dart b/moxxmpp_socket/lib/src/socket.dart index 0d16502..c71dcb7 100644 --- a/moxxmpp_socket/lib/src/socket.dart +++ b/moxxmpp_socket/lib/src/socket.dart @@ -2,8 +2,9 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:logging/logging.dart'; -import 'package:moxdns/moxdns.dart'; +import 'package:meta/meta.dart'; import 'package:moxxmpp/moxxmpp.dart'; +import 'package:moxxmpp_socket/src/record.dart'; import 'package:moxxmpp_socket/src/rfc_2782.dart'; /// TCP socket implementation for XmppConnection @@ -38,6 +39,16 @@ class TCPSocketWrapper extends BaseSocketWrapper { void destroy() { _socketSubscription?.cancel(); } + + /// Called on connect to perform a SRV query against [domain]. If [dnssec] is true, + /// then DNSSEC validation should be performed. + /// + /// Returns a list of SRV records. If none are available or an error occured, an empty + /// list is returned. + @visibleForOverriding + Future> srvQuery(String domain, bool dnssec) async { + return []; + } bool _onBadCertificate(dynamic certificate, String domain) { _log.fine('Bad certificate: ${certificate.toString()}'); @@ -49,7 +60,7 @@ class TCPSocketWrapper extends BaseSocketWrapper { Future _xep368Connect(String domain) async { // TODO(Unknown): Maybe do DNSSEC one day - final results = await MoxdnsPlugin.srvQuery('_xmpps-client._tcp.$domain', false); + final results = await srvQuery('_xmpps-client._tcp.$domain', false); if (results.isEmpty) { return false; } @@ -90,7 +101,7 @@ class TCPSocketWrapper extends BaseSocketWrapper { Future _rfc6120Connect(String domain) async { // TODO(Unknown): Maybe do DNSSEC one day - final results = await MoxdnsPlugin.srvQuery('_xmpp-client._tcp.$domain', false); + final results = await srvQuery('_xmpp-client._tcp.$domain', false); results.sort(srvRecordSortComparator); for (final srv in results) { diff --git a/moxxmpp_socket/pubspec.yaml b/moxxmpp_socket/pubspec.yaml index 68ee641..0e3ddef 100644 --- a/moxxmpp_socket/pubspec.yaml +++ b/moxxmpp_socket/pubspec.yaml @@ -1,18 +1,15 @@ name: moxxmpp_socket -description: A socket for moxxmpp that resolves SRV records +description: A socket for moxxmpp that implements the RFC6120 connection algorithm version: 0.1.0 homepage: https://codeberg.org/moxxy/moxxmpp publish_to: https://git.polynom.me/api/packages/Moxxy/pub environment: sdk: '>=2.18.0 <3.0.0' - flutter: '>=2.13.0-0.1' dependencies: logging: 1.0.2 - moxdns: - hosted: https://git.polynom.me/api/packages/Moxxy/pub - version: 0.1.4 + meta: ^1.6.0 moxxmpp: hosted: https://git.polynom.me/api/packages/Moxxy/pub version: 0.1.0