From 75b477affbc8f377dee5d440c410466714051b01 Mon Sep 17 00:00:00 2001 From: Apprentice-Alchemist <53486764+Apprentice-Alchemist@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:39:04 +0100 Subject: [PATCH] Some changes, add TLS api. --- std/python/_std/sys/fs/DirEntry.hx | 12 ++++-- std/python/_std/sys/fs/File.hx | 10 ++--- std/python/_std/sys/fs/Fs.hx | 61 ++++++++++++++++++++------- std/python/_std/sys/fs/Metadata.hx | 7 ++- std/python/_std/sys/fs/Permissions.hx | 12 +++++- std/python/lib/Os.hx | 2 + std/sys/fs/Permissions.hx | 2 +- std/sys/net/DatagramSocket.hx | 1 - std/sys/net/Selector.hx | 28 ++++++++++++ std/sys/net/SocketAddress.hx | 2 +- std/sys/net/TcpListener.hx | 6 +-- std/sys/net/TcpStream.hx | 3 +- std/sys/net/UdpSocket.hx | 2 +- std/sys/tls/Certificate.hx | 6 +++ std/sys/tls/Identity.hx | 6 +++ std/sys/tls/TlsListener.hx | 7 +++ std/sys/tls/TlsListenerConfig.hx | 5 +++ std/sys/tls/TlsStream.hx | 11 +++++ std/sys/tls/TlsStreamConfig.hx | 11 +++++ 19 files changed, 156 insertions(+), 38 deletions(-) create mode 100644 std/sys/net/Selector.hx create mode 100644 std/sys/tls/Certificate.hx create mode 100644 std/sys/tls/Identity.hx create mode 100644 std/sys/tls/TlsListener.hx create mode 100644 std/sys/tls/TlsListenerConfig.hx create mode 100644 std/sys/tls/TlsStream.hx create mode 100644 std/sys/tls/TlsStreamConfig.hx diff --git a/std/python/_std/sys/fs/DirEntry.hx b/std/python/_std/sys/fs/DirEntry.hx index ded5a22854e..901bd104b27 100644 --- a/std/python/_std/sys/fs/DirEntry.hx +++ b/std/python/_std/sys/fs/DirEntry.hx @@ -1,6 +1,12 @@ package sys.fs; -extern class DirEntry { - function path():Path; - function metadata():Metadata; +import haxe.exceptions.NotImplementedException; + +class DirEntry { + public function path():Path { + throw new NotImplementedException(); + } + public function metadata():Metadata { + throw new NotImplementedException(); + } } diff --git a/std/python/_std/sys/fs/File.hx b/std/python/_std/sys/fs/File.hx index c3a88b47bc4..a1004c12ce9 100644 --- a/std/python/_std/sys/fs/File.hx +++ b/std/python/_std/sys/fs/File.hx @@ -50,11 +50,9 @@ class File { throw new NotImplementedException(); } - final path:String; final file:RawIOBase; function new(path:String, file:RawIOBase) { - this.path = path; this.file = file; } @@ -73,7 +71,7 @@ class File { } public function metadata():Metadata { - throw new NotImplementedException(); + return new Metadata(Os.fstat(file.fileno())); } public function setPermissions(perm:Permissions):Void { @@ -83,12 +81,12 @@ class File { // TODO: technically these should all use a Int64 or even uint64 type, but `haxe.io.Bytes` uses an int length, so... public function read(bytes:haxe.io.Bytes, bufferOffset:Int, bufferLength:Int):Int { - final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), [bufferOffset, bufferOffset + bufferLength]); + final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), bufferOffset, bufferOffset + bufferLength); return file.readinto(view); } public function write(bytes:haxe.io.Bytes, bufferOffset:Int, bufferLength:Int):Int { - final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), [bufferOffset, bufferOffset + bufferLength]); + final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), bufferOffset, bufferOffset + bufferLength); return file.write(view); } @@ -104,6 +102,6 @@ class File { } public function close():Void { - throw new NotImplementedException(); + file.close(); } } diff --git a/std/python/_std/sys/fs/Fs.hx b/std/python/_std/sys/fs/Fs.hx index 8ec21f23f6c..55255abe4da 100644 --- a/std/python/_std/sys/fs/Fs.hx +++ b/std/python/_std/sys/fs/Fs.hx @@ -1,21 +1,52 @@ package sys.fs; +import haxe.exceptions.NotImplementedException; import sys.fs.Metadata; +import python.lib.Os; @:coreApi -extern class Fs { - static function metadata(path:Path):Metadata; - /** - Returns the metadata for `path` without following symlinks. - **/ - static function symlinkMetadata(path:Path):Metadata; - static function setPermissions(path:Path, perm:Permissions):Void; - static function copy(from:Path, to:Path):Void; - static function rename(from:Path, to:Path):Void; - static function readDir(path:Path):Iterator; - static function createDir(path:Path):Void; - static function createDirRec(path:Path):Void; - static function removeDir(path:Path):Void; - static function removeDirRec(path:Path):Void; - static function removeFile(path:Path):Void; +class Fs { + public static function metadata(path:Path):Metadata { + return new Metadata(Os.stat(path.toString())); + } + + public static function symlinkMetadata(path:Path):Metadata { + return new Metadata(Os.lstat(path.toString())); + } + + public static function setPermissions(path:Path, perm:Permissions):Void { + throw new NotImplementedException(); + } + + public static function copy(from:Path, to:Path):Void { + throw new NotImplementedException(); + } + + public static function rename(from:Path, to:Path):Void { + throw new NotImplementedException(); + } + + public static function readDir(path:Path):Iterator { + throw new NotImplementedException(); + } + + public static function createDir(path:Path):Void { + throw new NotImplementedException(); + } + + public static function createDirRec(path:Path):Void { + throw new NotImplementedException(); + } + + public static function removeDir(path:Path):Void { + throw new NotImplementedException(); + } + + public static function removeDirRec(path:Path):Void { + throw new NotImplementedException(); + } + + public static function removeFile(path:Path):Void { + throw new NotImplementedException(); + } } diff --git a/std/python/_std/sys/fs/Metadata.hx b/std/python/_std/sys/fs/Metadata.hx index ccf084f62ed..2430403abab 100644 --- a/std/python/_std/sys/fs/Metadata.hx +++ b/std/python/_std/sys/fs/Metadata.hx @@ -8,8 +8,8 @@ class Metadata { final stat:Stat; @:allow(sys.fs) - function new(path:String, followSymlinks:Bool) { - stat = followSymlinks ? Os.stat(path) : Os.lstat(path); + function new(stat:Stat) { + this.stat = stat; } public function isDir():Bool { @@ -25,7 +25,7 @@ class Metadata { } public function size():haxe.Int64 { - return stat.st_size + return stat.st_size; } public function permissions():Permissions { @@ -33,7 +33,6 @@ class Metadata { } public function modified():Null { - stat. throw new NotImplementedException(); } diff --git a/std/python/_std/sys/fs/Permissions.hx b/std/python/_std/sys/fs/Permissions.hx index 65b729f78d5..612c065fc98 100644 --- a/std/python/_std/sys/fs/Permissions.hx +++ b/std/python/_std/sys/fs/Permissions.hx @@ -1,9 +1,17 @@ package sys.fs; @:coreApi -extern class Permissions { +class Permissions { /** Note that this does not affect the file, use `Fs.setPermissions`. **/ - var readonly(get, set):Bool; + public var readonly(get, set):Bool; + + function get_readonly():Bool { + throw new haxe.exceptions.NotImplementedException(); + } + + function set_readonly(value:Bool):Bool { + throw new haxe.exceptions.NotImplementedException(); + } } \ No newline at end of file diff --git a/std/python/lib/Os.hx b/std/python/lib/Os.hx index b7e650db972..1f0582e6926 100644 --- a/std/python/lib/Os.hx +++ b/std/python/lib/Os.hx @@ -115,4 +115,6 @@ extern class Os { static function fdatasync(fd:Int):Void; static function lstat(path:String):Stat; + + static function fstat(fd:Int):Stat; } diff --git a/std/sys/fs/Permissions.hx b/std/sys/fs/Permissions.hx index 65b729f78d5..83ebd333456 100644 --- a/std/sys/fs/Permissions.hx +++ b/std/sys/fs/Permissions.hx @@ -3,7 +3,7 @@ package sys.fs; @:coreApi extern class Permissions { /** - Note that this does not affect the file, use `Fs.setPermissions`. + Note that changing this does not affect the file, use `Fs.setPermissions`. **/ var readonly(get, set):Bool; } \ No newline at end of file diff --git a/std/sys/net/DatagramSocket.hx b/std/sys/net/DatagramSocket.hx index a41cb6473a6..728937a257e 100644 --- a/std/sys/net/DatagramSocket.hx +++ b/std/sys/net/DatagramSocket.hx @@ -1,7 +1,6 @@ package sys.net; import haxe.io.Bytes; -import sys.net.IpAddress; /** A User Datagram Protocol socket. diff --git a/std/sys/net/Selector.hx b/std/sys/net/Selector.hx new file mode 100644 index 00000000000..fb9e31c451c --- /dev/null +++ b/std/sys/net/Selector.hx @@ -0,0 +1,28 @@ +package sys.net; + +import haxe.extern.EitherType; + +typedef Selectable = EitherType>; + +enum abstract Event(Int) { + var Read; + var Write; + var Other; + + @:op(A | B) function and(b:Event):Event; + @:op(A & B) function or(b:Event):Event; +} + +@:coreApi +extern class Selector { + function new(); + function register(socket:T, ev:Event, data:Null):SelectorKey; + function modify(socket:T, ev:Event, data:Null):SelectorKey; + function unregister(socket:T):SelectorKey; + + function wait(?timeout:Float):KeyValueIterator; +} + +extern class SelectorKey { + // var socket: +} \ No newline at end of file diff --git a/std/sys/net/SocketAddress.hx b/std/sys/net/SocketAddress.hx index 5bafd1de510..624368230eb 100644 --- a/std/sys/net/SocketAddress.hx +++ b/std/sys/net/SocketAddress.hx @@ -10,7 +10,7 @@ enum SocketAddress { } class SocketAddressTools { - public static function getBytes(s:SocketAddress):haxe.io.Bytes { + public static function toBytes(s:SocketAddress):haxe.io.Bytes { return switch s { case Ipv4(i, _): i.toBytes(); case Ipv6(i, _, _, _): i.toBytes(); diff --git a/std/sys/net/TcpListener.hx b/std/sys/net/TcpListener.hx index ff03619759e..8d0bc705289 100644 --- a/std/sys/net/TcpListener.hx +++ b/std/sys/net/TcpListener.hx @@ -8,13 +8,13 @@ extern class TcpListener { function localAddress():SocketAddress; function accept():TcpStream; - public var ttl(get, set):Int; + public var timeToLive(get, set):Int; public var nonBlocking(get, set):Bool; function close():Void; - private function get_ttl():Int; - private function set_ttl(value:Int):Int; + private function get_timeToLive():Int; + private function set_timeToLive(value:Int):Int; private function get_nonBlocking():Bool; private function set_nonBlocking(value:Bool):Bool; } diff --git a/std/sys/net/TcpStream.hx b/std/sys/net/TcpStream.hx index 2a38b09ab70..b94ca331b91 100644 --- a/std/sys/net/TcpStream.hx +++ b/std/sys/net/TcpStream.hx @@ -6,7 +6,8 @@ import haxe.time.Duration; @:coreApi extern class TcpStream { - static function connect(address:SocketAddress, ?timeout:Duration):TcpStream; + static function connectByIp(address:SocketAddress, ?timeout:Duration):TcpStream; + static function connectByName(hostname:String, port:Int, ?timeout:Duration):TcpStream; function peerAddress():SocketAddress; function localAddress():SocketAddress; function shutdown(how:Shutdown):Void; diff --git a/std/sys/net/UdpSocket.hx b/std/sys/net/UdpSocket.hx index 0eb0463d579..4771b0be20a 100644 --- a/std/sys/net/UdpSocket.hx +++ b/std/sys/net/UdpSocket.hx @@ -25,7 +25,7 @@ package sys.net; /** A UDP socket class **/ -@:deprecated("use UDPSocket (note the uppercase)") +@:deprecated("use DatagramSocket") class UdpSocket extends Socket { public function new() { throw "Not available on this platform"; diff --git a/std/sys/tls/Certificate.hx b/std/sys/tls/Certificate.hx new file mode 100644 index 00000000000..b174843e320 --- /dev/null +++ b/std/sys/tls/Certificate.hx @@ -0,0 +1,6 @@ +package sys.tls; + +extern class Certificate { + static function fromDer(bytes:haxe.io.Bytes):Certificate; + static function fromPem(bytes:haxe.io.Bytes):Certificate; +} diff --git a/std/sys/tls/Identity.hx b/std/sys/tls/Identity.hx new file mode 100644 index 00000000000..2a9406b7d49 --- /dev/null +++ b/std/sys/tls/Identity.hx @@ -0,0 +1,6 @@ +package sys.tls; + +extern class Identity { + static function fromPkcs12(der:haxe.io.Bytes, ?pass:String):Identity; + static function fromPkcs8(pem:haxe.io.Bytes, key:haxe.io.Bytes):Identity; +} diff --git a/std/sys/tls/TlsListener.hx b/std/sys/tls/TlsListener.hx new file mode 100644 index 00000000000..46018028852 --- /dev/null +++ b/std/sys/tls/TlsListener.hx @@ -0,0 +1,7 @@ +package sys.tls; + +extern class TlsListener extends sys.net.TcpListener { + static function bind(address:SocketAddress, config:TlsListenerConfig):TlsListener; + + override function accept():TlsStream; +} diff --git a/std/sys/tls/TlsListenerConfig.hx b/std/sys/tls/TlsListenerConfig.hx new file mode 100644 index 00000000000..bcb0729ccc7 --- /dev/null +++ b/std/sys/tls/TlsListenerConfig.hx @@ -0,0 +1,5 @@ +package sys.tls; + +@:structInit class TlsListenerConfig { + var identity:Identity; +} diff --git a/std/sys/tls/TlsStream.hx b/std/sys/tls/TlsStream.hx new file mode 100644 index 00000000000..99062aad448 --- /dev/null +++ b/std/sys/tls/TlsStream.hx @@ -0,0 +1,11 @@ +package sys.tls; + +import haxe.time.Duration; +import sys.net.SocketAddress; + +extern class TlsStream extends sys.net.TcpStream { + static function connectByIp(address:SocketAddress, config:TlsStreamConfig, ?timeout:Duration):TlsStream; + static function connectByName(hostname:String, port:Int, config:TlsStreamConfig, ?timeout:Duration):TlsStream; + + function peerCertificate():Null; +} diff --git a/std/sys/tls/TlsStreamConfig.hx b/std/sys/tls/TlsStreamConfig.hx new file mode 100644 index 00000000000..c38a0d50bc1 --- /dev/null +++ b/std/sys/tls/TlsStreamConfig.hx @@ -0,0 +1,11 @@ +package sys.tls; + +@:structInit class TlsStreamConfig { + var use_built_in_roots:Bool; + var extra_roots:Array = []; + + /** + For Server Name Indication + **/ + var hostname:Null = null; +}