|
| 1 | +/* |
| 2 | + * Copyright (C)2009 - SSHJ Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.hierynomus.sshj.backport; |
| 17 | + |
| 18 | +import net.schmizz.sshj.common.IOUtils; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.net.*; |
| 23 | + |
| 24 | +public class Jdk7HttpProxySocket extends Socket { |
| 25 | + |
| 26 | + private Proxy httpProxy = null; |
| 27 | + |
| 28 | + public Jdk7HttpProxySocket(Proxy proxy) { |
| 29 | + super(proxy.type() == Proxy.Type.HTTP ? Proxy.NO_PROXY : proxy); |
| 30 | + if (proxy.type() == Proxy.Type.HTTP) { |
| 31 | + this.httpProxy = proxy; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void connect(SocketAddress endpoint, int timeout) throws IOException { |
| 37 | + if (httpProxy != null) { |
| 38 | + connectHttpProxy(endpoint, timeout); |
| 39 | + } else { |
| 40 | + super.connect(endpoint, timeout); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private void connectHttpProxy(SocketAddress endpoint, int timeout) throws IOException { |
| 45 | + super.connect(httpProxy.address(), timeout); |
| 46 | + |
| 47 | + if (!(endpoint instanceof InetSocketAddress)) { |
| 48 | + throw new SocketException("Expected an InetSocketAddress to connect to, got: " + endpoint); |
| 49 | + } |
| 50 | + InetSocketAddress isa = (InetSocketAddress) endpoint; |
| 51 | + String httpConnect = "CONNECT " + isa.getHostName() + ":" + isa.getPort() + " HTTP/1.0\n\n"; |
| 52 | + getOutputStream().write(httpConnect.getBytes(IOUtils.UTF8)); |
| 53 | + checkAndFlushProxyResponse(); |
| 54 | + } |
| 55 | + |
| 56 | + private void checkAndFlushProxyResponse()throws IOException { |
| 57 | + InputStream socketInput = getInputStream(); |
| 58 | + byte[] tmpBuffer = new byte[512]; |
| 59 | + int len = socketInput.read(tmpBuffer, 0, tmpBuffer.length); |
| 60 | + |
| 61 | + if (len == 0) { |
| 62 | + throw new SocketException("Empty response from proxy"); |
| 63 | + } |
| 64 | + |
| 65 | + String proxyResponse = new String(tmpBuffer, 0, len, IOUtils.UTF8); |
| 66 | + |
| 67 | + // Expecting HTTP/1.x 200 OK |
| 68 | + if (proxyResponse.contains("200")) { |
| 69 | + // Flush any outstanding message in buffer |
| 70 | + if (socketInput.available() > 0) { |
| 71 | + socketInput.skip(socketInput.available()); |
| 72 | + } |
| 73 | + // Proxy Connect Successful |
| 74 | + } else { |
| 75 | + throw new SocketException("Fail to create Socket\nResponse was:" + proxyResponse); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments