Skip to content

Commit e373123

Browse files
committed
Improve server with security context, improve scheme
1 parent 6cec10c commit e373123

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

api/lib/connection.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ class SwampConnection extends NetworkerPipe<Uint8List, RpcNetworkerPacket>
171171
scheme: scheme.substring(kSwampSchemePrefix.length),
172172
);
173173
}
174+
if (!address.hasScheme) {
175+
address = address.replace(scheme: 'wss');
176+
}
174177
final channel =
175178
_channel = WebSocketChannel.connect(address, protocols: ['swamp-0']);
176179
channel.stream.listen(

server/bin/swamp.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:io';
22

3+
import 'package:consoler/consoler.dart';
34
import 'package:swamp/server.dart';
45

56
final welcomeMessage = """
@@ -21,8 +22,25 @@ Future<void> main(List<String> args) async {
2122

2223
// For running in containers, we respect the PORT environment variable.
2324
final port = int.parse(Platform.environment['PORT'] ?? '8080');
24-
final server = SwampServer(ip, port);
25+
SecurityContext? securityContext;
26+
try {
27+
final privateKey = await File('certs/server.key').readAsBytes();
28+
final certificate = await File('certs/server.crt').readAsBytes();
29+
securityContext =
30+
SecurityContext()
31+
..usePrivateKeyBytes(privateKey)
32+
..useCertificateChainBytes(certificate);
33+
} on PathNotFoundException catch (_) {}
34+
final server = SwampServer(ip, port, securityContext: securityContext);
2535
server.log(welcomeMessage);
36+
if (securityContext != null) {
37+
server.log('Certificates found, using secure connection', LogLevel.info);
38+
} else {
39+
server.log(
40+
'No certificates found, using insecure connection',
41+
LogLevel.warning,
42+
);
43+
}
2644
await server.init();
2745
server.log('Server listening on port ${server.port}');
2846
}

server/lib/server.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SwampServer extends NetworkerSocketServer {
2424
super.port, {
2525
bool withConsole = true,
2626
LogLevel? minLogLevel,
27+
super.securityContext,
2728
}) {
2829
connect(_rpcPipe..connect(_roomManager));
2930

server/lib/src/programs/room.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:typed_data';
2+
13
import 'package:consoler/consoler.dart';
24
import 'package:swamp/room.dart';
35
import 'package:swamp_api/models.dart';
@@ -19,7 +21,13 @@ class RoomProgram extends ConsoleProgram {
1921
print("Usage: room ${getUsage()}");
2022
return;
2123
}
22-
final roomId = decodeRoomCode(args[0]);
24+
Uint8List roomId;
25+
try {
26+
roomId = decodeRoomCode(args[0]);
27+
} on FormatException {
28+
print("Invalid room code");
29+
return;
30+
}
2331
final room = roomManager.getRoom(roomId);
2432
if (room == null) {
2533
print("Room not found");

0 commit comments

Comments
 (0)