22
33import sockslib .client .SSLSocks5 ;
44import sockslib .client .Socks5 ;
5+ import sockslib .client .SocksProxy ;
56import sockslib .client .SocksSocket ;
67import sockslib .common .KeyStoreInfo ;
78import sockslib .common .SSLConfiguration ;
9+ import sockslib .common .UsernamePasswordCredentials ;
810
911import java .io .BufferedReader ;
1012import java .io .IOException ;
1618public class DoIt {
1719 public static void main (String [] args )
1820 throws IOException {
19- // Socket socket = test1(new Socks5(new InetSocketAddress("localhost", 1030)));
20- Socket socket = test1 (new SSLSocks5 (new InetSocketAddress ("localhost" , 1030 ),
21- new SSLConfiguration (null , new KeyStoreInfo ("client-trust-keystore.jks" , "123456" , "JKS" ))));
21+ Socket socketAnonymous = createSocketPlainAnonymous ();
22+ Socket socketAuth = createSocketAuthenticated ();
23+ Socket socketSSLAnonymous = createSocketSSL ();
24+ Socket socketSSLAuth = createSocketSSLAuthenticated ();
2225
23- InputStreamReader isr = new InputStreamReader (socket .getInputStream ());
26+ if (socketAnonymous != null ) {
27+ System .out .println ("socketAnonymous: " + socketAnonymous );
28+ testSocket (socketAnonymous );
29+ }
30+ if (socketAuth != null ) {
31+ System .out .println ("socketAuth: " + socketAuth );
32+ testSocket (socketAuth );
33+ }
34+ if (socketSSLAnonymous != null ) {
35+ System .out .println ("socketSSLAnonymous: " + socketSSLAnonymous );
36+ testSocket (socketSSLAnonymous );
37+ }
38+ if (socketSSLAuth != null ) {
39+ System .out .println ("socketSSLAuth: " + socketSSLAuth );
40+ testSocket (socketSSLAuth );
41+ }
42+ }
43+
44+ private static Socket createSocketSSLAuthenticated () {
45+ try {
46+ SSLSocks5 socksProxySSLAuth = new SSLSocks5 (new InetSocketAddress ("localhost" , 1030 ),
47+ new SSLConfiguration (null , new KeyStoreInfo ("client-trust-keystore.jks" , "123456" , "JKS" )));
48+ socksProxySSLAuth .setCredentials (new UsernamePasswordCredentials ("PANCAKE" , "letmein" ));
49+ return new SocksSocket (socksProxySSLAuth , new InetSocketAddress ("whois.internic.net" , 43 ));
50+ } catch (Exception e ) {
51+ System .out .println ("socketSSLAuth failed" );
52+ System .out .println (e .getMessage ());
53+ }
54+ return null ;
55+ }
56+
57+ private static Socket createSocketSSL () {
58+ try {
59+ SSLSocks5 socksProxySSLAnonymous = new SSLSocks5 (new InetSocketAddress ("localhost" , 1030 ),
60+ new SSLConfiguration (null , new KeyStoreInfo ("client-trust-keystore.jks" , "123456" , "JKS" )));
61+ return new SocksSocket (socksProxySSLAnonymous , new InetSocketAddress ("whois.internic.net" , 43 ));
62+ } catch (Exception e ) {
63+ System .out .println ("socketSSLAnonymous failed" );
64+ System .out .println (e .getMessage ());
65+ }
66+ return null ;
67+ }
68+
69+ private static Socket createSocketAuthenticated () {
70+ try {
71+ SocksProxy proxyAuth = new Socks5 (new InetSocketAddress ("localhost" , 1080 ));
72+ proxyAuth .setCredentials (new UsernamePasswordCredentials ("PANCAKE" , "letmein" ));
73+ Socket socketAuth = new SocksSocket (
74+ proxyAuth ); // refactor to: new SocksSocket(proxy1, new InetSocketAddress("whois.internic.net", 43))
75+ socketAuth .connect (new InetSocketAddress ("whois.internic.net" , 43 )); // refactor out (see line above)
76+ return socketAuth ;
77+ } catch (Exception e ) {
78+ System .out .println ("socketAuth failed" );
79+ System .out .println (e .getMessage ());
80+ }
81+ return null ;
82+ }
83+
84+ private static Socket createSocketPlainAnonymous () {
85+ try {
86+ Socks5 socksProxyAnonymous = new Socks5 (new InetSocketAddress ("localhost" , 1030 ));
87+ return new SocksSocket (socksProxyAnonymous , new InetSocketAddress ("whois.internic.net" , 43 ));
88+ } catch (Exception e ) {
89+ System .out .println ("socketAnonymous failed" );
90+ System .out .println (e .getMessage ());
91+ }
92+ return null ;
93+ }
94+
95+ private static void testSocket (Socket socketToTest )
96+ throws IOException {
97+ InputStreamReader isr = new InputStreamReader (socketToTest .getInputStream ());
2498 BufferedReader in = new BufferedReader (isr );
2599
26- PrintWriter out = new PrintWriter (socket .getOutputStream (), true );
100+ PrintWriter out = new PrintWriter (socketToTest .getOutputStream (), true );
27101 out .println ("google.com" );
28102
29103 String line ;
@@ -32,8 +106,4 @@ public static void main(String[] args)
32106 }
33107 }
34108
35- private static SocksSocket test1 (Socks5 socks5 )
36- throws IOException {
37- return new SocksSocket (socks5 , new InetSocketAddress ("whois.internic.net" ,43 ));
38- }
39109}
0 commit comments