From 4e89b1696abcce5bc00dc746598f8c1d37159dd6 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 28 Nov 2010 21:33:44 -0800 Subject: [PATCH] Moving to session provider architecture for auth and connection config --- .../net/BasicGuacamoleSessionProvider.java | 38 ++++++++++++++++ .../guacamole/net/GuacamoleConfiguration.java | 36 +++++++++++++++ .../guacamole/net/GuacamoleServlet.java | 25 ++++++++++- .../guacamole/net/GuacamoleSession.java | 45 +++++++++++++++++++ .../net/GuacamoleSessionProvider.java | 29 ++++++++++++ .../net/NullGuacamoleSessionProvider.java | 31 +++++++++++++ .../guacamole/net/tunnel/Connect.java | 4 +- guacamole/web-client/web/META-INF/context.xml | 5 ++- 8 files changed, 206 insertions(+), 7 deletions(-) create mode 100644 guacamole/web-client/src/net/sourceforge/guacamole/net/BasicGuacamoleSessionProvider.java create mode 100644 guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSessionProvider.java create mode 100644 guacamole/web-client/src/net/sourceforge/guacamole/net/NullGuacamoleSessionProvider.java diff --git a/guacamole/web-client/src/net/sourceforge/guacamole/net/BasicGuacamoleSessionProvider.java b/guacamole/web-client/src/net/sourceforge/guacamole/net/BasicGuacamoleSessionProvider.java new file mode 100644 index 000000000..85dcb3fbd --- /dev/null +++ b/guacamole/web-client/src/net/sourceforge/guacamole/net/BasicGuacamoleSessionProvider.java @@ -0,0 +1,38 @@ + +package net.sourceforge.guacamole.net; + +import javax.servlet.http.HttpSession; +import net.sourceforge.guacamole.GuacamoleException; + +/* + * Guacamole - Clientless Remote Desktop + * Copyright (C) 2010 Michael Jumper + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +public class BasicGuacamoleSessionProvider implements GuacamoleSessionProvider { + + public GuacamoleSession createSession(HttpSession session) throws GuacamoleException { + + GuacamoleSession guacSession = new GuacamoleSession(session); + + guacSession.setConnection("vnc", "localhost", 5901); + guacSession.setPassword("potato"); + + return guacSession; + + } + +} diff --git a/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleConfiguration.java b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleConfiguration.java index 8de96acdb..ac56cb825 100644 --- a/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleConfiguration.java +++ b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleConfiguration.java @@ -19,13 +19,16 @@ package net.sourceforge.guacamole.net; * along with this program. If not, see . */ +import java.lang.reflect.InvocationTargetException; import javax.servlet.ServletContext; +import javax.servlet.http.HttpSession; import net.sourceforge.guacamole.GuacamoleException; public class GuacamoleConfiguration extends Configuration { private String guacd_hostname; private int guacd_port; + private GuacamoleSessionProvider sessionProvider; public GuacamoleConfiguration(ServletContext context) throws GuacamoleException { @@ -34,6 +37,34 @@ public class GuacamoleConfiguration extends Configuration { guacd_hostname = context.getInitParameter("guacd-hostname"); guacd_port = readIntParameter("guacd-port", null); + // Get session provider instance + try { + String sessionProviderClassName = context.getInitParameter("session-provider"); + Object obj = Class.forName(sessionProviderClassName).getConstructor().newInstance(); + if (!(obj instanceof GuacamoleSessionProvider)) + throw new GuacamoleException("Specified session provider class is not a GuacamoleSessionProvider"); + + sessionProvider = (GuacamoleSessionProvider) obj; + } + catch (ClassNotFoundException e) { + throw new GuacamoleException("Session provider class not found", e); + } + catch (NoSuchMethodException e) { + throw new GuacamoleException("Default constructor for session provider not present", e); + } + catch (SecurityException e) { + throw new GuacamoleException("Creation of session provider disallowed; check your security settings", e); + } + catch (InstantiationException e) { + throw new GuacamoleException("Unable to instantiate session provider", e); + } + catch (IllegalAccessException e) { + throw new GuacamoleException("Unable to access default constructor of session provider", e); + } + catch (InvocationTargetException e) { + throw new GuacamoleException("Internal error in constructor of session provider", e.getTargetException()); + } + } public int getProxyPort() { @@ -43,4 +74,9 @@ public class GuacamoleConfiguration extends Configuration { public String getProxyHostname() { return guacd_hostname; } + + public GuacamoleSession createSession(HttpSession session) throws GuacamoleException { + return sessionProvider.createSession(session); + } + } diff --git a/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleServlet.java b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleServlet.java index 5c0a55dd9..0ccbff9e3 100644 --- a/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleServlet.java +++ b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleServlet.java @@ -20,14 +20,28 @@ package net.sourceforge.guacamole.net; */ import java.io.IOException; +import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import net.sourceforge.guacamole.GuacamoleException; public abstract class GuacamoleServlet extends HttpServlet { + private GuacamoleConfiguration config; + + @Override + public void init(ServletConfig config) throws ServletException { + try { + this.config = new GuacamoleConfiguration(config.getServletContext()); + } + catch (GuacamoleException e) { + throw new ServletException(e); + } + } + @Override protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { @@ -49,8 +63,15 @@ public abstract class GuacamoleServlet extends HttpServlet { } private final void handleRequest(HttpServletRequest request, HttpServletResponse response) throws GuacamoleException { - GuacamoleSession session = new GuacamoleSession(request.getSession(shouldCreateSession())); - handleRequest(session, request, response); + + HttpSession httpSession = request.getSession(shouldCreateSession()); + + if (httpSession != null) { + GuacamoleSession session = config.createSession(httpSession); + handleRequest(session, request, response); + } + else + throw new GuacamoleException("No session"); } protected abstract void handleRequest(GuacamoleSession session, HttpServletRequest request, HttpServletResponse response) throws GuacamoleException; diff --git a/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSession.java b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSession.java index d95a6e9fe..650f2fce5 100644 --- a/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSession.java +++ b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSession.java @@ -35,6 +35,11 @@ public class GuacamoleSession { private SessionClient client; private ReentrantLock instructionStreamLock; + private String protocol; + private String hostname; + private int port; + private String password; + public class SessionClient extends Client implements HttpSessionBindingListener { private Client client; @@ -162,4 +167,44 @@ public class GuacamoleSession { return instructionStreamLock; } + public void setConnection(String protocol, String hostname, int port) { + this.protocol = protocol; + this.hostname = hostname; + this.port = port; + } + + public String getProtocol() { + return protocol; + } + + public String getHostname() { + return hostname; + } + + public int getPort() { + return port; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getConnectMessage() throws GuacamoleException { + + if (getProtocol() == null) + throw new GuacamoleException("Protocol not specified"); + + if (getHostname() == null) + throw new GuacamoleException("Hostname not specified"); + + if (getPassword() == null) + return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + ";"; + else + return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + "," + getPassword() + ";"; + } + } diff --git a/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSessionProvider.java b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSessionProvider.java new file mode 100644 index 000000000..3bcd25876 --- /dev/null +++ b/guacamole/web-client/src/net/sourceforge/guacamole/net/GuacamoleSessionProvider.java @@ -0,0 +1,29 @@ + +package net.sourceforge.guacamole.net; + +import javax.servlet.http.HttpSession; +import net.sourceforge.guacamole.GuacamoleException; + +/* + * Guacamole - Clientless Remote Desktop + * Copyright (C) 2010 Michael Jumper + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +public interface GuacamoleSessionProvider { + + public GuacamoleSession createSession(HttpSession session) throws GuacamoleException; + +} diff --git a/guacamole/web-client/src/net/sourceforge/guacamole/net/NullGuacamoleSessionProvider.java b/guacamole/web-client/src/net/sourceforge/guacamole/net/NullGuacamoleSessionProvider.java new file mode 100644 index 000000000..bb0e75532 --- /dev/null +++ b/guacamole/web-client/src/net/sourceforge/guacamole/net/NullGuacamoleSessionProvider.java @@ -0,0 +1,31 @@ + +package net.sourceforge.guacamole.net; + +import javax.servlet.http.HttpSession; +import net.sourceforge.guacamole.GuacamoleException; + +/* + * Guacamole - Clientless Remote Desktop + * Copyright (C) 2010 Michael Jumper + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +public class NullGuacamoleSessionProvider implements GuacamoleSessionProvider { + + public GuacamoleSession createSession(HttpSession session) throws GuacamoleException { + throw new GuacamoleException("Null provider will not create sessions"); + } + +} diff --git a/guacamole/web-client/src/net/sourceforge/guacamole/net/tunnel/Connect.java b/guacamole/web-client/src/net/sourceforge/guacamole/net/tunnel/Connect.java index d025c9255..79aa94d40 100644 --- a/guacamole/web-client/src/net/sourceforge/guacamole/net/tunnel/Connect.java +++ b/guacamole/web-client/src/net/sourceforge/guacamole/net/tunnel/Connect.java @@ -43,11 +43,9 @@ public class Connect extends GuacamoleServlet { // Obtain new connection session.connect(); - String connectString = "connect:vnc,localhost,5901,potato;"; - // Send data try { - char[] connect = connectString.toCharArray(); + char[] connect = session.getConnectMessage().toCharArray(); session.getClient().write(connect, 0, connect.length); session.getClient().authorize(); } diff --git a/guacamole/web-client/web/META-INF/context.xml b/guacamole/web-client/web/META-INF/context.xml index c63f9b865..240554b2f 100644 --- a/guacamole/web-client/web/META-INF/context.xml +++ b/guacamole/web-client/web/META-INF/context.xml @@ -2,7 +2,8 @@ - - + + +