📄 vpnclient.java
字号:
}
public IOStreamConnectorListener getRXIOListener() {
if (rxIo == null) {
rxIo = new RXIOStreamConnectorListener();
}
return rxIo;
}
public VPNConnectionListener startListeningSocket(Tunnel tunnel) throws IOException {
if (tunnel.getType() != Tunnel.LOCAL_TUNNEL)
throw new IOException("Invalid tunnel type " + tunnel.getType()
+ ": only local forwards can start a local listening socket");
VPNConnectionListener listener = super.startListeningSocket(tunnel);
PortItem pi = new PortItem(tunnel, listener);
PORTS.getModel().addPortItem(pi);
updateInformation();
return listener;
}
public MultiplexedConnection startRemoteForwarding(Tunnel tunnel) throws IOException {
MultiplexedConnection mc = super.startRemoteForwarding(tunnel);
PortItem pi = new PortItem(tunnel, mc);
PORTS.getModel().addPortItem(pi);
updateInformation();
return mc;
}
public void stopListeningSocket(String ticket) {
/* DEBUG */log.error("Stopping listening socket " + ticket);
VPNConnectionListener listener = (VPNConnectionListener) activeListeners.get(ticket);
MultiplexedConnection remoteConnection = (MultiplexedConnection) remoteListeners.get(ticket);
super.stopListeningSocket(ticket);
if (listener == null) {
PORTS.getModel().removeItemAt(PORTS.getModel().getIndexForMultiplexedConnection(remoteConnection));
}
else {
PORTS.getModel().removeItemAt(PORTS.getModel().getIndexForVPNConnectionListener(listener));
}
updateInformation();
}
public byte[] processHTTPRequest(String path, HTTPRequest request) throws RedirectException, IOException {
/* DEBUG */log.info("Processing HTTP request " + path);
/* DEBUG */Vector params = request.getParameters();
/* DEBUG */Enumeration pairs = params.elements();
/* DEBUG */while (pairs.hasMoreElements()) {
/* DEBUG */HTTPRequest.ParameterPair k = (HTTPRequest.ParameterPair) pairs.nextElement();
/* DEBUG */log.info(k.getName() + "=" + k.getValue());
/* DEBUG */}
if (path.equals("/launchApplication")) {
String redirect = request.getParameter("path");
try {
String additionalParams = launchApplication(request);
if (additionalParams != null) {
redirect = addRedirectParameters(redirect, additionalParams);
}
redirect = addRedirectParameter(redirect, "vpnMessage", "Application launched.");
} catch (Exception e) {
/* DEBUG */log.error("Failed to launch application. ", e);
redirect = addRedirectParameter(redirect, "vpnError", e.getMessage());
}
throw new RedirectException("https://" + getSSLExplorerHost() + ":" + getSSLExplorerPort() + redirect);
} else if (path.equals("/shutdown")) {
String redirect = request.getParameter("path");
if (redirect == null) {
redirect = "/logon.do";
}
startShutdownProcedure(true, false);
redirect = addRedirectParameter(redirect, "vpnMessage", "SSL-Explorer Agent shutdown.");
throw new RedirectException(getRedirectUrl(redirect));
} else if (path.equals("/openTunnel")) {
String redirect = "/showTunnels.do";
int portToBind = Integer.parseInt(request.getParameter("sourcePort"));
String transport = request.getParameter("transport");
String hostToConnect = request.getParameter("destinationHost");
int portToConnect = Integer.parseInt(request.getParameter("destinationPort"));
boolean autoStart = "on".equals(request.getParameter("autoStart"));
int type = Tunnel.LOCAL_TUNNEL;
if (request.containsParameter("tunnelType")) {
type = Integer.parseInt(request.getParameter("tunnelType"));
}
boolean allowExternalHosts = false;
if (request.containsParameter("allowExternalHosts"))
allowExternalHosts = "on".equals(request.getParameter("allowExternalHosts"));
/* DEBUG */log.info("Requesting tunnel for "
/* DEBUG */+ hostToConnect
/* DEBUG */+ ":"
/* DEBUG */+ portToConnect
/* DEBUG */+ " [allowExternalHosts="
/* DEBUG */+ allowExternalHosts
/* DEBUG */+ ", autoStart="
/* DEBUG */+ autoStart
/* DEBUG */+ "]");
DefaultTunnel tunnel = new DefaultTunnel(-1, type, autoStart, transport, username, portToBind, portToConnect,
hostToConnect, allowExternalHosts, true, false, hostToConnect + ":" + portToConnect, false);
try {
switch (type) {
case Tunnel.LOCAL_TUNNEL:
if (autoStart) {
VPNConnectionListener listener = startListeningSocket(tunnel);
tunnel.setId(listener.getId());
} else {
XMLElement xml = registerListeningSocket(false, tunnel);
if (xml == null) {
throw new Exception("Failed to register local tunnel with the server.");
}
tunnel.setId(xml.getIntAttribute("id"));
/* DEBUG */log.info("Registered tunnel with ID of " + tunnel.getId());
}
forwardingTunnels.addElement(tunnel);
break;
case Tunnel.REMOTE_TUNNEL:
if (autoStart) {
startRemoteForwarding(tunnel);
} else {
XMLElement xml = registerListeningSocket(false, tunnel);
if (xml == null) {
throw new Exception("Failed to register remote tunnel with the server.");
}
tunnel.setId(xml.getIntAttribute("id"));
/* DEBUG */log.info("Registered tunnel with ID of " + tunnel.getId());
}
remoteTunnels.addElement(tunnel);
break;
default:
/* DEBUG */log.info("Invalid tunnel type " + type);
}
updateInformation();
} catch (Exception e) {
/* DEBUG */log.error("Failed to start tunnel. ", e);
redirect = addRedirectParameter(redirect, "vpnError", "Failed to start tunnel. " + e.getMessage());
}
throw new RedirectException("https://" + getSSLExplorerHost() + ":" + getSSLExplorerPort() + redirect);
} else if (path.equals("/openURL")) {
String link = URLDecoder.decode(request.getParameter("link"));
String redirect = request.getParameter("path");
/* DEBUG */log.info("Request to open " + link + " via a temporary tunnel");
// Parse the link address and create a new address for the browser
// to use
URI url = new URI(link);
String linkHost = url.getHost();
int linkPort = url.getPort() == -1 ? (url.getScheme().equalsIgnoreCase("https") ? 443 : 80) : url.getPort();
// Start a tunnel
final VPNConnectionListener listener = createTemporaryListeningSocket(linkHost, linkPort, false, -1, 10, false);
if (listener == null) {
redirect = addRedirectParameter(redirect, "vpnError", "Could not create temporary tunnel.");
} else {
// Build up the url which the browser will be provided
StringBuffer buf = new StringBuffer();
buf.append(url.getScheme());
buf.append("://localhost:");
buf.append(listener.getLocalPort());
if (url.getPath() != null) {
if (!url.getPath().startsWith("/")) {
buf.append("/");
}
buf.append(url.getPath());
}
if (url.getQueryString() != null) {
buf.append("?");
buf.append(url.getQueryString());
}
// If getQuery is not supported then ref will be included in
// getFile
if (url.getFragment() != null) {
buf.append("#");
buf.append(url.getFragment());
}
// Wait a few seconds so the clients browser can catch up a bit
try {
Thread.sleep(3000);
} catch (InterruptedException ie) {
}
throw new RedirectException(buf.toString());
}
throw new RedirectException("https://" + getSSLExplorerHost() + ":" + getSSLExplorerPort() + redirect);
} else if (path.equals("/closeTunnel")) { // TODO is this used by
// anything
// other than port forwarding form
String redirect = request.getParameter("path");
String ticket = request.getParameter("ticket");
stopListeningSocket(ticket);
redirect = addRedirectParameter(redirect, "vpnMessage", "Tunnel closed.");
throw new RedirectException("https://" + getSSLExplorerHost() + ":" + getSSLExplorerPort() + redirect);
} else if (path.equals("/updateTunnel")) {
String redirect = request.getParameter("redirect");
String errorRedirect = request.getParameter("errorRedirect");
if (redirect == null) {
redirect = "/updateTunnel.do";
}
if (errorRedirect == null) {
errorRedirect = "/showTunnels.do";
}
String[] tickets = request.getParameterValues("ticket");
String error = null;
try {
if ("close".equalsIgnoreCase(request.getParameter("actionTarget"))) {
redirect = closeTunnels(redirect, tickets);
} else if ("open".equalsIgnoreCase(request.getParameter("actionTarget"))) {
redirect = openTunnels(redirect, tickets);
} else if ("remove".equalsIgnoreCase(request.getParameter("actionTarget"))) {
redirect = removeTunnels(redirect, tickets);
}
} catch (Exception e) {
redirect = addRedirectParameter(errorRedirect, "vpnError", e.getMessage());
}
redirect = addRedirectParameter(redirect, "actionTarget", request.getParameter("actionTarget"));
throw new RedirectException("https://" + getSSLExplorerHost() + ":" + getSSLExplorerPort() + redirect);
}
/* DEBUG */log.info("Request not found");
return null;
}
protected String getRedirectUrl(String path) {
try {
new URL(path);
return path;
} catch (Exception e) {
return "https://" + getSSLExplorerHost() + ":" + getSSLExplorerPort() + path;
}
}
protected String removeTunnels(String redirect, String[] tickets) throws Exception {
if (tickets == null || tickets.length == 0) {
throw new Exception("You must select at least one tunnel to remove.");
}
try {
for (int i = 0; i < tickets.length; i++) {
redirect = addRedirectParameter(redirect, "ticket", tickets[i]);
//
int idx = tickets[i].indexOf('/');
int id = -1;
try {
id = Integer.parseInt(tickets[i].substring(0, idx));
} catch (NumberFormatException nfe) {
}
String ticket = tickets[i].substring(idx + 1);
// We can only remove tunnels that are persistant, i.e. have an
// id
Tunnel tunnel = getPersistantForwardingTunnel(id);
if (tunnel != null) {
/* DEBUG */log.info("Removing tunnels with username of '" + tunnel.getUsername() + "'");
if (tunnel.getUsername().equals("")) {
/* DEBUG */log.error("Cannot remove global tunnels.");
throw new Exception("Only administrators can remove global tunnels.");
} else {
// The tunnel may be active
if (ticket.length() > 0) {
stopListeningSocket(ticket);
}
}
} else {
/* DEBUG */log.error("No tunnel with id of " + id + ". Probably a temporary tunnel.");
}
}
updateInformation();
} catch (Exception e) {
/* DEBUG */log.error("Failed to remove tunnels.", e);
throw new Exception("Failed to remove tunnel. " + e.getMessage());
}
return redirect;
}
protected String openTunnels(String redirect, String[] tickets) throws Exception {
if (tickets == null || tickets.length == 0) {
throw new Exception("You must select at least one tunnel to open.");
}
try {
for (int i = 0; i < tickets.length; i++) {
int idx = tickets[i].indexOf('/');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -