70 lines
1.5 KiB
Haxe
70 lines
1.5 KiB
Haxe
package leenkx.network.krom;
|
|
|
|
import haxe.io.Bytes;
|
|
import haxe.io.BytesOutput;
|
|
import haxe.io.Error;
|
|
import leenkx.network.krom.KromSocket.KromHost;
|
|
|
|
@:native("krom_socket_enable_ssl") extern function krom_socket_enable_ssl(id:Int):Bool;
|
|
|
|
class KromSecureSocket extends KromSocket {
|
|
private var _sslEnabled:Bool = false;
|
|
private var _hostname:String = null;
|
|
private var _certPath:String = null;
|
|
private var _keyPath:String = null;
|
|
private var _caPath:String = null;
|
|
public var verifyCert:Bool = true;
|
|
|
|
public function new() {
|
|
super();
|
|
}
|
|
|
|
public function setHostname(hostname:String):Void {
|
|
_hostname = hostname;
|
|
}
|
|
|
|
public function setCA(ca:Dynamic):Void {
|
|
if (ca != null) {
|
|
_caPath = Std.string(ca);
|
|
}
|
|
}
|
|
|
|
public function setCertificate(cert:Dynamic, key:Dynamic):Void {
|
|
if (cert != null) {
|
|
_certPath = Std.string(cert);
|
|
}
|
|
if (key != null) {
|
|
_keyPath = Std.string(key);
|
|
}
|
|
}
|
|
|
|
public function enableSSL():Bool {
|
|
if (getSocketId() >= 0 && !_sslEnabled) {
|
|
var result = krom_socket_enable_ssl(getSocketId());
|
|
if (result) {
|
|
_sslEnabled = true;
|
|
} else {
|
|
trace("SecureSocket: Failed to enable SSL for socket " + getSocketId());
|
|
}
|
|
return result;
|
|
}
|
|
return _sslEnabled;
|
|
}
|
|
|
|
override public function connect(host:KromHost, port:Int):Void {
|
|
setBlocking(true);
|
|
try {
|
|
super.connect(host, port);
|
|
enableSSL();
|
|
setBlocking(false);
|
|
} catch (e:Dynamic) {
|
|
setBlocking(false);
|
|
trace('Failed Krom Host');
|
|
}
|
|
}
|
|
|
|
public function isSSLEnabled():Bool {
|
|
return _sslEnabled;
|
|
}
|
|
}
|