1 package net.sourceforge.guacamole.net.basic;
4 * Guacamole - Clientless Remote Desktop
5 * Copyright (C) 2010 Michael Jumper
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import java.io.IOException;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpSession;
27 import net.sourceforge.guacamole.GuacamoleException;
28 import net.sourceforge.guacamole.net.InetGuacamoleSocket;
29 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
30 import net.sourceforge.guacamole.properties.GuacamoleProperties;
31 import net.sourceforge.guacamole.net.GuacamoleSocket;
32 import net.sourceforge.guacamole.servlet.GuacamoleSession;
33 import net.sourceforge.guacamole.net.GuacamoleTunnel;
34 import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket;
35 import net.sourceforge.guacamole.servlet.GuacamoleHTTPTunnelServlet;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Connects users to a tunnel associated with the authorized configuration
41 * having the given ID.
43 * @author Michael Jumper
45 public class BasicGuacamoleTunnelServlet extends AuthenticatingHttpServlet {
47 private Logger logger = LoggerFactory.getLogger(BasicGuacamoleTunnelServlet.class);
50 protected void authenticatedService(
51 Map<String, GuacamoleConfiguration> configs,
52 HttpServletRequest request, HttpServletResponse response)
53 throws IOException, ServletException {
55 // If authenticated, respond as tunnel
56 tunnelServlet.service(request, response);
61 * Wrapped GuacamoleHTTPTunnelServlet which will handle all authenticated
64 private GuacamoleHTTPTunnelServlet tunnelServlet = new GuacamoleHTTPTunnelServlet() {
67 protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
69 HttpSession httpSession = request.getSession(true);
71 // Get ID of connection
72 String id = request.getParameter("id");
74 // Get authorized configs
75 Map<String, GuacamoleConfiguration> configs = (Map<String, GuacamoleConfiguration>)
76 httpSession.getAttribute("GUAC_CONFIGS");
78 // If no configs in session, not authorized
80 throw new GuacamoleException("Cannot connect - user not logged in.");
82 // Get authorized config
83 GuacamoleConfiguration config = configs.get(id);
85 logger.error("Error retrieving authorized configuration id={}.", id);
86 throw new GuacamoleException("Unknown configuration ID.");
89 logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
91 // Configure and connect socket
92 String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
93 int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
95 GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
96 new InetGuacamoleSocket(hostname, port),
100 // Associate socket with tunnel
101 GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);
103 // Attach tunnel to session
104 GuacamoleSession session = new GuacamoleSession(httpSession);
105 session.attachTunnel(tunnel);