From 5509319a8bec4ea4d869346ea145deb2b511dee6 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Thu, 14 Jul 2022 17:10:39 +0200 Subject: [PATCH] nextcloud_push_proxy: Minimal library implementation --- .github/workflows/dart.yml | 30 +++- .idea/nextcloud-harbour.iml | 5 + packages/nextcloud_push_proxy/.gitignore | 10 ++ packages/nextcloud_push_proxy/LICENSE | 12 ++ .../analysis_options.yaml | 5 + .../lib/nextcloud_push_proxy.dart | 143 ++++++++++++++++++ packages/nextcloud_push_proxy/mono_pkg.yaml | 7 + packages/nextcloud_push_proxy/pubspec.yaml | 15 ++ 8 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 packages/nextcloud_push_proxy/.gitignore create mode 100644 packages/nextcloud_push_proxy/LICENSE create mode 100644 packages/nextcloud_push_proxy/analysis_options.yaml create mode 100644 packages/nextcloud_push_proxy/lib/nextcloud_push_proxy.dart create mode 100644 packages/nextcloud_push_proxy/mono_pkg.yaml create mode 100644 packages/nextcloud_push_proxy/pubspec.yaml diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index 08b11ef1..4df1fc8a 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -35,16 +35,16 @@ jobs: - name: mono_repo self validate run: dart pub global run mono_repo generate --validate job_002: - name: "analyze; PKGS: packages/file_icons, packages/harbour, packages/nextcloud, packages/settings, packages/sort_box, packages/spec_templates; `dart format --output=none --set-exit-if-changed --line-length 120 .`" + name: "analyze; PKGS: packages/file_icons, packages/harbour, packages/nextcloud, packages/nextcloud_push_proxy, packages/settings, packages/sort_box, packages/spec_templates; `dart format --output=none --set-exit-if-changed --line-length 120 .`" runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies uses: actions/cache@4504faf7e9bcf8f3ed0bc863c4e1d21499ab8ef8 with: path: "~/.pub-cache/hosted" - key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/settings-packages/sort_box-packages/spec_templates;commands:format" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/nextcloud_push_proxy-packages/settings-packages/sort_box-packages/spec_templates;commands:format" restore-keys: | - os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/settings-packages/sort_box-packages/spec_templates + os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/nextcloud_push_proxy-packages/settings-packages/sort_box-packages/spec_templates os:ubuntu-latest;pub-cache-hosted;sdk:stable os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest @@ -80,6 +80,15 @@ jobs: if: "always() && steps.packages_nextcloud_pub_upgrade.conclusion == 'success'" working-directory: packages/nextcloud run: "dart format --output=none --set-exit-if-changed --line-length 120 ." + - id: packages_nextcloud_push_proxy_pub_upgrade + name: packages/nextcloud_push_proxy; flutter pub pub upgrade + if: "always() && steps.checkout.conclusion == 'success'" + working-directory: packages/nextcloud_push_proxy + run: flutter pub pub upgrade + - name: "packages/nextcloud_push_proxy; dart format --output=none --set-exit-if-changed --line-length 120 ." + if: "always() && steps.packages_nextcloud_push_proxy_pub_upgrade.conclusion == 'success'" + working-directory: packages/nextcloud_push_proxy + run: "dart format --output=none --set-exit-if-changed --line-length 120 ." - id: packages_settings_pub_upgrade name: packages/settings; flutter pub pub upgrade if: "always() && steps.checkout.conclusion == 'success'" @@ -158,16 +167,16 @@ jobs: needs: - job_001 job_004: - name: "analyze; PKGS: packages/nextcloud, packages/sort_box, packages/spec_templates; `dart analyze`" + name: "analyze; PKGS: packages/nextcloud, packages/nextcloud_push_proxy, packages/sort_box, packages/spec_templates; `dart analyze`" runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies uses: actions/cache@4504faf7e9bcf8f3ed0bc863c4e1d21499ab8ef8 with: path: "~/.pub-cache/hosted" - key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/sort_box-packages/spec_templates;commands:analyze_1" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/nextcloud_push_proxy-packages/sort_box-packages/spec_templates;commands:analyze_1" restore-keys: | - os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/sort_box-packages/spec_templates + os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/nextcloud_push_proxy-packages/sort_box-packages/spec_templates os:ubuntu-latest;pub-cache-hosted;sdk:stable os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest @@ -185,6 +194,15 @@ jobs: if: "always() && steps.packages_nextcloud_pub_upgrade.conclusion == 'success'" working-directory: packages/nextcloud run: dart analyze + - id: packages_nextcloud_push_proxy_pub_upgrade + name: packages/nextcloud_push_proxy; dart pub upgrade + if: "always() && steps.checkout.conclusion == 'success'" + working-directory: packages/nextcloud_push_proxy + run: dart pub upgrade + - name: packages/nextcloud_push_proxy; dart analyze + if: "always() && steps.packages_nextcloud_push_proxy_pub_upgrade.conclusion == 'success'" + working-directory: packages/nextcloud_push_proxy + run: dart analyze - id: packages_sort_box_pub_upgrade name: packages/sort_box; dart pub upgrade if: "always() && steps.checkout.conclusion == 'success'" diff --git a/.idea/nextcloud-harbour.iml b/.idea/nextcloud-harbour.iml index 4db18808..1078fb02 100644 --- a/.idea/nextcloud-harbour.iml +++ b/.idea/nextcloud-harbour.iml @@ -44,6 +44,11 @@ + + + + + diff --git a/packages/nextcloud_push_proxy/.gitignore b/packages/nextcloud_push_proxy/.gitignore new file mode 100644 index 00000000..65c34dc8 --- /dev/null +++ b/packages/nextcloud_push_proxy/.gitignore @@ -0,0 +1,10 @@ +# Files and directories created by pub. +.dart_tool/ +.packages + +# Conventional directory for build outputs. +build/ + +# Omit committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/packages/nextcloud_push_proxy/LICENSE b/packages/nextcloud_push_proxy/LICENSE new file mode 100644 index 00000000..195a2697 --- /dev/null +++ b/packages/nextcloud_push_proxy/LICENSE @@ -0,0 +1,12 @@ +Copyright (c) 2022, jld3103 +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/nextcloud_push_proxy/analysis_options.yaml b/packages/nextcloud_push_proxy/analysis_options.yaml new file mode 100644 index 00000000..0cc523ce --- /dev/null +++ b/packages/nextcloud_push_proxy/analysis_options.yaml @@ -0,0 +1,5 @@ +include: package:nit_picking/dart.yaml + +linter: + rules: + prefer_final_parameters: false # Disabled until super.X is no longer complained about in constructors diff --git a/packages/nextcloud_push_proxy/lib/nextcloud_push_proxy.dart b/packages/nextcloud_push_proxy/lib/nextcloud_push_proxy.dart new file mode 100644 index 00000000..56bbff58 --- /dev/null +++ b/packages/nextcloud_push_proxy/lib/nextcloud_push_proxy.dart @@ -0,0 +1,143 @@ +// ignore_for_file: public_member_api_docs + +library nextcloud_push_proxy; + +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:shelf/shelf.dart'; +import 'package:shelf/shelf_io.dart'; +import 'package:shelf_router/shelf_router.dart'; + +/// Implements the listening part of a Nextcloud push proxy +class NextcloudPushProxy { + HttpServer? _server; + + late StreamController _onNewDeviceController; + late StreamController _onNewNotificationController; + + Stream? _onNewDeviceStream; + Stream? _onNewNotificationStream; + + /// Listens for new devices + Stream get onNewDevice { + if (_onNewDeviceStream == null) { + throw Exception('Server not created'); + } + return _onNewDeviceStream!; + } + + /// Listens for new notifications + Stream get onNewNotification { + if (_onNewNotificationStream == null) { + throw Exception('Server not created'); + } + return _onNewNotificationStream!; + } + + late final _router = Router() + ..post('/devices', _devicesHandler) + ..post('/notifications', _notificationsHandler); + + Future _devicesHandler(Request request) async { + final data = Uri(query: await request.readAsString()).queryParameters; + _onNewDeviceController.add( + PushProxyDevice( + pushToken: data['pushToken']!, + deviceIdentifier: data['deviceIdentifier']!, + deviceIdentifierSignature: data['deviceIdentifierSignature']!, + userPublicKey: data['userPublicKey']!, + ), + ); + return Response.ok(''); + } + + Future _notificationsHandler(Request request) async { + final data = Uri(query: await request.readAsString()).queryParameters; + for (final notification in data.values) { + final notificationData = json.decode(notification) as Map; + _onNewNotificationController.add( + PushProxyNotification( + deviceIdentifier: notificationData['deviceIdentifier']! as String, + pushTokenHash: notificationData['pushTokenHash']! as String, + subject: notificationData['subject']! as String, + signature: notificationData['signature']! as String, + priority: notificationData['priority']! as String, + type: notificationData['type']! as String, + ), + ); + } + return Response.ok(''); + } + + /// Creates a server listening on the [port] + Future create({ + final bool logging = true, + final int port = 8080, + }) async { + if (_server != null) { + throw Exception('Server already created'); + } + + _onNewDeviceController = StreamController(); + _onNewNotificationController = StreamController(); + _onNewDeviceStream = _onNewDeviceController.stream.asBroadcastStream(); + _onNewNotificationStream = _onNewNotificationController.stream.asBroadcastStream(); + + var handler = Cascade().add(_router).handler; + if (logging) { + handler = logRequests().addHandler(handler); + } + final server = await serve( + handler, + InternetAddress.anyIPv4, + port, + ); + server.autoCompress = true; + + _server = server; + } + + /// Closes the server + Future close() async { + if (_server != null) { + await _server!.close(); + _server = null; + await _onNewDeviceController.close(); + await _onNewNotificationController.close(); + } + } +} + +class PushProxyDevice { + PushProxyDevice({ + required this.pushToken, + required this.deviceIdentifier, + required this.deviceIdentifierSignature, + required this.userPublicKey, + }); + + final String pushToken; + final String deviceIdentifier; + final String deviceIdentifierSignature; + final String userPublicKey; +} + +class PushProxyNotification { + PushProxyNotification({ + required this.deviceIdentifier, + required this.pushTokenHash, + required this.subject, + required this.signature, + required this.priority, + required this.type, + }); + + final String deviceIdentifier; + final String pushTokenHash; + final String subject; + final String signature; + final String priority; + final String type; +} diff --git a/packages/nextcloud_push_proxy/mono_pkg.yaml b/packages/nextcloud_push_proxy/mono_pkg.yaml new file mode 100644 index 00000000..e3d1d89c --- /dev/null +++ b/packages/nextcloud_push_proxy/mono_pkg.yaml @@ -0,0 +1,7 @@ +sdk: + - stable + +stages: + - analyze: + - analyze + - format: --output=none --set-exit-if-changed --line-length 120 . diff --git a/packages/nextcloud_push_proxy/pubspec.yaml b/packages/nextcloud_push_proxy/pubspec.yaml new file mode 100644 index 00000000..ee60bb9d --- /dev/null +++ b/packages/nextcloud_push_proxy/pubspec.yaml @@ -0,0 +1,15 @@ +name: nextcloud_push_proxy +version: 1.0.0 + +environment: + sdk: '>=2.17.0 <3.0.0' + +dependencies: + shelf: ^1.3.1 + shelf_router: ^1.1.3 + +dev_dependencies: + nit_picking: + git: + url: https://github.com/stack11/dart_nit_picking + ref: f29382f