6d7e574c3f06c962a943705544f083b970a95ef6
[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.io.IOException;
22 import java.util.Map;
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;
38
39 /**
40  * Connects users to a tunnel associated with the authorized configuration
41  * having the given ID.
42  * 
43  * @author Michael Jumper
44  */
45 public class BasicGuacamoleTunnelServlet extends AuthenticatingHttpServlet {
46
47     private Logger logger = LoggerFactory.getLogger(BasicGuacamoleTunnelServlet.class);
48    
49     @Override
50     protected void authenticatedService(
51             Map<String, GuacamoleConfiguration> configs,
52             HttpServletRequest request, HttpServletResponse response)
53     throws IOException, ServletException {
54         
55         // If authenticated, respond as tunnel
56         tunnelServlet.service(request, response);
57         
58     }
59
60     /**
61      * Wrapped GuacamoleHTTPTunnelServlet which will handle all authenticated
62      * requests.
63      */
64     private GuacamoleHTTPTunnelServlet tunnelServlet = new GuacamoleHTTPTunnelServlet() {
65
66         @Override
67         protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
68
69             HttpSession httpSession = request.getSession(true);
70
71             // Get ID of connection
72             String id = request.getParameter("id");
73             
74             // Get authorized configs
75             Map<String, GuacamoleConfiguration> configs = (Map<String, GuacamoleConfiguration>) 
76                     httpSession.getAttribute("GUAC_CONFIGS");
77
78             // If no configs in session, not authorized
79             if (configs == null)
80                 throw new GuacamoleException("Cannot connect - user not logged in.");
81
82             // Get authorized config
83             GuacamoleConfiguration config = configs.get(id);
84             if (config == null) {
85                 logger.error("Error retrieving authorized configuration id={}.", id);
86                 throw new GuacamoleException("Unknown configuration ID.");
87             }
88             
89             logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
90
91             // Configure and connect socket
92             String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
93             int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
94
95             GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
96                     new InetGuacamoleSocket(hostname, port),
97                     config
98             );
99
100             // Associate socket with tunnel
101             GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);
102
103             // Attach tunnel to session
104             GuacamoleSession session = new GuacamoleSession(httpSession);
105             session.attachTunnel(tunnel);
106
107             return tunnel;
108
109         }
110
111     };
112
113 }
114