📄 abstractvpnclient.java
字号:
XMLElement xml = new XMLElement();
xml.parseFromReader(new InputStreamReader(response.getInputStream()));
if (xml.getName().equalsIgnoreCase("success")) {
// This is the ticket we will use for proxy authentication
registeredListeningSockets.put(xml.getContent(), tunnel);
return xml;
} else {
throw new Exception(xml.getContent());
}
} finally {
response.close();
}
}
public void stopListeningSocket(String ticket) {
// #ifdef DEBUG
log.info("Removing VPN connection listener with ticket " + ticket);
// #endif
VPNConnectionListener listener = (VPNConnectionListener) activeListeners.get(ticket);
if (listener == null) {
MultiplexedConnection remoteListener = (MultiplexedConnection) remoteListeners.get(ticket);
if (remoteListener == null) {
// #ifdef DEBUG
log.error("No VPN connection listener with ticket " + ticket);
// #endif
} else {
// #ifdef DEBUG
log.info("Disconnecting remote forwarding listener with ticket " + ticket);
// #endif
remoteListeners.remove(ticket);
unregisterListeningSocket((Tunnel) registeredListeningSockets.get(ticket));
remoteListener.disconnect(1, "Closing remote listener");
}
} else {
listener.stop();
activeListeners.remove(ticket);
}
}
protected void stopAllListeningSockets() {
for (Enumeration e = activeListeners.keys(); e.hasMoreElements();) {
stopListeningSocket((String) e.nextElement());
}
}
/**
* Create an application tunnel.
*
* @param tunnel
* @return
* @throws SSLException
* @throws IOException
*/
public Socket createTunnel(Tunnel tunnel) throws UnsupportedAuthenticationException, AuthenticationCancelledException,
IOException {
// Depending upon the configuration we may need to perform two proxy
// operations, the first for a external proxy, the second for the
// sslexplorer machine itself.
// #ifdef DEBUG
log.info("Creating SSL tunnel to " + tunnel.getDestinationHost() + ":" + tunnel.getDestinationPort());
// #endif
// Create a HTTP client
HttpClient client = getHttpClient();
try {
// Set up the SSL-Explorer connection method
HttpMethod connect = new ConnectMethod(tunnel.getDestinationHost(), tunnel.getDestinationPort(), false);
PasswordCredentials sslexplorerCredentials = new PasswordCredentials();
sslexplorerCredentials.setUsername(ticket);
sslexplorerCredentials.setPassword(tunnel.getTicket());
// #ifdef DEBUG
log.info("Set SSL-Explorer credentials to " + ticket + ":" + tunnel.getTicket());
// #endif
client.setCredentials(sslexplorerCredentials);
client.setPreemtiveAuthentication(true);
// We now have enough information to create a tunnel but first lets
// check
// to see if we need to route throug a local proxy
// If we have a local proxy then set its credentials
// CHECK Why are we doing this? Something similar is done in
// getHttpClient()
// if (localProxyURL != null) {
//
// client.setProxyHost(localProxyURL.getHost());
// client.setProxyPort(localProxyURL.getPort());
// client.setProxyType(localProxyURL.getScheme().equalsIgnoreCase("https")
// ? HttpClient.PROXY_HTTPS
// : HttpClient.PROXY_HTTP);
//
// // Add the proxy credentials
// PasswordCredentials proxyCredentials = new PasswordCredentials();
// String userInfo = localProxyURL.getUserinfo();
// String user = "";
// String password = "";
// if (userInfo != null && !userInfo.equals("")) {
// int idx = userInfo.indexOf(':');
// user = userInfo;
// if (idx != -1) {
// password = userInfo.substring(idx + 1);
// user = userInfo.substring(0, idx);
// }
// }
//
// /**
// * TODO: We could set a proxy authentication prompt here so that
// * if the local proxy credentials are incorrect the user has the
// * option to enter them manually.
// */
//
// if(!user.equals("")) {
// proxyCredentials.setUsername(user);
// proxyCredentials.setPassword(password);
// client.setProxyCredentials(proxyCredentials);
// }
//
// //#ifdef DEBUG
// log.info("Set local proxy to " +
// obfuscateURL(localProxyURL.toString()));
// //#endif
//
// }
HttpResponse response = client.execute(connect);
return response.getConnection().getSocket();
} catch (HttpException ex) {
throw new IOException("HTTP connection failed: " + ex.getMessage() + " [" + ex.getStatus() + "]");
}
/*
* return HttpsProxySocket.connectViaProxy(tunnel.getHostname(),
* tunnel.getPort(), sslexplorerHostname, sslexplorerPort, ticket,
* tunnel.getTicket(), "SSL Explorer Embedded VPN Client");
*/
}
/**
* @return Returns the defaultProxyCredentials.
*/
public PasswordCredentials getDefaultProxyCredentials() {
return defaultProxyCredentials;
}
/**
* @param defaultProxyCredentials The defaultProxyCredentials to set.
*/
public void setDefaultProxyCredentials(PasswordCredentials defaultProxyCredentials) {
this.defaultProxyCredentials = defaultProxyCredentials;
}
/**
* @return Returns the defaultProxyHost.
*/
public String getDefaultProxyHost() {
return defaultProxyHost;
}
/**
* @param defaultProxyHost The defaultProxyHost to set.
*/
public void setDefaultProxyHost(String defaultProxyHost) {
this.defaultProxyHost = defaultProxyHost;
}
/**
* @return Returns the defaultProxyPort.
*/
public int getDefaultProxyPort() {
return defaultProxyPort;
}
/**
* @param defaultProxyPort The defaultProxyPort to set.
*/
public void setDefaultProxyPort(int defaultProxyPort) {
this.defaultProxyPort = defaultProxyPort;
}
/**
* @return Returns the defaultProxyType.
*/
public int getDefaultProxyType() {
return defaultProxyType;
}
/**
* @param defaultProxyType The defaultProxyType to set.
*/
public void setDefaultProxyType(int defaultProxyType) {
this.defaultProxyType = defaultProxyType;
}
/**
* @return Returns the defaultProxyPreferredAuthentication.
*/
public String getDefaultProxyPreferredAuthentication() {
return defaultProxyPreferredAuthentication;
}
/**
* @param defaultProxyPreferredAuthentication The
* defaultProxyPreferredAuthentication to set.
*/
public void setDefaultProxyPreferredAuthentication(String defaultProxyPreferredAuthentication) {
this.defaultProxyPreferredAuthentication = defaultProxyPreferredAuthentication;
}
/**
* @return Returns the defaultProxyAuthenticationPrompt.
*/
public AuthenticationPrompt getDefaultProxyAuthenticationPrompt() {
return defaultProxyAuthenticationPrompt;
}
/**
* @param defaultProxyAuthenticationPrompt The
* defaultProxyAuthenticationPrompt to set.
*/
public void setDefaultProxyAuthenticationPrompt(AuthenticationPrompt defaultProxyAuthenticationPrompt) {
this.defaultProxyAuthenticationPrompt = defaultProxyAuthenticationPrompt;
}
/**
* @param localProxyURL
* @return
*/
protected static String obfuscateURL(String localProxyURL) {
try {
URI url = new URI(localProxyURL);
String userInfo = url.getUserinfo();
String user = "";
if (userInfo != null && !userInfo.equals("")) {
int idx = userInfo.indexOf(':');
user = userInfo;
if (idx != -1) {
user = userInfo.substring(0, idx);
}
return url.getScheme() + "://" + user + ":***@" + url.getHost() + ":" + url.getPort()
+ (url.getQueryString() != null ? ("?" + url.getQueryString()) : "");
} else {
return localProxyURL;
}
} catch (URI.MalformedURIException e) {
return localProxyURL;
}
}
/**
* Create an application tunnel.
*
* @param hostToConnect
* @param portToConnect
* @return
* @throws IOException
* @throws SSLException
*/
public Socket createTunnel(String hostToConnect, int portToConnect) throws IOException, HttpException,
UnsupportedAuthenticationException, AuthenticationCancelledException {
String key = hostToConnect + ":" + portToConnect;
Tunnel tunnel;
if (!activeDirectTunnels.containsKey(key)) {
tunnel = registerApplicationTunnel("[Undefined]", hostToConnect, portToConnect);
} else
tunnel = (Tunnel) activeDirectTunnels.get(key);
return createTunnel(tunnel);
}
/*
* public boolean promptForCredentials(HttpAuthenticator authenticator) {
* Panel p = new Panel(new GridBagLayout()); GridBagConstraints gbc = new
* GridBagConstraints(); gbc.anchor = GridBagConstraints.WEST; gbc.fill =
* GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(2, 2, 2, 2);
* // Realm if(authenticator instanceof BasicAuthentication) { gbc.weightx =
* 0.0; UIUtil.gridBagAdd(p, new Label("Realm:"), gbc,
* GridBagConstraints.RELATIVE); gbc.weightx = 1.0; UIUtil.gridBagAdd(p, new
* Label(((BasicAuthentication)authenticator).getRealm()), gbc,
* GridBagConstraints.REMAINDER); }
* // Realm TextField domain = null; if(authenticator instanceof
* NTLMAuthentication) { gbc.weightx = 0.0; UIUtil.gridBagAdd(p, new
* Label("Domain:"), gbc, GridBagConstraints.RELATIVE); domain = new
* TextField("", 15); gbc.weightx = 1.0; UIUtil.gridBagAdd(p, domain, gbc,
* GridBagConstraints.REMAINDER); }
* // Username gbc.weightx = 0.0; UIUtil.gridBagAdd(p, new
* Label("Username:"), gbc, GridBagConstraints.RELATIVE); final TextField
* username = new TextField("", 15); gbc.weightx = 1.0; UIUtil.gridBagAdd(p,
* username, gbc, GridBagConstraints.REMAINDER); username.requestFocus();
* // Password gbc.weightx = 0.0; UIUtil.gridBagAdd(p, new
* Label("Password:"), gbc, GridBagConstraints.RELATIVE); final TextField
* password = new TextField("", 15); gbc.weightx = 1.0; UIUtil.gridBagAdd(p,
* password, gbc, GridBagConstraints.REMAINDER); password.requestFocus();
* password.setEchoChar('*');
*
* final OptionDialog dialog = new OptionDialog(OptionDialog.QUESTION, p,
* OptionDialog.CHOICES_OK_CANCEL, null); username.addActionListener(new
* ActionListener() { public void actionPerformed(ActionEvent evt) {
* dialog.choice(OptionDialog.CHOICE_OK); } });
* password.addActionListener(new ActionListener() { public void
* actionPerformed(ActionEvent evt) { dialog.choice(OptionDialog.CHOICE_OK); }
* }); if(domain != null) { domain.addActionListener(new ActionListener() {
* public void actionPerformed(ActionEvent evt) {
* dialog.choice(OptionDialog.CHOICE_OK); } }); } if
* (dialog.dialogPrompt(null, "Proxy Server Authentication") ==
* OptionDialog.CHOICE_OK) { authenticator.setCredentials(new
* PasswordCredentials(username.getText().trim(), password.getText()));
* if(authenticator instanceof NTLMAuthentication &&
* !domain.getText().trim().equals("")) {
* ((NTLMAuthentication)authenticator).setDomain(domain.getText().trim()); }
* return true; }
*
* return false; }
*/
/**
* Get a <code>Properties</code> that may be sent back to the server so it
* can check if the client is in a supported environment. For example, the
* sun.os.patch.level property may be used to deny clients that are running
* Windows SP1.
*
* @return
*/
protected static Properties getSystemPropertiesToSend() {
Properties p = new Properties();
setIfNotEmpty("java.version", p);
setIfNotEmpty("java.vendor", p);
setIfNotEmpty("sun.os.patch.level", p);
setIfNotEmpty("os.name", p);
setIfNotEmpty("os.version", p);
setIfNotEmpty("os.arch", p);
return p;
}
protected static void setIfNotEmpty(String name, Properties p) {
String v = System.getProperty(name);
if (v != null && !v.equals("")) {
p.put(name, v);
}
}
public void onChannelOpen(Channel channel) {
}
public void onChannelClose(Channel channel) {
}
/**
*
* @return IOStreamConnectorListener
*/
public abstract IOStreamConnectorListener getTXIOListener();
public abstract IOStreamConnectorListener getRXIOListener();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -