44123c30573acc57b537a287a12833528ffbb772
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / BasicGuacamoleTunnelServlet.java
1 package net.sourceforge.guacamole.net.basic;
2
3 /*
4  *  Guacamole - Clientless Remote Desktop
5  *  Copyright (C) 2010  Michael Jumper
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 import java.util.Map;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpSession;
24 import net.sourceforge.guacamole.GuacamoleException;
25 import net.sourceforge.guacamole.net.InetGuacamoleSocket;
26 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
27 import net.sourceforge.guacamole.properties.GuacamoleProperties;
28 import net.sourceforge.guacamole.net.GuacamoleSocket;
29 import net.sourceforge.guacamole.servlet.GuacamoleSession;
30 import net.sourceforge.guacamole.net.GuacamoleTunnel;
31 import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket;
32 import net.sourceforge.guacamole.servlet.GuacamoleHTTPTunnelServlet;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Connects users to a tunnel associated with the authorized configuration
38  * having the given ID.
39  * 
40  * @author Michael Jumper
41  */
42 public class BasicGuacamoleTunnelServlet extends GuacamoleHTTPTunnelServlet {
43
44     private Logger logger = LoggerFactory.getLogger(BasicGuacamoleTunnelServlet.class);
45     
46     @Override
47     protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
48
49         HttpSession httpSession = request.getSession(true);
50
51         // Get ID of connection
52         String id = request.getParameter("id");
53         
54         // Get authorized configs
55         Map<String, GuacamoleConfiguration> configs = (Map<String, GuacamoleConfiguration>) 
56                 httpSession.getAttribute("GUAC_CONFIGS");
57
58         // If no configs in session, not authorized
59         if (configs == null)
60             throw new GuacamoleException("Cannot connect - user not logged in.");
61
62         // Get authorized config
63         GuacamoleConfiguration config = configs.get(id);
64         if (config == null) {
65             logger.error("Error retrieving authorized configuration id={}.", id);
66             throw new GuacamoleException("Unknown configuration ID.");
67         }
68         
69         logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
70
71         // Configure and connect socket
72         String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
73         int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
74
75         GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
76                 new InetGuacamoleSocket(hostname, port),
77                 config
78         );
79
80         // Associate socket with tunnel
81         GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);
82
83         // Attach tunnel to session
84         GuacamoleSession session = new GuacamoleSession(httpSession);
85         session.attachTunnel(tunnel);
86
87         return tunnel;
88
89     }
90
91 }
92