Просмотр исходного кода

[python] add very basic ssl support for http requests

frabbit 8 лет назад
Родитель
Сommit
659dfa63bd

+ 7 - 3
std/python/_std/sys/net/Socket.hx

@@ -136,8 +136,12 @@ private class SocketOutput extends haxe.io.Output {
     public function new() : Void {
     }
 
-    function __init() : Void  {
+    function __initSocket ():Void {
         __s = new PSocket();
+    }
+
+    function __init() : Void  {
+        __initSocket();
         input = new SocketInput(__s);
         output = new SocketOutput(__s);
     }
@@ -169,7 +173,7 @@ private class SocketOutput extends haxe.io.Output {
     public function connect( host : Host, port : Int ) : Void {
         __init();
         var host_str = host.toString();
-        __s.connect(python.Syntax.pythonCode("(host_str,port)"));
+        __s.connect(Tuple2.make(host_str,port));
     }
 
     /**
@@ -191,7 +195,7 @@ private class SocketOutput extends haxe.io.Output {
     public function bind( host : Host, port : Int ) : Void {
         __init();
         var host_str = host.toString();
-        __s.bind(python.Syntax.pythonCode("(host_str,port)"));
+        __s.bind(Tuple2.make(host_str,port));
     }
 
     /**

+ 46 - 0
std/python/lib/Ssl.hx

@@ -0,0 +1,46 @@
+/*
+ * Copyright (C)2005-2017 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package python.lib;
+
+import python.lib.ssl.SSLContext;
+
+@:pythonImport("ssl")
+extern class Ssl {
+
+	public static function create_default_context(purpose:String):SSLContext;
+
+	/**
+		Prevents a TLSv1 connection. This option is only applicable in conjunction
+		with PROTOCOL_TLS. It prevents the peers from choosing TLSv1 as the
+		protocol version.
+	**/
+	public static var OP_NO_TLSv1:Int;
+	/**
+		Prevents a TLSv1.1 connection. This option is only applicable in conjunction
+		with PROTOCOL_TLS. It prevents the peers from choosing TLSv1.1 as the
+		protocol version. Available only with openssl version 1.0.1+.
+
+		since python 3.4
+	**/
+	public static var OP_NO_TLSv1_1:Int;
+
+}

+ 7 - 0
std/python/lib/ssl/Purpose.hx

@@ -0,0 +1,7 @@
+package python.lib.ssl;
+
+@:pythonImport("ssl", "Purpose")
+extern class Purpose {
+	public static var SERVER_AUTH:String;
+	public static var CLIENT_AUTH:String;
+}

+ 36 - 0
std/python/lib/ssl/SSLContext.hx

@@ -0,0 +1,36 @@
+/*
+ * Copyright (C)2005-2017 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package python.lib.ssl;
+
+import python.lib.ssl.SSLSocket;
+
+@:pythonImport("ssl", "SSLContext")
+extern class SSLContext {
+	public function wrap_socket(s:python.lib.net.Socket, server_side:Bool = false, do_handshake_on_connect:Bool = true, suppress_ragged_eofs:Bool = true, server_hostname:String = null ):python.lib.ssl.SSLSocket;
+	public var options:Int;
+
+	//public function load_default_certs():Void;
+	//public function load_cert_chain(certfile:String, keyfile:String = null, password:String = null):Void;
+	//public function set_servername_callback(callback:SSLSocket -> String -> SSLContext -> Void ):Void;
+	//public var check_hostname:Bool;
+	//public var verify_mode:Int;
+}

+ 27 - 0
std/python/lib/ssl/SSLSocket.hx

@@ -0,0 +1,27 @@
+/*
+ * Copyright (C)2005-2017 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package python.lib.ssl;
+
+@:pythonImport("ssl", "SSLSocket")
+extern class SSLSocket extends python.lib.net.Socket {
+
+}

+ 31 - 0
std/python/net/SslSocket.hx

@@ -0,0 +1,31 @@
+package python.net;
+
+import python.lib.Ssl;
+import python.lib.ssl.Purpose;
+import python.lib.net.Socket as PSocket;
+import sys.net.Host;
+
+class SslSocket extends sys.net.Socket {
+
+	var hostName:String;
+
+	override function __initSocket ():Void {
+		var context = Ssl.create_default_context(Purpose.SERVER_AUTH);
+		context.options |= Ssl.OP_NO_TLSv1; // python 3.4 | Ssl.OP_NO_TLSv1_1;
+		__s = new PSocket();
+		__s = context.wrap_socket(__s,
+			false,
+			true,
+			true,
+			this.hostName
+		);
+	}
+
+	public override function connect( host : Host, port : Int ) : Void {
+		 this.hostName = host.host;
+		 super.connect(host, port);
+	}
+	public override function bind( host : Host, port : Int ) : Void {
+		throw "not implemented";
+	}
+}

+ 2 - 0
std/sys/Http.hx

@@ -90,6 +90,8 @@ class Http extends haxe.http.HttpBase {
 				sock = new php.net.SslSocket();
 				#elseif java
 				sock = new java.net.SslSocket();
+				#elseif python
+				sock = new python.net.SslSocket();
 				#elseif (!no_ssl && (hxssl || hl || cpp || (neko && !(macro || interp))))
 				sock = new sys.ssl.Socket();
 				#else