1 19 20 package com.sslexplorer.reverseproxy.actions; 21 22 import java.net.URL ; 23 import java.util.Collection ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.struts.action.ActionForm; 30 import org.apache.struts.action.ActionForward; 31 import org.apache.struts.action.ActionMapping; 32 33 import com.sslexplorer.boot.HostService; 34 import com.sslexplorer.boot.Util; 35 import com.sslexplorer.core.CoreEvent; 36 import com.sslexplorer.core.CoreServlet; 37 import com.sslexplorer.core.actions.AuthenticatedAction; 38 import com.sslexplorer.core.stringreplacement.VariableReplacement; 39 import com.sslexplorer.policyframework.LaunchSession; 40 import com.sslexplorer.policyframework.LaunchSessionFactory; 41 import com.sslexplorer.policyframework.NoPermissionException; 42 import com.sslexplorer.policyframework.ResourceAccessEvent; 43 import com.sslexplorer.properties.Property; 44 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey; 45 import com.sslexplorer.security.LogonControllerFactory; 46 import com.sslexplorer.security.SessionInfo; 47 import com.sslexplorer.webforwards.ReverseProxyWebForward; 48 import com.sslexplorer.webforwards.WebForwardEventConstants; 49 import com.sslexplorer.webforwards.WebForwardPlugin; 50 import com.sslexplorer.webforwards.WebForwardTypeItem; 51 import com.sslexplorer.webforwards.WebForwardTypes; 52 53 61 public class LaunchReverseProxyAction extends AuthenticatedAction { 62 63 67 public LaunchReverseProxyAction() { 68 } 69 70 75 protected boolean isIgnoreSessionLock() { 76 return true; 77 } 78 79 public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 80 throws Exception { 81 83 String launchId = request.getParameter(LaunchSession.LAUNCH_ID); 84 if (Util.isNullOrTrimmedBlank(launchId)) { 85 throw new Exception ("No launch ID supplied."); 86 } 87 88 LaunchSession launchSession = LaunchSessionFactory.getInstance().getLaunchSession(launchId); 89 ReverseProxyWebForward wf = (ReverseProxyWebForward) launchSession.getResource(); 90 91 94 Collection <LaunchSession> sessions = LaunchSessionFactory.getInstance().getLaunchSessionsForType(launchSession.getSession(), 95 WebForwardPlugin.WEBFORWARD_RESOURCE_TYPE); 96 for (LaunchSession rs : sessions) { 97 if (rs != launchSession && rs.getResource() instanceof ReverseProxyWebForward && rs.getResource().getResourceId() == wf.getResourceId()) { 98 LaunchSessionFactory.getInstance().removeLaunchSession(rs); 99 } 100 } 101 102 if (wf.getActiveDNS() && !isValidForActiveDNS(request.getServerName())) 103 throw new Exception ("Invalid host '" + request.getServerName() + "'; only FQDNs are valid for Active DNS forwarding"); 104 105 String path; 106 String url = wf.getDestinationURL(); 107 String hostField = request.getHeader("Host"); 108 HostService hostService = hostField == null ? null : new HostService(hostField); 109 SessionInfo session = getSessionInfo(request); 110 111 try { 112 launchSession.checkAccessRights(null, session); 113 114 122 123 140 141 VariableReplacement r = new VariableReplacement(); 142 r.setServletRequest(request); 143 r.setLaunchSession(launchSession); 144 url = r.replace(url); 145 146 CoreEvent evt = new ResourceAccessEvent(this, 147 WebForwardEventConstants.WEB_FORWARD_STARTED, 148 wf, 149 launchSession.getPolicy(), 150 launchSession.getSession(), 151 CoreEvent.STATE_SUCCESSFUL).addAttribute(WebForwardEventConstants.EVENT_ATTR_WEB_FORWARD_TYPE, 152 ((WebForwardTypeItem) WebForwardTypes.WEB_FORWARD_TYPES.get(wf.getType())).getName()) 153 .addAttribute(WebForwardEventConstants.EVENT_ATTR_WEB_FORWARD_URL, url); 154 155 CoreServlet.getServlet().fireCoreEvent(evt); 156 157 if (wf.getActiveDNS()) { 159 URL u = new URL (url); 160 URL adu; 161 if (Property.getPropertyInt(new SystemConfigKey("webforward.activeDNSFormat")) == 1) { 162 adu = new URL ("https", launchSession.getId() + "." + hostService.getHost(), hostService.getPort() == 0 ? -1 163 : hostService.getPort(), u.getFile()); 164 } else { 165 int idx = hostService.getHost().indexOf('.'); 166 adu = new URL ("https", 167 launchSession.getId() + "." + hostService.getHost().substring(idx + 1), 168 hostService.getPort() == 0 ? -1 : hostService.getPort(), 169 u.getFile()); 170 } 171 path = adu.toExternalForm(); 172 173 } else if (wf.getHostHeader() != null && !wf.getHostHeader().equals("")) { 174 URL u = new URL (url); 175 176 URL adu = new URL ("https", wf.getHostHeader(), hostService.getPort() == 0 ? -1 : hostService.getPort(), u.getFile()); 177 178 path = adu.toExternalForm(); 179 180 if (adu.getQuery() == null || adu.getQuery().equals("")) { 181 path += "?" + LaunchSession.LAUNCH_ID + "=" + launchSession.getId(); 182 } else { 183 path += "&" + LaunchSession.LAUNCH_ID + "=" + launchSession.getId(); 184 } 185 186 195 URL adu2 = new URL ("https", 196 hostService.getHost(), 197 hostService.getPort() == 0 ? -1 : hostService.getPort(), 198 "/reverseProxyRedirect.jsp?redirectURL=" + Util.urlEncode(path)); 199 200 return new ActionForward(adu2.toExternalForm(), true); 201 202 } else { 203 URL u = new URL (url); 204 path = u.getPath(); 205 if (u.getQuery() == null || u.getQuery().equals("")) { 206 path += "?" + LaunchSession.LONG_LAUNCH_ID + "=" + launchSession.getId(); 207 } else { 208 path += "?" + u.getQuery() + "&" + LaunchSession.LONG_LAUNCH_ID + "=" + launchSession.getId(); 209 } 210 211 URL redir = new URL ("https", 212 hostService.getHost(), 213 hostService.getPort() == 0 ? -1 : hostService.getPort(), 214 path); 215 path = redir.toExternalForm(); 216 } 217 } catch (NoPermissionException npe) { 218 219 CoreEvent evt = new ResourceAccessEvent(this, 220 WebForwardEventConstants.WEB_FORWARD_STARTED, 221 wf, 222 launchSession.getPolicy(), 223 launchSession.getSession(), 224 npe).addAttribute(WebForwardEventConstants.EVENT_ATTR_WEB_FORWARD_TYPE, 225 ((WebForwardTypeItem) WebForwardTypes.WEB_FORWARD_TYPES.get(wf.getType())).getName()) 226 .addAttribute(WebForwardEventConstants.EVENT_ATTR_WEB_FORWARD_URL, url); 227 CoreServlet.getServlet().fireCoreEvent(evt); 228 229 throw npe; 230 } 231 232 return new ActionForward(path, true); 233 } 234 235 238 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 239 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT; 240 } 241 242 private boolean isValidForActiveDNS(String host) { 243 244 StringTokenizer tokens = new StringTokenizer (host, "."); 245 if (tokens.countTokens() == 1) 246 return false; 247 248 boolean numerical = true; 249 while (tokens.hasMoreTokens()) { 250 String token = tokens.nextToken(); 251 252 try { 253 int val = Integer.parseInt(token); 254 255 if (val > 255) { 256 numerical = false; 257 break; 258 } 259 } catch (NumberFormatException ex) { 260 numerical = false; 261 break; 262 } 263 } 264 265 return !numerical; 266 267 } 268 269 } 270 | Popular Tags |