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.net.GuacamoleTunnel;
33 import net.sourceforge.guacamole.net.auth.Credentials;
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");
75 Credentials credentials = getCredentials(httpSession);
77 // Get authorized configs
78 Map<String, GuacamoleConfiguration> configs = getConfigurations(httpSession);
80 // If no configs/credentials in session, not authorized
81 if (credentials == null || configs == null)
82 throw new GuacamoleException("Cannot connect - user not logged in.");
84 // Get authorized config
85 GuacamoleConfiguration config = configs.get(id);
87 logger.error("Error retrieving authorized configuration id={}.", id);
88 throw new GuacamoleException("Unknown configuration ID.");
91 logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
93 // Configure and connect socket
94 String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
95 int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
97 GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
98 new InetGuacamoleSocket(hostname, port),
102 // Associate socket with tunnel
103 GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);