, ; .
HTTP 1.1 CONNECT .
Java
final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
JSch jsch = new JSch();
Session session = jsch.getSession("root", "SSH-server", 22);
session.setSocketFactory(new SocketFactory() {
Socket tunnel = null;
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
SSLSocketFactory ssf = sc.getSocketFactory();
tunnel = ssf.createSocket(System.getProperty("https.proxyHost"), Integer.getInteger("https.proxyPort"));
tunnel.setKeepAlive(true);
doTunnelHandshake(tunnel, host, port);
System.out.println(tunnel + " connect " + tunnel.isConnected());
return tunnel;
}
public InputStream getInputStream(Socket socket) throws IOException {
System.out.println(tunnel + " getInputStream " + socket.isConnected());
return tunnel.getInputStream();
}
public OutputStream getOutputStream(Socket socket) throws IOException {
System.out.println("getOutputStream");
return socket.getOutputStream();
} });
session.connect();
try {
session.setPortForwardingR(3391, "localhost", 3389);
....
private static void doTunnelHandshake(Socket tunnel, String host, int port) throws IOException {
OutputStream out = tunnel.getOutputStream();
String msg = "CONNECT " + host + ":" + port + " HTTP/1.0\n" +
"User-Agent: " +
sun.net.www.protocol.http.HttpURLConnection.userAgent + "\r\n\r\n";
byte b[];
try {
b = msg.getBytes("ASCII7");
} catch (UnsupportedEncodingException ignored) {
b = msg.getBytes();
}
out.write(b);
out.flush();
byte reply[] = new byte[200];
int replyLen = 0;
int newlinesSeen = 0;
boolean headerDone = false;
InputStream in = tunnel.getInputStream();
boolean error = false;
while (newlinesSeen < 2) {
int i = in.read();
if (i < 0) {
throw new IOException("Unexpected EOF from proxy");
}
if (i == '\n') {
headerDone = true;
++newlinesSeen;
} else if (i != '\r') {
newlinesSeen = 0;
if (!headerDone && replyLen < reply.length) {
reply[replyLen++] = (byte) i;
}
}
}
String replyStr;
try {
replyStr = new String(reply, 0, replyLen, "ASCII7");
} catch (UnsupportedEncodingException ignored) {
replyStr = new String(reply, 0, replyLen);
}
System.out.println(replyStr);
if (!replyStr.startsWith("HTTP/1.0 200")) {
throw new IOException("Unable to tunnel for " + host + ":" + port + ". Proxy returns \"" + replyStr + "\"");
}
}
apache
ssl
SSLEngine on
SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateKeyFile "conf/ssl.key/server.key"
Connecting to localhost port 22
HTTP/1.0 200 Connection Established
....
Authentications that can continue: password
Next authentication method: password
Authentication succeeded (password).
Connected