changeset 1204:5d0cbd908582

remove Io socket support add SSL for Rpc
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Mar 2018 19:48:15 -0700
parents 7e6d132904fd
children 8f14426f8ada
files src/luan/modules/Io.luan src/luan/modules/IoLuan.java src/luan/modules/Rpc.luan
diffstat 3 files changed, 34 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
diff -r 7e6d132904fd -r 5d0cbd908582 src/luan/modules/Io.luan
--- a/src/luan/modules/Io.luan	Tue Mar 06 13:29:56 2018 -0700
+++ b/src/luan/modules/Io.luan	Tue Mar 06 19:48:15 2018 -0700
@@ -10,7 +10,7 @@
 Io.schemes = IoLuan.newSchemes()
 Io.uri = IoLuan.uri
 Io.stdin = IoLuan.defaultStdin.table()
-Io.socket_server = IoLuan.socket_server
+--Io.socket_server = IoLuan.socket_server
 Io.stdout = IoLuan.textWriter(System.out)
 Io.stderr = IoLuan.textWriter(System.err)
 Io.unrestricted = IoLuan.unrestricted
diff -r 7e6d132904fd -r 5d0cbd908582 src/luan/modules/IoLuan.java
--- a/src/luan/modules/IoLuan.java	Tue Mar 06 13:29:56 2018 -0700
+++ b/src/luan/modules/IoLuan.java	Tue Mar 06 19:48:15 2018 -0700
@@ -682,7 +682,7 @@
 			add( schemes, "string", String.class );
 			add( schemes, "file", LuanState.class, String.class );
 			add( schemes, "classpath", LuanState.class, String.class );
-			add( schemes, "socket", String.class );
+//			add( schemes, "socket", String.class );
 			add( schemes, "http", LuanState.class, String.class, LuanTable.class );
 			add( schemes, "https", LuanState.class, String.class, LuanTable.class );
 			add( schemes, "luan", LuanState.class, String.class );
@@ -717,7 +717,7 @@
 			throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
 		return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
 	}
-
+/*
 	public static final class LuanSocket extends LuanIO {
 		public final Socket socket;
 
@@ -778,7 +778,7 @@
 			}
 		};
 	}
-
+*/
 
 	public static class BaseOs extends LuanIO {
 		private final String cmd;
diff -r 7e6d132904fd -r 5d0cbd908582 src/luan/modules/Rpc.luan
--- a/src/luan/modules/Rpc.luan	Tue Mar 06 13:29:56 2018 -0700
+++ b/src/luan/modules/Rpc.luan	Tue Mar 06 19:48:15 2018 -0700
@@ -1,4 +1,8 @@
 java()
+local Socket = require "java:java.net.Socket"
+local ServerSocket = require "java:java.net.ServerSocket"
+local SSLSocketFactory = require "java:javax.net.ssl.SSLSocketFactory"
+local SSLServerSocketFactory = require "java:javax.net.ssl.SSLServerSocketFactory"
 local RpcClient = require "java:luan.lib.rpc.RpcClient"
 local RpcServer = require "java:luan.lib.rpc.RpcServer"
 local RpcCall = require "java:luan.lib.rpc.RpcCall"
@@ -27,6 +31,8 @@
 
 Rpc.port = 9101
 
+Rpc.cipher_suite = "SSL_DH_anon_WITH_RC4_128_MD5"
+
 local function java_args(list)
 	for i,v in ipairs(list) do
 		list[i] = JavaLuan.toJava(v)
@@ -75,9 +81,8 @@
 	return binary_in, len_in
 end
 
-function Rpc.caller(socket)
-	local java_socket = socket.java.socket
-	local client = RpcClient.new(java_socket)
+local function rpc_caller(socket)
+	local client = RpcClient.new(socket)
 	return function(fn_name,...)
 		local args = {...}
 		local binary_in, len_in = encode_binary(args)
@@ -111,10 +116,8 @@
 
 Rpc.functions = {}
 
-function Rpc.responder(socket,fns)
-	fns = fns or Rpc.functions
-	local java_socket = socket.java.socket
-	local server = RpcServer.new(java_socket)
+local function rpc_responder(socket,fns)
+	local server = RpcServer.new(socket)
 	local responder = {}
 	function responder.is_closed()
 		return server.isClosed()
@@ -156,9 +159,15 @@
 	return responder
 end_function
 
-function Rpc.remote_socket(socket_uri)
-	local socket = Io.uri(socket_uri)
-	local call = Rpc.caller(socket)
+function Rpc.remote(domain)
+	local socket
+	if Rpc.cipher_suite == nil then
+		socket = Socket.new(domain,Rpc.port)
+	else
+		socket = SSLSocketFactory.getDefault().createSocket(domain,Rpc.port)
+		socket.setEnabledCipherSuites{Rpc.cipher_suite}
+	end
+	local call = rpc_caller(socket)
 	local mt = {}
 	function mt.__index(_,key)
 		return function(...)
@@ -170,21 +179,24 @@
 	return t
 end_function
 
-function Rpc.remote(domain)
-	local socket = "socket:"..domain..":"..Rpc.port
-	return Rpc.remote_socket(socket)
-end_function
-
 function Rpc.serve(port,fns)
-	local socket_server = Io.socket_server(port or Rpc.port)
+	port = port or Rpc.port
+	fns = fns or Rpc.functions
+	local socket_server
+	if Rpc.cipher_suite == nil then
+		socket_server = ServerSocket.new(port)
+	else
+		socket_server = SSLServerSocketFactory.getDefault().createServerSocket(port)
+		socket_server.setEnabledCipherSuites{Rpc.cipher_suite}
+	end
 	while true do
 		try {
 			function()
-				local socket = socket_server()
+				local socket = socket_server.accept()
 				local function server()
 					try {
 						function()
-							local responder = Rpc.responder(socket)
+							local responder = rpc_responder(socket,fns)
 							while not responder.is_closed() do
 								responder.respond()
 							end