break;
default:
}
if( !check ) {
throw new JSchException("Failed to connect ProxySOCKS5 (check)");
}
/*
The SOCKS request is formed as follows:
+----+-----+-------+------+----------+----------+
|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
+----+-----+-------+------+----------+----------+
| 1 | 1 | X'00' | 1 | Variable | 2 |
+----+-----+-------+------+----------+----------+
Where:
o VER protocol version: X'05'
o CMD
o CONNECT X'01'
o BIND X'02'
o UDP ASSOCIATE X'03'
o RSV RESERVED
o ATYP address type of following address
o IP V4 address: X'01'
o DOMAINNAME: X'03'
o IP V6 address: X'04'
o DST.ADDR desired destination address
o DST.PORT desired destination port in network octet
order
*/
index = 0;
buf[index++] = 5;
buf[index++] = 1; // CONNECT
buf[index++] = 0;
byte[] hostb = Util.str2byte(host);
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);
_proxyOut.write(buf, 0, index);
/*
The SOCKS request information is sent by the client as soon as it has
established a connection to the SOCKS server, and completed the
authentication negotiations. The server evaluates the request, and
returns a reply formed as follows:
+----+-----+-------+------+----------+----------+
|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
+----+-----+-------+------+----------+----------+
| 1 | 1 | X'00' | 1 | Variable | 2 |
+----+-----+-------+------+----------+----------+
Where:
o VER protocol version: X'05'
o REP Reply field:
o X'00' succeeded
o X'01' general SOCKS server failure
o X'02' connection not allowed by ruleset
o X'03' Network unreachable
o X'04' Host unreachable
o X'05' Connection refused
o X'06' TTL expired
o X'07' Command not supported
o X'08' Address type not supported
o X'09' to X'FF' unassigned
o RSV RESERVED
o ATYP address type of following address
o IP V4 address: X'01'
o DOMAINNAME: X'03'
o IP V6 address: X'04'
o BND.ADDR server bound address
o BND.PORT server bound port in network octet order
*/
//in.read(buf, 0, 4);
fill(_proxyIn, buf, 4);
if( buf[1] != 0 ) {
throw new JSchException("ProxySOCKS5: server returns " + buf[1]);
}
switch(buf[3] & 0xff) {
case 1:
//in.read(buf, 0, 6);
fill(_proxyIn, buf, 6);
break;
case 3:
//in.read(buf, 0, 1);
fill(_proxyIn, buf, 1);
//in.read(buf, 0, buf[0]+2);
fill(_proxyIn, buf, (buf[0] & 0xff) + 2);
break;
case 4:
//in.read(buf, 0, 18);
fill(_proxyIn, buf, 18);
break;
default:
}
} catch(JSchException e) {
close(); // If error occured, close all resources!
throw e;
} catch(Exception e) {
close(); // If error occured, close all resources!
throw new JSchException("Failed to connect ProxySOCKS5: "+e, e);
}
}