< prev index next >

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

Print this page




  65         SocketAddress a = proxy.address();
  66         if (a instanceof InetSocketAddress) {
  67             InetSocketAddress ad = (InetSocketAddress) a;
  68             // Use getHostString() to avoid reverse lookups
  69             server = ad.getHostString();
  70             serverPort = ad.getPort();
  71         }
  72     }
  73 
  74     void setV4() {
  75         useV4 = true;
  76     }
  77 
  78     private synchronized void privilegedConnect(final String host,
  79                                               final int port,
  80                                               final int timeout)
  81          throws IOException
  82     {
  83         try {
  84             AccessController.doPrivileged(
  85                 new java.security.PrivilegedExceptionAction<Void>() {
  86                     public Void run() throws IOException {
  87                               superConnectServer(host, port, timeout);
  88                               cmdIn = getInputStream();
  89                               cmdOut = getOutputStream();
  90                               return null;
  91                           }
  92                       });
  93         } catch (java.security.PrivilegedActionException pae) {
  94             throw (IOException) pae.getException();
  95         }
  96     }
  97 
  98     private void superConnectServer(String host, int port,
  99                                     int timeout) throws IOException {
 100         super.connect(new InetSocketAddress(host, port), timeout);
 101     }
 102 
 103     private static int remainingMillis(long deadlineMillis) throws IOException {
 104         if (deadlineMillis == 0L)
 105             return 0;


 140         return authenticate(method, in, out, 0L);
 141     }
 142 
 143     private boolean authenticate(byte method, InputStream in,
 144                                  BufferedOutputStream out,
 145                                  long deadlineMillis) throws IOException {
 146         // No Authentication required. We're done then!
 147         if (method == NO_AUTH)
 148             return true;
 149         /**
 150          * User/Password authentication. Try, in that order :
 151          * - The application provided Authenticator, if any
 152          * - the user.name & no password (backward compatibility behavior).
 153          */
 154         if (method == USER_PASSW) {
 155             String userName;
 156             String password = null;
 157             final InetAddress addr = InetAddress.getByName(server);
 158             PasswordAuthentication pw =
 159                 java.security.AccessController.doPrivileged(
 160                     new java.security.PrivilegedAction<PasswordAuthentication>() {
 161                         public PasswordAuthentication run() {
 162                                 return Authenticator.requestPasswordAuthentication(
 163                                        server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
 164                             }
 165                         });
 166             if (pw != null) {
 167                 userName = pw.getUserName();
 168                 password = new String(pw.getPassword());
 169             } else {
 170                 userName = java.security.AccessController.doPrivileged(
 171                         new sun.security.action.GetPropertyAction("user.name"));
 172             }
 173             if (userName == null)
 174                 return false;
 175             out.write(1);
 176             out.write(userName.length());
 177             try {
 178                 out.write(userName.getBytes("ISO-8859-1"));
 179             } catch (java.io.UnsupportedEncodingException uee) {
 180                 assert false;


 334             deadlineMillis = finish < 0 ? Long.MAX_VALUE : finish;
 335         }
 336 
 337         SecurityManager security = System.getSecurityManager();
 338         if (endpoint == null || !(endpoint instanceof InetSocketAddress))
 339             throw new IllegalArgumentException("Unsupported address type");
 340         InetSocketAddress epoint = (InetSocketAddress) endpoint;
 341         if (security != null) {
 342             if (epoint.isUnresolved())
 343                 security.checkConnect(epoint.getHostName(),
 344                                       epoint.getPort());
 345             else
 346                 security.checkConnect(epoint.getAddress().getHostAddress(),
 347                                       epoint.getPort());
 348         }
 349         if (server == null) {
 350             // This is the general case
 351             // server is not null only when the socket was created with a
 352             // specified proxy in which case it does bypass the ProxySelector
 353             ProxySelector sel = java.security.AccessController.doPrivileged(
 354                 new java.security.PrivilegedAction<ProxySelector>() {
 355                     public ProxySelector run() {
 356                             return ProxySelector.getDefault();
 357                         }
 358                     });
 359             if (sel == null) {
 360                 /*
 361                  * No default proxySelector --> direct connection
 362                  */
 363                 super.connect(epoint, remainingMillis(deadlineMillis));
 364                 return;
 365             }
 366             URI uri;
 367             // Use getHostString() to avoid reverse lookups
 368             String host = epoint.getHostString();
 369             // IPv6 litteral?
 370             if (epoint.getAddress() instanceof Inet6Address &&
 371                 (!host.startsWith("[")) && (host.indexOf(':') >= 0)) {
 372                 host = "[" + host + "]";
 373             }
 374             try {


 578         if (ex != null) {
 579             in.close();
 580             out.close();
 581             throw ex;
 582         }
 583         external_address = epoint;
 584     }
 585 
 586     private void bindV4(InputStream in, OutputStream out,
 587                         InetAddress baddr,
 588                         int lport) throws IOException {
 589         if (!(baddr instanceof Inet4Address)) {
 590             throw new SocketException("SOCKS V4 requires IPv4 only addresses");
 591         }
 592         super.bind(baddr, lport);
 593         byte[] addr1 = baddr.getAddress();
 594         /* Test for AnyLocal */
 595         InetAddress naddr = baddr;
 596         if (naddr.isAnyLocalAddress()) {
 597             naddr = AccessController.doPrivileged(
 598                         new PrivilegedAction<InetAddress>() {
 599                             public InetAddress run() {
 600                                 return cmdsock.getLocalAddress();
 601 
 602                             }
 603                         });
 604             addr1 = naddr.getAddress();
 605         }
 606         out.write(PROTO_VERS4);
 607         out.write(BIND);
 608         out.write((super.getLocalPort() >> 8) & 0xff);
 609         out.write((super.getLocalPort() >> 0) & 0xff);
 610         out.write(addr1);
 611         String userName = getUserName();
 612         try {
 613             out.write(userName.getBytes("ISO-8859-1"));
 614         } catch (java.io.UnsupportedEncodingException uee) {
 615             assert false;
 616         }
 617         out.write(0);
 618         out.flush();


 654      * means "accept incoming connection from", so the SocketAddress is the
 655      * the one of the host we do accept connection from.
 656      *
 657      * @param      saddr   the Socket address of the remote host.
 658      * @exception  IOException  if an I/O error occurs when binding this socket.
 659      */
 660     protected synchronized void socksBind(InetSocketAddress saddr) throws IOException {
 661         if (socket != null) {
 662             // this is a client socket, not a server socket, don't
 663             // call the SOCKS proxy for a bind!
 664             return;
 665         }
 666 
 667         // Connects to the SOCKS server
 668 
 669         if (server == null) {
 670             // This is the general case
 671             // server is not null only when the socket was created with a
 672             // specified proxy in which case it does bypass the ProxySelector
 673             ProxySelector sel = java.security.AccessController.doPrivileged(
 674                 new java.security.PrivilegedAction<ProxySelector>() {
 675                     public ProxySelector run() {
 676                             return ProxySelector.getDefault();
 677                         }
 678                     });
 679             if (sel == null) {
 680                 /*
 681                  * No default proxySelector --> direct connection
 682                  */
 683                 return;
 684             }
 685             URI uri;
 686             // Use getHostString() to avoid reverse lookups
 687             String host = saddr.getHostString();
 688             // IPv6 litteral?
 689             if (saddr.getAddress() instanceof Inet6Address &&
 690                 (!host.startsWith("[")) && (host.indexOf(':') >= 0)) {
 691                 host = "[" + host + "]";
 692             }
 693             try {
 694                 uri = new URI("serversocket://" + ParseUtil.encodePath(host) + ":"+ saddr.getPort());


 707             while (iProxy.hasNext()) {
 708                 p = iProxy.next();
 709                 if (p == null || p.type() != Proxy.Type.SOCKS) {
 710                     return;
 711                 }
 712 
 713                 if (!(p.address() instanceof InetSocketAddress))
 714                     throw new SocketException("Unknown address type for proxy: " + p);
 715                 // Use getHostString() to avoid reverse lookups
 716                 server = ((InetSocketAddress) p.address()).getHostString();
 717                 serverPort = ((InetSocketAddress) p.address()).getPort();
 718                 if (p instanceof SocksProxy) {
 719                     if (((SocksProxy)p).protocolVersion() == 4) {
 720                         useV4 = true;
 721                     }
 722                 }
 723 
 724                 // Connects to the SOCKS server
 725                 try {
 726                     AccessController.doPrivileged(
 727                         new PrivilegedExceptionAction<Void>() {
 728                             public Void run() throws Exception {
 729                                 cmdsock = new Socket(new PlainSocketImpl());
 730                                 cmdsock.connect(new InetSocketAddress(server, serverPort));
 731                                 cmdIn = cmdsock.getInputStream();
 732                                 cmdOut = cmdsock.getOutputStream();
 733                                 return null;
 734                             }
 735                         });
 736                 } catch (Exception e) {
 737                     // Ooops, let's notify the ProxySelector
 738                     sel.connectFailed(uri,p.address(),new SocketException(e.getMessage()));
 739                     server = null;
 740                     serverPort = -1;
 741                     cmdsock = null;
 742                     savedExc = e;
 743                     // Will continue the while loop and try the next proxy
 744                 }
 745             }
 746 
 747             /*
 748              * If server is still null at this point, none of the proxy
 749              * worked
 750              */
 751             if (server == null || cmdsock == null) {
 752                 throw new SocketException("Can't connect to SOCKS proxy:"
 753                                           + savedExc.getMessage());
 754             }
 755         } else {
 756             try {
 757                 AccessController.doPrivileged(
 758                     new PrivilegedExceptionAction<Void>() {
 759                         public Void run() throws Exception {
 760                             cmdsock = new Socket(new PlainSocketImpl());
 761                             cmdsock.connect(new InetSocketAddress(server, serverPort));
 762                             cmdIn = cmdsock.getInputStream();
 763                             cmdOut = cmdsock.getOutputStream();
 764                             return null;
 765                         }
 766                     });
 767             } catch (Exception e) {
 768                 throw new SocketException(e.getMessage());
 769             }
 770         }
 771         BufferedOutputStream out = new BufferedOutputStream(cmdOut, 512);
 772         InputStream in = cmdIn;
 773         if (useV4) {
 774             bindV4(in, out, saddr.getAddress(), saddr.getPort());
 775             return;
 776         }
 777         out.write(PROTO_VERS);
 778         out.write(2);




  65         SocketAddress a = proxy.address();
  66         if (a instanceof InetSocketAddress) {
  67             InetSocketAddress ad = (InetSocketAddress) a;
  68             // Use getHostString() to avoid reverse lookups
  69             server = ad.getHostString();
  70             serverPort = ad.getPort();
  71         }
  72     }
  73 
  74     void setV4() {
  75         useV4 = true;
  76     }
  77 
  78     private synchronized void privilegedConnect(final String host,
  79                                               final int port,
  80                                               final int timeout)
  81          throws IOException
  82     {
  83         try {
  84             AccessController.doPrivileged(
  85                 new java.security.PrivilegedExceptionAction<>() {
  86                     public Void run() throws IOException {
  87                               superConnectServer(host, port, timeout);
  88                               cmdIn = getInputStream();
  89                               cmdOut = getOutputStream();
  90                               return null;
  91                           }
  92                       });
  93         } catch (java.security.PrivilegedActionException pae) {
  94             throw (IOException) pae.getException();
  95         }
  96     }
  97 
  98     private void superConnectServer(String host, int port,
  99                                     int timeout) throws IOException {
 100         super.connect(new InetSocketAddress(host, port), timeout);
 101     }
 102 
 103     private static int remainingMillis(long deadlineMillis) throws IOException {
 104         if (deadlineMillis == 0L)
 105             return 0;


 140         return authenticate(method, in, out, 0L);
 141     }
 142 
 143     private boolean authenticate(byte method, InputStream in,
 144                                  BufferedOutputStream out,
 145                                  long deadlineMillis) throws IOException {
 146         // No Authentication required. We're done then!
 147         if (method == NO_AUTH)
 148             return true;
 149         /**
 150          * User/Password authentication. Try, in that order :
 151          * - The application provided Authenticator, if any
 152          * - the user.name & no password (backward compatibility behavior).
 153          */
 154         if (method == USER_PASSW) {
 155             String userName;
 156             String password = null;
 157             final InetAddress addr = InetAddress.getByName(server);
 158             PasswordAuthentication pw =
 159                 java.security.AccessController.doPrivileged(
 160                     new java.security.PrivilegedAction<>() {
 161                         public PasswordAuthentication run() {
 162                                 return Authenticator.requestPasswordAuthentication(
 163                                        server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
 164                             }
 165                         });
 166             if (pw != null) {
 167                 userName = pw.getUserName();
 168                 password = new String(pw.getPassword());
 169             } else {
 170                 userName = java.security.AccessController.doPrivileged(
 171                         new sun.security.action.GetPropertyAction("user.name"));
 172             }
 173             if (userName == null)
 174                 return false;
 175             out.write(1);
 176             out.write(userName.length());
 177             try {
 178                 out.write(userName.getBytes("ISO-8859-1"));
 179             } catch (java.io.UnsupportedEncodingException uee) {
 180                 assert false;


 334             deadlineMillis = finish < 0 ? Long.MAX_VALUE : finish;
 335         }
 336 
 337         SecurityManager security = System.getSecurityManager();
 338         if (endpoint == null || !(endpoint instanceof InetSocketAddress))
 339             throw new IllegalArgumentException("Unsupported address type");
 340         InetSocketAddress epoint = (InetSocketAddress) endpoint;
 341         if (security != null) {
 342             if (epoint.isUnresolved())
 343                 security.checkConnect(epoint.getHostName(),
 344                                       epoint.getPort());
 345             else
 346                 security.checkConnect(epoint.getAddress().getHostAddress(),
 347                                       epoint.getPort());
 348         }
 349         if (server == null) {
 350             // This is the general case
 351             // server is not null only when the socket was created with a
 352             // specified proxy in which case it does bypass the ProxySelector
 353             ProxySelector sel = java.security.AccessController.doPrivileged(
 354                 new java.security.PrivilegedAction<>() {
 355                     public ProxySelector run() {
 356                             return ProxySelector.getDefault();
 357                         }
 358                     });
 359             if (sel == null) {
 360                 /*
 361                  * No default proxySelector --> direct connection
 362                  */
 363                 super.connect(epoint, remainingMillis(deadlineMillis));
 364                 return;
 365             }
 366             URI uri;
 367             // Use getHostString() to avoid reverse lookups
 368             String host = epoint.getHostString();
 369             // IPv6 litteral?
 370             if (epoint.getAddress() instanceof Inet6Address &&
 371                 (!host.startsWith("[")) && (host.indexOf(':') >= 0)) {
 372                 host = "[" + host + "]";
 373             }
 374             try {


 578         if (ex != null) {
 579             in.close();
 580             out.close();
 581             throw ex;
 582         }
 583         external_address = epoint;
 584     }
 585 
 586     private void bindV4(InputStream in, OutputStream out,
 587                         InetAddress baddr,
 588                         int lport) throws IOException {
 589         if (!(baddr instanceof Inet4Address)) {
 590             throw new SocketException("SOCKS V4 requires IPv4 only addresses");
 591         }
 592         super.bind(baddr, lport);
 593         byte[] addr1 = baddr.getAddress();
 594         /* Test for AnyLocal */
 595         InetAddress naddr = baddr;
 596         if (naddr.isAnyLocalAddress()) {
 597             naddr = AccessController.doPrivileged(
 598                         new PrivilegedAction<>() {
 599                             public InetAddress run() {
 600                                 return cmdsock.getLocalAddress();
 601 
 602                             }
 603                         });
 604             addr1 = naddr.getAddress();
 605         }
 606         out.write(PROTO_VERS4);
 607         out.write(BIND);
 608         out.write((super.getLocalPort() >> 8) & 0xff);
 609         out.write((super.getLocalPort() >> 0) & 0xff);
 610         out.write(addr1);
 611         String userName = getUserName();
 612         try {
 613             out.write(userName.getBytes("ISO-8859-1"));
 614         } catch (java.io.UnsupportedEncodingException uee) {
 615             assert false;
 616         }
 617         out.write(0);
 618         out.flush();


 654      * means "accept incoming connection from", so the SocketAddress is the
 655      * the one of the host we do accept connection from.
 656      *
 657      * @param      saddr   the Socket address of the remote host.
 658      * @exception  IOException  if an I/O error occurs when binding this socket.
 659      */
 660     protected synchronized void socksBind(InetSocketAddress saddr) throws IOException {
 661         if (socket != null) {
 662             // this is a client socket, not a server socket, don't
 663             // call the SOCKS proxy for a bind!
 664             return;
 665         }
 666 
 667         // Connects to the SOCKS server
 668 
 669         if (server == null) {
 670             // This is the general case
 671             // server is not null only when the socket was created with a
 672             // specified proxy in which case it does bypass the ProxySelector
 673             ProxySelector sel = java.security.AccessController.doPrivileged(
 674                 new java.security.PrivilegedAction<>() {
 675                     public ProxySelector run() {
 676                             return ProxySelector.getDefault();
 677                         }
 678                     });
 679             if (sel == null) {
 680                 /*
 681                  * No default proxySelector --> direct connection
 682                  */
 683                 return;
 684             }
 685             URI uri;
 686             // Use getHostString() to avoid reverse lookups
 687             String host = saddr.getHostString();
 688             // IPv6 litteral?
 689             if (saddr.getAddress() instanceof Inet6Address &&
 690                 (!host.startsWith("[")) && (host.indexOf(':') >= 0)) {
 691                 host = "[" + host + "]";
 692             }
 693             try {
 694                 uri = new URI("serversocket://" + ParseUtil.encodePath(host) + ":"+ saddr.getPort());


 707             while (iProxy.hasNext()) {
 708                 p = iProxy.next();
 709                 if (p == null || p.type() != Proxy.Type.SOCKS) {
 710                     return;
 711                 }
 712 
 713                 if (!(p.address() instanceof InetSocketAddress))
 714                     throw new SocketException("Unknown address type for proxy: " + p);
 715                 // Use getHostString() to avoid reverse lookups
 716                 server = ((InetSocketAddress) p.address()).getHostString();
 717                 serverPort = ((InetSocketAddress) p.address()).getPort();
 718                 if (p instanceof SocksProxy) {
 719                     if (((SocksProxy)p).protocolVersion() == 4) {
 720                         useV4 = true;
 721                     }
 722                 }
 723 
 724                 // Connects to the SOCKS server
 725                 try {
 726                     AccessController.doPrivileged(
 727                         new PrivilegedExceptionAction<>() {
 728                             public Void run() throws Exception {
 729                                 cmdsock = new Socket(new PlainSocketImpl());
 730                                 cmdsock.connect(new InetSocketAddress(server, serverPort));
 731                                 cmdIn = cmdsock.getInputStream();
 732                                 cmdOut = cmdsock.getOutputStream();
 733                                 return null;
 734                             }
 735                         });
 736                 } catch (Exception e) {
 737                     // Ooops, let's notify the ProxySelector
 738                     sel.connectFailed(uri,p.address(),new SocketException(e.getMessage()));
 739                     server = null;
 740                     serverPort = -1;
 741                     cmdsock = null;
 742                     savedExc = e;
 743                     // Will continue the while loop and try the next proxy
 744                 }
 745             }
 746 
 747             /*
 748              * If server is still null at this point, none of the proxy
 749              * worked
 750              */
 751             if (server == null || cmdsock == null) {
 752                 throw new SocketException("Can't connect to SOCKS proxy:"
 753                                           + savedExc.getMessage());
 754             }
 755         } else {
 756             try {
 757                 AccessController.doPrivileged(
 758                     new PrivilegedExceptionAction<>() {
 759                         public Void run() throws Exception {
 760                             cmdsock = new Socket(new PlainSocketImpl());
 761                             cmdsock.connect(new InetSocketAddress(server, serverPort));
 762                             cmdIn = cmdsock.getInputStream();
 763                             cmdOut = cmdsock.getOutputStream();
 764                             return null;
 765                         }
 766                     });
 767             } catch (Exception e) {
 768                 throw new SocketException(e.getMessage());
 769             }
 770         }
 771         BufferedOutputStream out = new BufferedOutputStream(cmdOut, 512);
 772         InputStream in = cmdIn;
 773         if (useV4) {
 774             bindV4(in, out, saddr.getAddress(), saddr.getPort());
 775             return;
 776         }
 777         out.write(PROTO_VERS);
 778         out.write(2);


< prev index next >