Skip to content

Commit

Permalink
Add allowBadCertificate for UaSettings.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Jun 12, 2020
1 parent f77f741 commit 55f4795
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
16 changes: 16 additions & 0 deletions example/lib/generated_plugin_registrant.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Generated file. Do not edit.
//

// ignore: unused_import
import 'dart:ui';

import 'package:shared_preferences_web/shared_preferences_web.dart';

import 'package:flutter_web_plugins/flutter_web_plugins.dart';

// ignore: public_member_api_docs
void registerPlugins(PluginRegistry registry) {
SharedPreferencesPlugin.registerWith(registry.registrarFor(SharedPreferencesPlugin));
registry.registerMessageHandler();
}
1 change: 1 addition & 0 deletions example/lib/src/register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class _MyRegisterWidget extends State<RegisterWidget>
settings.password = _password;
settings.displayName = _displayName;
settings.webSocketExtraHeaders = _wsExtraHeaders;
settings.allowBadCertificate = true;

helper.start(settings);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/sip_ua_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class SIPUAHelper extends EventManager {

_settings = Settings();
var socket = WebSocketInterface(
uaSettings.webSocketUrl, uaSettings.webSocketExtraHeaders);
uaSettings.webSocketUrl, uaSettings.webSocketExtraHeaders, uaSettings.allowBadCertificate);
_settings.sockets = [socket];
_settings.uri = uaSettings.uri;
_settings.password = uaSettings.password;
Expand Down Expand Up @@ -459,6 +459,7 @@ abstract class SipUaHelperListener {
class UaSettings {
String webSocketUrl;
Map<String, dynamic> webSocketExtraHeaders;
bool allowBadCertificate = false;

String uri;
String authorizationUser;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Transport {
}

var message = data.toString();
logger.debug('sending message:\n\n${message}\n');
//logger.debug('sending message:\n\n${message}\n');
return this.socket.send(message);
}

Expand Down
48 changes: 30 additions & 18 deletions lib/src/transports/websocket_dart_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'dart:convert';
import 'dart:async';
import '../logger.dart';
import '../grammar.dart';

typedef void OnMessageCallback(dynamic msg);
typedef void OnCloseCallback(int code, String reason);
Expand All @@ -17,15 +18,21 @@ class WebSocketImpl {
OnCloseCallback onClose;
WebSocketImpl(this._url);

void connect({Object protocols, Object headers}) async {
logger.info('connect $_url, $headers, $protocols');
void connect(
{Iterable<String> protocols,
Map<String, String> extHeaders,
bool allowBadCertificate = false}) async {
logger.info('connect $_url, $extHeaders, $protocols');
try {
_socket =
await WebSocket.connect(_url, protocols: protocols, headers: headers);

/// Allow self-signed certificate, for test only.
/// var parsed_url = Grammar.parse(this._url, 'absoluteURI');
/// _socket = await _connectForBadCertificate(parsed_url.scheme, parsed_url.host, parsed_url.port);
if (allowBadCertificate) {
/// Allow self-signed certificate, for test only.
var parsed_url = Grammar.parse(_url, 'absoluteURI');
_socket = await _connectForBadCertificate(
parsed_url.scheme, parsed_url.host, parsed_url.port, extHeaders);
} else {
_socket = await WebSocket.connect(_url,
protocols: protocols, headers: extHeaders);
}

this?.onOpen();
_socket.listen((data) {
Expand All @@ -41,7 +48,7 @@ class WebSocketImpl {
void send(data) {
if (_socket != null) {
_socket.add(data);
logger.debug('send: $data');
logger.debug('send: \n\n$data');
}
}

Expand All @@ -54,31 +61,36 @@ class WebSocketImpl {
}

/// For test only.
Future<WebSocket> _connectForBadCertificate(
String scheme, String host, int port) async {
Future<WebSocket> _connectForBadCertificate(String scheme, String host,
int port, Map<String, String> extHeaders) async {
try {
Random r = new Random();
String key = base64.encode(List<int>.generate(8, (_) => r.nextInt(255)));
SecurityContext securityContext = new SecurityContext();
HttpClient client = HttpClient(context: securityContext);
var r = new Random();
var key = base64.encode(List<int>.generate(8, (_) => r.nextInt(255)));
var securityContext = new SecurityContext();
var client = HttpClient(context: securityContext);
client.badCertificateCallback =
(X509Certificate cert, String host, int port) {
logger.warn('Allow self-signed certificate => $host:$port. ');
return true;
};

HttpClientRequest request = await client.getUrl(Uri.parse(
var request = await client.getUrl(Uri.parse(
(scheme == 'wss' ? 'https' : 'http') +
'://$host:$port/ws')); // form the correct url here

request.headers.add('Connection', 'Upgrade');
request.headers.add('Upgrade', 'websocket');
request.headers.add('Sec-WebSocket-Protocol', 'sip');
request.headers.add(
'Sec-WebSocket-Version', '13'); // insert the correct version here
request.headers.add('Sec-WebSocket-Key', key.toLowerCase());

HttpClientResponse response = await request.close();
//request.headers.add('Origin', 'http://localhost:5060');
//request.headers.add('Sec-WebSocket-Protocol', 'sip');
extHeaders.forEach((key, value) {
request.headers.add(key, value);
});

var response = await request.close();
var socket = await response.detachSocket();
var webSocket = WebSocket.fromUpgradedSocket(
socket,
Expand Down
12 changes: 8 additions & 4 deletions lib/src/transports/websocket_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class WebSocketInterface implements Socket {
String _sip_uri;
String _via_transport;
String _websocket_protocol = 'sip';
bool _allowBadCertificate = false;
WebSocketImpl _ws;
var _closed = false;
var _connected = false;
Expand All @@ -30,7 +31,8 @@ class WebSocketInterface implements Socket {
@override
void Function(dynamic data) ondata;

WebSocketInterface(String url, [Map<String, dynamic> wsExtraHeaders]) {
WebSocketInterface(String url,
[Map<String, dynamic> wsExtraHeaders, bool allowBadCertificate]) {
logger.debug('new() [url:' + url + ']');
this._url = url;
var parsed_url = Grammar.parse(url, 'absoluteURI');
Expand All @@ -42,11 +44,13 @@ class WebSocketInterface implements Socket {
throw new AssertionError('Invalid argument: ${url}');
} else {
var port = parsed_url.port != null ? ':${parsed_url.port}' : '';
this._sip_uri = 'sip:${parsed_url.host}${port};transport=${parsed_url.scheme}';
this._sip_uri =
'sip:${parsed_url.host}${port};transport=${parsed_url.scheme}';
logger.debug('SIP URI: ${this._sip_uri}');
this._via_transport = parsed_url.scheme.toUpperCase();
}
this._wsExtraHeaders = wsExtraHeaders ?? {};
this._allowBadCertificate = allowBadCertificate ?? false;
}

@override
Expand Down Expand Up @@ -96,10 +100,10 @@ class WebSocketInterface implements Socket {
this._onClose(true, closeCode, closeReason);
};

await this._ws.connect(headers: {
await this._ws.connect(extHeaders: {
'Sec-WebSocket-Protocol': _websocket_protocol,
...this._wsExtraHeaders
});
}, allowBadCertificate: this._allowBadCertificate);
} catch (e, s) {
Log.e(e.toString(), null, s);
_connected = false;
Expand Down
9 changes: 6 additions & 3 deletions lib/src/transports/websocket_web_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ class WebSocketImpl {

WebSocketImpl(this._url);

void connect({Object protocols, Object headers}) async {
logger.info('connect $_url, $headers, $protocols');
void connect(
{Iterable<String> protocols,
Map<String, String> extHeaders,
bool allowBadCertificate = false}) async {
logger.info('connect $_url, $extHeaders, $protocols');
try {
_socket = WebSocket(_url, 'sip');
_socket.onOpen.listen((e) {
Expand Down Expand Up @@ -46,7 +49,7 @@ class WebSocketImpl {
void send(data) {
if (_socket != null && _socket.readyState == WebSocket.OPEN) {
_socket.send(data);
logger.debug('send: $data');
logger.debug('send: \n\n$data');
} else {
logger.error('WebSocket not connected, message $data not sent');
}
Expand Down

0 comments on commit 55f4795

Please sign in to comment.