jld3103
2 years ago
8 changed files with 221 additions and 6 deletions
@ -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 |
@ -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. |
@ -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 |
@ -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<PushProxyDevice> _onNewDeviceController; |
||||
late StreamController<PushProxyNotification> _onNewNotificationController; |
||||
|
||||
Stream<PushProxyDevice>? _onNewDeviceStream; |
||||
Stream<PushProxyNotification>? _onNewNotificationStream; |
||||
|
||||
/// Listens for new devices |
||||
Stream<PushProxyDevice> get onNewDevice { |
||||
if (_onNewDeviceStream == null) { |
||||
throw Exception('Server not created'); |
||||
} |
||||
return _onNewDeviceStream!; |
||||
} |
||||
|
||||
/// Listens for new notifications |
||||
Stream<PushProxyNotification> get onNewNotification { |
||||
if (_onNewNotificationStream == null) { |
||||
throw Exception('Server not created'); |
||||
} |
||||
return _onNewNotificationStream!; |
||||
} |
||||
|
||||
late final _router = Router() |
||||
..post('/devices', _devicesHandler) |
||||
..post('/notifications', _notificationsHandler); |
||||
|
||||
Future<Response> _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<Response> _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<String, dynamic>; |
||||
_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<PushProxyDevice>(); |
||||
_onNewNotificationController = StreamController<PushProxyNotification>(); |
||||
_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; |
||||
} |
@ -0,0 +1,7 @@
|
||||
sdk: |
||||
- stable |
||||
|
||||
stages: |
||||
- analyze: |
||||
- analyze |
||||
- format: --output=none --set-exit-if-changed --line-length 120 . |
@ -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 |
Loading…
Reference in new issue