52
52
import java .security .cert .*;
53
53
import javax .net .*;
54
54
import javax .net .ssl .*;
55
- import javax .security .auth .x500 .X500Principal ;
56
55
57
56
/**
58
57
* This class is used to get Sockets. Depending on the arguments passed
@@ -275,8 +274,10 @@ private static Socket createSocket(InetAddress localaddr, int localport,
275
274
", host " + host + ", port " + port +
276
275
", connection timeout " + cto + ", timeout " + to +
277
276
", socket factory " + sf + ", useSSL " + useSSL );
278
-
277
+
279
278
String proxyHost = props .getProperty (prefix + ".proxy.host" , null );
279
+ String proxyUser = props .getProperty (prefix + ".proxy.user" , null );
280
+ String proxyPassword = props .getProperty (prefix + ".proxy.password" , null );
280
281
int proxyPort = 80 ;
281
282
String socksHost = null ;
282
283
int socksPort = 1080 ;
@@ -295,8 +296,12 @@ private static Socket createSocket(InetAddress localaddr, int localport,
295
296
proxyPort = PropUtil .getIntProperty (props ,
296
297
prefix + ".proxy.port" , proxyPort );
297
298
err = "Using web proxy host, port: " + proxyHost + ", " + proxyPort ;
298
- if (logger .isLoggable (Level .FINER ))
299
+ if (logger .isLoggable (Level .FINER )) {
299
300
logger .finer ("web proxy host " + proxyHost + ", port " + proxyPort );
301
+ if (proxyUser != null )
302
+ logger .finer ("web proxy user " + proxyUser + ", password " +
303
+ (proxyPassword == null ? "<null>" : "<non-null>" ));
304
+ }
300
305
} else if ((socksHost =
301
306
props .getProperty (prefix + ".socks.host" , null )) != null ) {
302
307
int i = socksHost .indexOf (':' );
@@ -346,7 +351,8 @@ private static Socket createSocket(InetAddress localaddr, int localport,
346
351
try {
347
352
logger .finest ("connecting..." );
348
353
if (proxyHost != null )
349
- proxyConnect (socket , proxyHost , proxyPort , host , port , cto );
354
+ proxyConnect (socket , proxyHost , proxyPort ,
355
+ proxyUser , proxyPassword , host , port , cto );
350
356
else if (cto >= 0 )
351
357
socket .connect (new InetSocketAddress (host , port ), cto );
352
358
else
@@ -407,7 +413,7 @@ private static SocketFactory getSocketFactory(String sfClass)
407
413
if (sfClass == null || sfClass .length () == 0 )
408
414
return null ;
409
415
410
- // dynamically load the class
416
+ // dynamically load the class
411
417
412
418
ClassLoader cl = getContextClassLoader ();
413
419
Class <?> clsSockFact = null ;
@@ -419,7 +425,7 @@ private static SocketFactory getSocketFactory(String sfClass)
419
425
if (clsSockFact == null )
420
426
clsSockFact = Class .forName (sfClass );
421
427
// get & invoke the getDefault() method
422
- Method mthGetDefault = clsSockFact .getMethod ("getDefault" ,
428
+ Method mthGetDefault = clsSockFact .getMethod ("getDefault" ,
423
429
new Class <?>[]{});
424
430
SocketFactory sf = (SocketFactory )
425
431
mthGetDefault .invoke (new Object (), new Object []{});
@@ -662,7 +668,7 @@ private static boolean isRecoverable(Throwable t) {
662
668
/**
663
669
* Check the server from the Socket connection against the server name(s)
664
670
* as expressed in the server certificate (RFC 2595 check).
665
- *
671
+ *
666
672
* @param server name of the server expected
667
673
* @param sslSocket SSLSocket connected to the server
668
674
* @exception IOException if we can't verify identity of server
@@ -693,7 +699,7 @@ private static void checkServerIdentity(String server, SSLSocket sslSocket)
693
699
694
700
/**
695
701
* Do any of the names in the cert match the server name?
696
- *
702
+ *
697
703
* @param server name of the server expected
698
704
* @param cert X509Certificate to get the subject's name from
699
705
* @return true if it matches
@@ -714,7 +720,7 @@ private static boolean matchCert(String server, X509Certificate cert) {
714
720
// invoke HostnameChecker.getInstance(HostnameChecker.TYPE_LDAP)
715
721
// HostnameChecker.TYPE_LDAP == 2
716
722
// LDAP requires the same regex handling as we need
717
- Method getInstance = hnc .getMethod ("getInstance" ,
723
+ Method getInstance = hnc .getMethod ("getInstance" ,
718
724
new Class <?>[] { byte .class });
719
725
Object hostnameChecker = getInstance .invoke (new Object (),
720
726
new Object [] { Byte .valueOf ((byte )2 ) });
@@ -820,6 +826,7 @@ private static boolean matchServer(String server, String name) {
820
826
*/
821
827
private static void proxyConnect (Socket socket ,
822
828
String proxyHost , int proxyPort ,
829
+ String proxyUser , String proxyPassword ,
823
830
String host , int port , int cto )
824
831
throws IOException {
825
832
if (logger .isLoggable (Level .FINE ))
@@ -832,8 +839,22 @@ private static void proxyConnect(Socket socket,
832
839
socket .connect (new InetSocketAddress (proxyHost , proxyPort ));
833
840
PrintStream os = new PrintStream (socket .getOutputStream (), false ,
834
841
StandardCharsets .UTF_8 .name ());
835
- os .print ("CONNECT " + host + ":" + port + " HTTP/1.1\r \n " );
836
- os .print ("Host: " + host + ":" + port + "\r \n \r \n " );
842
+ StringBuilder requestBuilder = new StringBuilder ();
843
+ requestBuilder .append ("CONNECT " ).append (host ).append (":" ).append (port ).
844
+ append (" HTTP/1.1\r \n " );
845
+ requestBuilder .append ("Host: " ).append (host ).append (":" ).append (port ).
846
+ append ("\r \n " );
847
+ if (proxyUser != null && proxyPassword != null ) {
848
+ byte [] upbytes = (proxyUser + ':' + proxyPassword ).
849
+ getBytes (StandardCharsets .UTF_8 );
850
+ String proxyHeaderValue = new String (
851
+ BASE64EncoderStream .encode (upbytes ),
852
+ StandardCharsets .US_ASCII );
853
+ requestBuilder .append ("Proxy-Authorization: Basic " ).
854
+ append (proxyHeaderValue ).append ("\r \n " );
855
+ }
856
+ requestBuilder .append ("Proxy-Connection: keep-alive\r \n \r \n " );
857
+ os .print (requestBuilder .toString ());
837
858
os .flush ();
838
859
BufferedReader r = new BufferedReader (new InputStreamReader (
839
860
socket .getInputStream (), StandardCharsets .UTF_8 ));
0 commit comments