summaryrefslogtreecommitdiffstats
path: root/lib/vehicle_signal/vehicle_signal_config.dart
blob: 3f0ff1d2cf8d8dad4107fb7f1be35baa5b2c23ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SPDX-License-Identifier: Apache-2.0

import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_cluster_dashboard/cluster_config.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final sockConnectprovider = FutureProvider.family<WebSocket, HttpClient>(
    (ref, client) => connect(client, ref));

// load certificates and set context and returns http client
Future<HttpClient> initializeClient() async {
  ByteData dataCA = await rootBundle.load('assets/cert/CA.pem');
  ByteData dataCert = await rootBundle.load('assets/cert/Client.pem');
  ByteData dataKey = await rootBundle.load('assets/cert/Client.key');

  SecurityContext ctx = SecurityContext.defaultContext;
  ctx.useCertificateChainBytes(dataCert.buffer.asUint8List());
  ctx.usePrivateKeyBytes(dataKey.buffer.asUint8List());
  ctx.setTrustedCertificatesBytes(dataCA.buffer.asUint8List());
  HttpClient client = HttpClient(context: ctx)
    ..findProxy = null
    ..badCertificateCallback = (cert, host, port) {
      return true;
    };
  return client;
}

Future<WebSocket> connect(HttpClient client, ref) async {
  final config = ref.read(clusterConfigStateprovider);
  WebSocket socket = await WebSocket.connect(
      "wss://${config.hostname}:${config.port}",
      customClient: client);
  return socket;
}