6852108: Remove Preferences dependance from SocksSocketImpl
Removed Preferences API use and fixed a few findbugs gotchas Reviewed-by: alanb
This commit is contained in:
parent
1d5c589340
commit
712e4baa4b
@ -29,7 +29,6 @@ import java.io.OutputStream;
|
|||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.security.AccessController;
|
import java.security.AccessController;
|
||||||
import java.security.PrivilegedExceptionAction;
|
import java.security.PrivilegedExceptionAction;
|
||||||
import java.util.prefs.Preferences;
|
|
||||||
import sun.net.www.ParseUtil;
|
import sun.net.www.ParseUtil;
|
||||||
/* import org.ietf.jgss.*; */
|
/* import org.ietf.jgss.*; */
|
||||||
|
|
||||||
@ -41,7 +40,7 @@ import sun.net.www.ParseUtil;
|
|||||||
|
|
||||||
class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
||||||
private String server = null;
|
private String server = null;
|
||||||
private int port = DEFAULT_PORT;
|
private int serverPort = DEFAULT_PORT;
|
||||||
private InetSocketAddress external_address;
|
private InetSocketAddress external_address;
|
||||||
private boolean useV4 = false;
|
private boolean useV4 = false;
|
||||||
private Socket cmdsock = null;
|
private Socket cmdsock = null;
|
||||||
@ -54,7 +53,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
|
|
||||||
SocksSocketImpl(String server, int port) {
|
SocksSocketImpl(String server, int port) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.port = (port == -1 ? DEFAULT_PORT : port);
|
this.serverPort = (port == -1 ? DEFAULT_PORT : port);
|
||||||
}
|
}
|
||||||
|
|
||||||
SocksSocketImpl(Proxy proxy) {
|
SocksSocketImpl(Proxy proxy) {
|
||||||
@ -63,7 +62,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
InetSocketAddress ad = (InetSocketAddress) a;
|
InetSocketAddress ad = (InetSocketAddress) a;
|
||||||
// Use getHostString() to avoid reverse lookups
|
// Use getHostString() to avoid reverse lookups
|
||||||
server = ad.getHostString();
|
server = ad.getHostString();
|
||||||
port = ad.getPort();
|
serverPort = ad.getPort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,16 +112,12 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
*/
|
*/
|
||||||
private boolean authenticate(byte method, InputStream in,
|
private boolean authenticate(byte method, InputStream in,
|
||||||
BufferedOutputStream out) throws IOException {
|
BufferedOutputStream out) throws IOException {
|
||||||
byte[] data = null;
|
|
||||||
int i;
|
|
||||||
// No Authentication required. We're done then!
|
// No Authentication required. We're done then!
|
||||||
if (method == NO_AUTH)
|
if (method == NO_AUTH)
|
||||||
return true;
|
return true;
|
||||||
/**
|
/**
|
||||||
* User/Password authentication. Try, in that order :
|
* User/Password authentication. Try, in that order :
|
||||||
* - The application provided Authenticator, if any
|
* - The application provided Authenticator, if any
|
||||||
* - The user preferences java.net.socks.username &
|
|
||||||
* java.net.socks.password
|
|
||||||
* - the user.name & no password (backward compatibility behavior).
|
* - the user.name & no password (backward compatibility behavior).
|
||||||
*/
|
*/
|
||||||
if (method == USER_PASSW) {
|
if (method == USER_PASSW) {
|
||||||
@ -134,40 +129,15 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
new java.security.PrivilegedAction<PasswordAuthentication>() {
|
new java.security.PrivilegedAction<PasswordAuthentication>() {
|
||||||
public PasswordAuthentication run() {
|
public PasswordAuthentication run() {
|
||||||
return Authenticator.requestPasswordAuthentication(
|
return Authenticator.requestPasswordAuthentication(
|
||||||
server, addr, port, "SOCKS5", "SOCKS authentication", null);
|
server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (pw != null) {
|
if (pw != null) {
|
||||||
userName = pw.getUserName();
|
userName = pw.getUserName();
|
||||||
password = new String(pw.getPassword());
|
password = new String(pw.getPassword());
|
||||||
} else {
|
} else {
|
||||||
final Preferences prefs = Preferences.userRoot().node("/java/net/socks");
|
userName = java.security.AccessController.doPrivileged(
|
||||||
try {
|
|
||||||
userName = AccessController.doPrivileged(
|
|
||||||
new java.security.PrivilegedExceptionAction<String>() {
|
|
||||||
public String run() throws IOException {
|
|
||||||
return prefs.get("username", null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (java.security.PrivilegedActionException pae) {
|
|
||||||
throw (IOException) pae.getException();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userName != null) {
|
|
||||||
try {
|
|
||||||
password = AccessController.doPrivileged(
|
|
||||||
new java.security.PrivilegedExceptionAction<String>() {
|
|
||||||
public String run() throws IOException {
|
|
||||||
return prefs.get("password", null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (java.security.PrivilegedActionException pae) {
|
|
||||||
throw (IOException) pae.getException();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
userName = java.security.AccessController.doPrivileged(
|
|
||||||
new sun.security.action.GetPropertyAction("user.name"));
|
new sun.security.action.GetPropertyAction("user.name"));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (userName == null)
|
if (userName == null)
|
||||||
return false;
|
return false;
|
||||||
@ -188,8 +158,8 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
} else
|
} else
|
||||||
out.write(0);
|
out.write(0);
|
||||||
out.flush();
|
out.flush();
|
||||||
data = new byte[2];
|
byte[] data = new byte[2];
|
||||||
i = readSocksReply(in, data);
|
int i = readSocksReply(in, data);
|
||||||
if (i != 2 || data[1] != 0) {
|
if (i != 2 || data[1] != 0) {
|
||||||
/* RFC 1929 specifies that the connection MUST be closed if
|
/* RFC 1929 specifies that the connection MUST be closed if
|
||||||
authentication fails */
|
authentication fails */
|
||||||
@ -322,6 +292,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
* @throws IllegalArgumentException if endpoint is null or a
|
* @throws IllegalArgumentException if endpoint is null or a
|
||||||
* SocketAddress subclass not supported by this socket
|
* SocketAddress subclass not supported by this socket
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void connect(SocketAddress endpoint, int timeout) throws IOException {
|
protected void connect(SocketAddress endpoint, int timeout) throws IOException {
|
||||||
SecurityManager security = System.getSecurityManager();
|
SecurityManager security = System.getSecurityManager();
|
||||||
if (endpoint == null || !(endpoint instanceof InetSocketAddress))
|
if (endpoint == null || !(endpoint instanceof InetSocketAddress))
|
||||||
@ -352,7 +323,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
super.connect(epoint, timeout);
|
super.connect(epoint, timeout);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
URI uri = null;
|
URI uri;
|
||||||
// Use getHostString() to avoid reverse lookups
|
// Use getHostString() to avoid reverse lookups
|
||||||
String host = epoint.getHostString();
|
String host = epoint.getHostString();
|
||||||
// IPv6 litteral?
|
// IPv6 litteral?
|
||||||
@ -365,6 +336,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
// This shouldn't happen
|
// This shouldn't happen
|
||||||
assert false : e;
|
assert false : e;
|
||||||
|
uri = null;
|
||||||
}
|
}
|
||||||
Proxy p = null;
|
Proxy p = null;
|
||||||
IOException savedExc = null;
|
IOException savedExc = null;
|
||||||
@ -386,18 +358,18 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
throw new SocketException("Unknow address type for proxy: " + p);
|
throw new SocketException("Unknow address type for proxy: " + p);
|
||||||
// Use getHostString() to avoid reverse lookups
|
// Use getHostString() to avoid reverse lookups
|
||||||
server = ((InetSocketAddress) p.address()).getHostString();
|
server = ((InetSocketAddress) p.address()).getHostString();
|
||||||
port = ((InetSocketAddress) p.address()).getPort();
|
serverPort = ((InetSocketAddress) p.address()).getPort();
|
||||||
|
|
||||||
// Connects to the SOCKS server
|
// Connects to the SOCKS server
|
||||||
try {
|
try {
|
||||||
privilegedConnect(server, port, timeout);
|
privilegedConnect(server, serverPort, timeout);
|
||||||
// Worked, let's get outta here
|
// Worked, let's get outta here
|
||||||
break;
|
break;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Ooops, let's notify the ProxySelector
|
// Ooops, let's notify the ProxySelector
|
||||||
sel.connectFailed(uri,p.address(),e);
|
sel.connectFailed(uri,p.address(),e);
|
||||||
server = null;
|
server = null;
|
||||||
port = -1;
|
serverPort = -1;
|
||||||
savedExc = e;
|
savedExc = e;
|
||||||
// Will continue the while loop and try the next proxy
|
// Will continue the while loop and try the next proxy
|
||||||
}
|
}
|
||||||
@ -414,7 +386,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
} else {
|
} else {
|
||||||
// Connects to the SOCKS server
|
// Connects to the SOCKS server
|
||||||
try {
|
try {
|
||||||
privilegedConnect(server, port, timeout);
|
privilegedConnect(server, serverPort, timeout);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new SocketException(e.getMessage());
|
throw new SocketException(e.getMessage());
|
||||||
}
|
}
|
||||||
@ -487,7 +459,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
if (i != 4)
|
if (i != 4)
|
||||||
throw new SocketException("Reply from SOCKS server has bad length");
|
throw new SocketException("Reply from SOCKS server has bad length");
|
||||||
SocketException ex = null;
|
SocketException ex = null;
|
||||||
int nport, len;
|
int len;
|
||||||
byte[] addr;
|
byte[] addr;
|
||||||
switch (data[1]) {
|
switch (data[1]) {
|
||||||
case REQUEST_OK:
|
case REQUEST_OK:
|
||||||
@ -502,8 +474,6 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
i = readSocksReply(in, data);
|
i = readSocksReply(in, data);
|
||||||
if (i != 2)
|
if (i != 2)
|
||||||
throw new SocketException("Reply from SOCKS server badly formatted");
|
throw new SocketException("Reply from SOCKS server badly formatted");
|
||||||
nport = ((int)data[0] & 0xff) << 8;
|
|
||||||
nport += ((int)data[1] & 0xff);
|
|
||||||
break;
|
break;
|
||||||
case DOMAIN_NAME:
|
case DOMAIN_NAME:
|
||||||
len = data[1];
|
len = data[1];
|
||||||
@ -515,8 +485,6 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
i = readSocksReply(in, data);
|
i = readSocksReply(in, data);
|
||||||
if (i != 2)
|
if (i != 2)
|
||||||
throw new SocketException("Reply from SOCKS server badly formatted");
|
throw new SocketException("Reply from SOCKS server badly formatted");
|
||||||
nport = ((int)data[0] & 0xff) << 8;
|
|
||||||
nport += ((int)data[1] & 0xff);
|
|
||||||
break;
|
break;
|
||||||
case IPV6:
|
case IPV6:
|
||||||
len = data[1];
|
len = data[1];
|
||||||
@ -528,8 +496,6 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
i = readSocksReply(in, data);
|
i = readSocksReply(in, data);
|
||||||
if (i != 2)
|
if (i != 2)
|
||||||
throw new SocketException("Reply from SOCKS server badly formatted");
|
throw new SocketException("Reply from SOCKS server badly formatted");
|
||||||
nport = ((int)data[0] & 0xff) << 8;
|
|
||||||
nport += ((int)data[1] & 0xff);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ex = new SocketException("Reply from SOCKS server contains wrong code");
|
ex = new SocketException("Reply from SOCKS server contains wrong code");
|
||||||
@ -663,7 +629,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
*/
|
*/
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
URI uri = null;
|
URI uri;
|
||||||
// Use getHostString() to avoid reverse lookups
|
// Use getHostString() to avoid reverse lookups
|
||||||
String host = saddr.getHostString();
|
String host = saddr.getHostString();
|
||||||
// IPv6 litteral?
|
// IPv6 litteral?
|
||||||
@ -676,6 +642,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
// This shouldn't happen
|
// This shouldn't happen
|
||||||
assert false : e;
|
assert false : e;
|
||||||
|
uri = null;
|
||||||
}
|
}
|
||||||
Proxy p = null;
|
Proxy p = null;
|
||||||
Exception savedExc = null;
|
Exception savedExc = null;
|
||||||
@ -695,7 +662,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
throw new SocketException("Unknow address type for proxy: " + p);
|
throw new SocketException("Unknow address type for proxy: " + p);
|
||||||
// Use getHostString() to avoid reverse lookups
|
// Use getHostString() to avoid reverse lookups
|
||||||
server = ((InetSocketAddress) p.address()).getHostString();
|
server = ((InetSocketAddress) p.address()).getHostString();
|
||||||
port = ((InetSocketAddress) p.address()).getPort();
|
serverPort = ((InetSocketAddress) p.address()).getPort();
|
||||||
|
|
||||||
// Connects to the SOCKS server
|
// Connects to the SOCKS server
|
||||||
try {
|
try {
|
||||||
@ -703,7 +670,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
new PrivilegedExceptionAction<Void>() {
|
new PrivilegedExceptionAction<Void>() {
|
||||||
public Void run() throws Exception {
|
public Void run() throws Exception {
|
||||||
cmdsock = new Socket(new PlainSocketImpl());
|
cmdsock = new Socket(new PlainSocketImpl());
|
||||||
cmdsock.connect(new InetSocketAddress(server, port));
|
cmdsock.connect(new InetSocketAddress(server, serverPort));
|
||||||
cmdIn = cmdsock.getInputStream();
|
cmdIn = cmdsock.getInputStream();
|
||||||
cmdOut = cmdsock.getOutputStream();
|
cmdOut = cmdsock.getOutputStream();
|
||||||
return null;
|
return null;
|
||||||
@ -713,7 +680,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
// Ooops, let's notify the ProxySelector
|
// Ooops, let's notify the ProxySelector
|
||||||
sel.connectFailed(uri,p.address(),new SocketException(e.getMessage()));
|
sel.connectFailed(uri,p.address(),new SocketException(e.getMessage()));
|
||||||
server = null;
|
server = null;
|
||||||
port = -1;
|
serverPort = -1;
|
||||||
cmdsock = null;
|
cmdsock = null;
|
||||||
savedExc = e;
|
savedExc = e;
|
||||||
// Will continue the while loop and try the next proxy
|
// Will continue the while loop and try the next proxy
|
||||||
@ -734,7 +701,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
new PrivilegedExceptionAction<Void>() {
|
new PrivilegedExceptionAction<Void>() {
|
||||||
public Void run() throws Exception {
|
public Void run() throws Exception {
|
||||||
cmdsock = new Socket(new PlainSocketImpl());
|
cmdsock = new Socket(new PlainSocketImpl());
|
||||||
cmdsock.connect(new InetSocketAddress(server, port));
|
cmdsock.connect(new InetSocketAddress(server, serverPort));
|
||||||
cmdIn = cmdsock.getInputStream();
|
cmdIn = cmdsock.getInputStream();
|
||||||
cmdOut = cmdsock.getOutputStream();
|
cmdOut = cmdsock.getOutputStream();
|
||||||
return null;
|
return null;
|
||||||
@ -809,7 +776,6 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
switch (data[1]) {
|
switch (data[1]) {
|
||||||
case REQUEST_OK:
|
case REQUEST_OK:
|
||||||
// success!
|
// success!
|
||||||
InetSocketAddress real_end = null;
|
|
||||||
switch(data[3]) {
|
switch(data[3]) {
|
||||||
case IPV4:
|
case IPV4:
|
||||||
addr = new byte[4];
|
addr = new byte[4];
|
||||||
@ -1016,6 +982,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
* @return the value of this socket's <code>address</code> field.
|
* @return the value of this socket's <code>address</code> field.
|
||||||
* @see java.net.SocketImpl#address
|
* @see java.net.SocketImpl#address
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected InetAddress getInetAddress() {
|
protected InetAddress getInetAddress() {
|
||||||
if (external_address != null)
|
if (external_address != null)
|
||||||
return external_address.getAddress();
|
return external_address.getAddress();
|
||||||
@ -1029,6 +996,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
* @return the value of this socket's <code>port</code> field.
|
* @return the value of this socket's <code>port</code> field.
|
||||||
* @see java.net.SocketImpl#port
|
* @see java.net.SocketImpl#port
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected int getPort() {
|
protected int getPort() {
|
||||||
if (external_address != null)
|
if (external_address != null)
|
||||||
return external_address.getPort();
|
return external_address.getPort();
|
||||||
@ -1036,6 +1004,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
return super.getPort();
|
return super.getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected int getLocalPort() {
|
protected int getLocalPort() {
|
||||||
if (socket != null)
|
if (socket != null)
|
||||||
return super.getLocalPort();
|
return super.getLocalPort();
|
||||||
@ -1045,6 +1014,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
|
|||||||
return super.getLocalPort();
|
return super.getLocalPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected void close() throws IOException {
|
protected void close() throws IOException {
|
||||||
if (cmdsock != null)
|
if (cmdsock != null)
|
||||||
cmdsock.close();
|
cmdsock.close();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user