mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 21:27:40 +00:00
Moving to session provider architecture for auth and connection config
This commit is contained in:
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -19,13 +19,16 @@ package net.sourceforge.guacamole.net;
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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() + ";";
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
public interface GuacamoleSessionProvider {
|
||||
|
||||
public GuacamoleSession createSession(HttpSession session) throws GuacamoleException;
|
||||
|
||||
}
|
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
public class NullGuacamoleSessionProvider implements GuacamoleSessionProvider {
|
||||
|
||||
public GuacamoleSession createSession(HttpSession session) throws GuacamoleException {
|
||||
throw new GuacamoleException("Null provider will not create sessions");
|
||||
}
|
||||
|
||||
}
|
@@ -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();
|
||||
}
|
||||
|
@@ -2,7 +2,8 @@
|
||||
<Context antiJARLocking="true" path="">
|
||||
|
||||
<!-- Hostname and port of guacamole proxy -->
|
||||
<Parameter name="guacd-hostname" value="localhost"/>
|
||||
<Parameter name="guacd-port" value="4822"/>
|
||||
<Parameter name="guacd-hostname" value="localhost"/>
|
||||
<Parameter name="guacd-port" value="4822"/>
|
||||
<Parameter name="session-provider" value="net.sourceforge.guacamole.net.BasicGuacamoleSessionProvider"/>
|
||||
|
||||
</Context>
|
||||
|
Reference in New Issue
Block a user