< prev index next >

src/java.base/share/classes/java/net/SocksSocketImpl.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.net;
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.OutputStream;
  29 import java.io.BufferedOutputStream;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.security.PrivilegedExceptionAction;
  33 import sun.net.SocksProxy;
  34 import sun.net.spi.DefaultProxySelector;
  35 import sun.net.www.ParseUtil;

  36 /* import org.ietf.jgss.*; */
  37 
  38 /**
  39  * SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
  40  * This is a subclass of PlainSocketImpl.
  41  * Note this class should <b>NOT</b> be public.
  42  */
  43 
  44 class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
  45     private String server = null;
  46     private int serverPort = DEFAULT_PORT;
  47     private InetSocketAddress external_address;
  48     private boolean useV4 = false;
  49     private Socket cmdsock = null;
  50     private InputStream cmdIn = null;
  51     private OutputStream cmdOut = null;
  52     /* true if the Proxy has been set programatically */
  53     private boolean applicationSetProxy;  /* false */
  54 
  55 


 160          * User/Password authentication. Try, in that order :
 161          * - The application provided Authenticator, if any
 162          * - the user.name & no password (backward compatibility behavior).
 163          */
 164         if (method == USER_PASSW) {
 165             String userName;
 166             String password = null;
 167             final InetAddress addr = InetAddress.getByName(server);
 168             PasswordAuthentication pw =
 169                 java.security.AccessController.doPrivileged(
 170                     new java.security.PrivilegedAction<>() {
 171                         public PasswordAuthentication run() {
 172                                 return Authenticator.requestPasswordAuthentication(
 173                                        server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
 174                             }
 175                         });
 176             if (pw != null) {
 177                 userName = pw.getUserName();
 178                 password = new String(pw.getPassword());
 179             } else {
 180                 userName = java.security.AccessController.doPrivileged(
 181                         new sun.security.action.GetPropertyAction("user.name"));
 182             }
 183             if (userName == null)
 184                 return false;
 185             out.write(1);
 186             out.write(userName.length());
 187             try {
 188                 out.write(userName.getBytes("ISO-8859-1"));
 189             } catch (java.io.UnsupportedEncodingException uee) {
 190                 assert false;
 191             }
 192             if (password != null) {
 193                 out.write(password.length());
 194                 try {
 195                     out.write(password.getBytes("ISO-8859-1"));
 196                 } catch (java.io.UnsupportedEncodingException uee) {
 197                     assert false;
 198                 }
 199             } else
 200                 out.write(0);
 201             out.flush();


1071             return external_address.getPort();
1072         else
1073             return super.getLocalPort();
1074     }
1075 
1076     @Override
1077     protected void close() throws IOException {
1078         if (cmdsock != null)
1079             cmdsock.close();
1080         cmdsock = null;
1081         super.close();
1082     }
1083 
1084     private String getUserName() {
1085         String userName = "";
1086         if (applicationSetProxy) {
1087             try {
1088                 userName = System.getProperty("user.name");
1089             } catch (SecurityException se) { /* swallow Exception */ }
1090         } else {
1091             userName = java.security.AccessController.doPrivileged(
1092                 new sun.security.action.GetPropertyAction("user.name"));
1093         }
1094         return userName;
1095     }
1096 }


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.net;
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.OutputStream;
  29 import java.io.BufferedOutputStream;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.security.PrivilegedExceptionAction;
  33 import sun.net.SocksProxy;
  34 import sun.net.spi.DefaultProxySelector;
  35 import sun.net.www.ParseUtil;
  36 import sun.security.action.GetPropertyAction;
  37 /* import org.ietf.jgss.*; */
  38 
  39 /**
  40  * SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
  41  * This is a subclass of PlainSocketImpl.
  42  * Note this class should <b>NOT</b> be public.
  43  */
  44 
  45 class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
  46     private String server = null;
  47     private int serverPort = DEFAULT_PORT;
  48     private InetSocketAddress external_address;
  49     private boolean useV4 = false;
  50     private Socket cmdsock = null;
  51     private InputStream cmdIn = null;
  52     private OutputStream cmdOut = null;
  53     /* true if the Proxy has been set programatically */
  54     private boolean applicationSetProxy;  /* false */
  55 
  56 


 161          * User/Password authentication. Try, in that order :
 162          * - The application provided Authenticator, if any
 163          * - the user.name & no password (backward compatibility behavior).
 164          */
 165         if (method == USER_PASSW) {
 166             String userName;
 167             String password = null;
 168             final InetAddress addr = InetAddress.getByName(server);
 169             PasswordAuthentication pw =
 170                 java.security.AccessController.doPrivileged(
 171                     new java.security.PrivilegedAction<>() {
 172                         public PasswordAuthentication run() {
 173                                 return Authenticator.requestPasswordAuthentication(
 174                                        server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
 175                             }
 176                         });
 177             if (pw != null) {
 178                 userName = pw.getUserName();
 179                 password = new String(pw.getPassword());
 180             } else {
 181                 userName = GetPropertyAction.getProperty("user.name");

 182             }
 183             if (userName == null)
 184                 return false;
 185             out.write(1);
 186             out.write(userName.length());
 187             try {
 188                 out.write(userName.getBytes("ISO-8859-1"));
 189             } catch (java.io.UnsupportedEncodingException uee) {
 190                 assert false;
 191             }
 192             if (password != null) {
 193                 out.write(password.length());
 194                 try {
 195                     out.write(password.getBytes("ISO-8859-1"));
 196                 } catch (java.io.UnsupportedEncodingException uee) {
 197                     assert false;
 198                 }
 199             } else
 200                 out.write(0);
 201             out.flush();


1071             return external_address.getPort();
1072         else
1073             return super.getLocalPort();
1074     }
1075 
1076     @Override
1077     protected void close() throws IOException {
1078         if (cmdsock != null)
1079             cmdsock.close();
1080         cmdsock = null;
1081         super.close();
1082     }
1083 
1084     private String getUserName() {
1085         String userName = "";
1086         if (applicationSetProxy) {
1087             try {
1088                 userName = System.getProperty("user.name");
1089             } catch (SecurityException se) { /* swallow Exception */ }
1090         } else {
1091             userName = GetPropertyAction.getProperty("user.name");

1092         }
1093         return userName;
1094     }
1095 }
< prev index next >