📄 registerlisteningsocketaction.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.tunnels.actions;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jdom.Attribute;
import com.sslexplorer.boot.HostService;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.actions.XMLOutputAction;
import com.sslexplorer.forwarding.VPNListeningSocket;
import com.sslexplorer.policyframework.PolicyConstants;
import com.sslexplorer.policyframework.PolicyUtil;
import com.sslexplorer.policyframework.ResourceUtil;
import com.sslexplorer.security.SystemDatabase;
import com.sslexplorer.security.User;
import com.sslexplorer.security.VPNSession;
import com.sslexplorer.tunnels.DefaultTunnel;
import com.sslexplorer.tunnels.TransportType;
import com.sslexplorer.tunnels.Tunnel;
import com.sslexplorer.util.TicketGenerator;
public class RegisterListeningSocketAction extends XMLOutputAction {
final Log log = LogFactory.getLog(UnregisterListeningSocketAction.class);
public RegisterListeningSocketAction() {
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
int id = request.getParameter("id") == null ? -1 : Integer.parseInt(request.getParameter("id"));
VPNSession session = CoreServlet.getServlet().getLogonController().getVPNSession(request);
if (session != null) {
String ticket = TicketGenerator.getInstance().generateUniqueTicket("TUN");
Tunnel tunnel;
SystemDatabase sdb = CoreServlet.getServlet().getSystemDatabase();
PolicyUtil.checkPermission(PolicyConstants.VPN_CLIENT_RESOURCE_TYPE, PolicyConstants.PERM_USE, session.getSessionInfo().getUser());
// If the tunnel already has an ID then retrieve it
try {
if (id != -1) {
tunnel = sdb.getTunnel(id);
ResourceUtil.checkResourceAccessRights(tunnel, session.getSessionInfo());
} else {
// Update persistant tunnels or create a temporary tunnel
User user = session.getSessionInfo().getUser();
int portToBind = Integer.parseInt(request.getParameter("sourcePort"));
String hostToConnect = request.getParameter("destinationHost");
int portToConnect = Integer.parseInt(request.getParameter("destinationPort"));
boolean autoStart = "true".equalsIgnoreCase(request.getParameter("autoStart"));
boolean permanent = "true".equalsIgnoreCase(request.getParameter("permanent"));
boolean allowExternalHosts = "true".equalsIgnoreCase(request.getParameter("allowExternalHosts"));
String transport = request.getParameter("transport");
int type = TransportType.LOCAL_TUNNEL_ID;
try {
type = Integer.parseInt(request.getParameter("type"));
} catch (Throwable t) {
}
// Check that we are not already listening on this port
// (only remote tunnels)
if (type == TransportType.REMOTE_TUNNEL_ID) {
if (CoreServlet.getServlet().getConnectProxyMethodHandler().isAddressInUse(portToBind)) {
sendError(portToBind + " port is already in use", response);
return null;
}
}
if (permanent) {
throw new Exception("Since version 0.2.0, permanent tunnels are no longer created in this way.");
} else {
Calendar now = Calendar.getInstance();
tunnel = new DefaultTunnel("", "", -1, type, autoStart, transport, user.getPrincipalName(), portToBind,
new HostService(hostToConnect, portToConnect), allowExternalHosts, 0, now, now);
}
}
} catch (Exception e) {
log.error("Failed to register tunnel", e);
sendError(e.getMessage(), response);
return null;
}
boolean doNotStart = "false".equalsIgnoreCase(request.getParameter("start"));
if (doNotStart) {
Attribute[] attributes = new Attribute[1];
attributes[0] = new Attribute("id", String.valueOf(tunnel.getResourceId()));
sendSuccess(ticket, response, attributes);
} else {
VPNListeningSocket vls = new VPNListeningSocket(ticket, tunnel, session);
session.addListeningSocket(vls);
Attribute[] attributes = new Attribute[2];
attributes[0] = new Attribute("ticket", ticket);
attributes[1] = new Attribute("id", String.valueOf(tunnel.getResourceId()));
sendSuccess(ticket, response, attributes);
}
} else {
sendError("Invalid VPN session", response);
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -