TrojanURL constructor

TrojanURL({
  1. required String url,
})

Creates a TrojanURL by parsing the provided trojan share link string.

Throws ArgumentError if the url does not start with trojan:// or cannot be decoded into a valid URI.

Implementation

TrojanURL({required super.url}) {
  if (!url.startsWith('trojan://')) {
    throw ArgumentError('url is invalid');
  }
  final temp = Uri.tryParse(url);
  if (temp == null) {
    throw ArgumentError('url is invalid');
  }
  uri = temp;
  if (uri.queryParameters.isNotEmpty) {
    final sni = super.populateTransportSettings(
      transport: uri.queryParameters['type'] ?? 'tcp',
      headerType: uri.queryParameters['headerType'],
      host: uri.queryParameters['host'],
      path: uri.queryParameters['path'],
      seed: uri.queryParameters['seed'],
      quicSecurity: uri.queryParameters['quicSecurity'],
      key: uri.queryParameters['key'],
      mode: uri.queryParameters['mode'],
      serviceName: uri.queryParameters['serviceName'],
    );

    super.populateTlsSettings(
      streamSecurity: uri.queryParameters['security'] ?? 'tls',
      allowInsecure: allowInsecure,
      sni: uri.queryParameters['sni'] ?? sni,
      fingerprint:
          streamSetting['tlsSettings']?['fingerprint'] ?? 'randomized',
      alpns: uri.queryParameters['alpn'],
      publicKey: null,
      shortId: null,
      spiderX: null,
    );
    flow = uri.queryParameters['flow'] ?? '';
  } else {
    super.populateTlsSettings(
      streamSecurity: 'tls',
      allowInsecure: allowInsecure,
      sni: '',
      fingerprint:
          streamSetting['tlsSettings']?['fingerprint'] ?? 'randomized',
      alpns: null,
      publicKey: null,
      shortId: null,
      spiderX: null,
    );
  }
}