if (!check) {
try {
socket.close();
} catch (Exception ignore) {
}
throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "check failed");
}
// 5) CONNECT
index = 0;
buf[index++] = 5;
buf[index++] = 1; // CONNECT
buf[index++] = 0;
byte[] hostb = host.getBytes();
int len = hostb.length;
buf[index++] = 3; // DOMAINNAME
buf[index++] = (byte) (len);
System.arraycopy(hostb, 0, buf, index, len);
index += len;
buf[index++] = (byte) (port >>> 8);
buf[index++] = (byte) (port & 0xff);
out.write(buf, 0, index);
// 6) RESPONSE
// in.read(buf, 0, 4);
fill(in, buf, 4);
if (buf[1] != 0) {
try {
socket.close();
} catch (Exception ignore) {
}
throw new HttpException(ProxyInfo.ProxyType.SOCKS5, "proxy returned " + buf[1]);
}
switch (buf[3] & 0xff) {
case 1:
// in.read(buf, 0, 6);
fill(in, buf, 6);
break;
case 3:
// in.read(buf, 0, 1);
fill(in, buf, 1);
// in.read(buf, 0, buf[0]+2);
fill(in, buf, (buf[0] & 0xff) + 2);
break;
case 4:
// in.read(buf, 0, 18);
fill(in, buf, 18);
break;
default:
}
return socket;
} catch (RuntimeException rttex) {
closeSocket(socket);
throw rttex;
} catch (Exception ex) {
closeSocket(socket);
throw new HttpException(ProxyInfo.ProxyType.SOCKS5, ex.toString(), ex);
}
}