Guest User

Untitled

a guest
May 7th, 2017
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. public class TestTor {
  2.  
  3. private static TorClient client;
  4.  
  5. public static void main(String[] args) {
  6. startOrchid();
  7. }
  8.  
  9. private static void startOrchid() {
  10. //listen on 127.0.0.1:9150 (default)
  11. client = new TorClient();
  12. client.addInitializationListener(createInitalizationListner());
  13. client.start();
  14. client.enableSocksListener();//or client.enableSocksListener(yourPortNum);
  15. }
  16.  
  17. private static void stopOrchid() {
  18. client.stop();
  19. }
  20.  
  21. public static TorInitializationListener createInitalizationListner() {
  22. return new TorInitializationListener() {
  23. @Override
  24. public void initializationProgress(String message, int percent) {
  25. System.out.println(">>> [ " + percent + "% ]: " + message);
  26. }
  27.  
  28. @Override
  29. public void initializationCompleted() {
  30. List<Router> list = client.getDirectory().getAllRouters();
  31. System.out.println("size = " + list.size());
  32.  
  33. System.out.println("Tor is ready to go!");
  34. doTests();
  35. }
  36. };
  37. }
  38.  
  39. private static void doTests() {
  40. //Working
  41. testHttpURLConnection();
  42. //Not working
  43. testCloseableHttpClient();
  44.  
  45. }
  46.  
  47. private static void testHttpURLConnection() {
  48. new Thread(() -> {
  49. try {
  50. URL url = new URL("https://wtfismyip.com/");
  51. Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 9150));
  52. HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
  53. uc.setConnectTimeout(10000);
  54. Map<String, List<String>> map = uc.getHeaderFields();
  55.  
  56. System.out.println("status = " + uc.getResponseCode());
  57. for (Map.Entry<String, List<String>> entry : map.entrySet()) {
  58. System.out.println("Key : " + entry.getKey()
  59. + " ,Value : " + entry.getValue());
  60. }
  61.  
  62. } catch (MalformedURLException ex) {
  63. Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, null, ex);
  64. } catch (IOException ex) {
  65. Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, null, ex);
  66. }
  67. }).start();
  68. }
  69.  
  70. private static void testCloseableHttpClient() {
  71. Thread thread = new Thread() {
  72. @Override
  73. public void run() {
  74. try {
  75. HttpGet headMethod = new HttpGet("https://wtfismyip.com/");
  76. HttpHost proxy = new HttpHost("127.0.0.1", 9150);
  77. RequestConfig requestConfig = RequestConfig.custom()
  78. .setSocketTimeout(5 * 1000)
  79. .setConnectTimeout(5 * 1000)
  80. .build();
  81. headMethod.setConfig(requestConfig);
  82.  
  83. RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
  84. connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
  85.  
  86. SSLContext sslContext
  87. = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
  88. @Override
  89. public boolean isTrusted(final X509Certificate[] chain, String authType) {
  90. return true;
  91. }
  92. }).build();
  93. SSLConnectionSocketFactory sslsf
  94. = new SniSSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
  95. connRegistryBuilder.register("https", sslsf);
  96.  
  97. Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
  98. PoolingHttpClientConnectionManager connectionManager = new SniPoolingHttpClientConnectionManager(connRegistry);
  99. connectionManager.setMaxTotal(10);
  100. connectionManager.setDefaultMaxPerRoute(10);
  101.  
  102. CloseableHttpClient httpClient = HttpClientBuilder
  103. .create()
  104. .setDefaultRequestConfig(requestConfig)
  105. .setConnectionManager(connectionManager)
  106. .setProxy(proxy)
  107. .disableContentCompression()
  108. .build();
  109.  
  110. HttpResponse httpResponse = httpClient.execute(headMethod);
  111. int statusCode = httpResponse.getStatusLine().getStatusCode();
  112. System.out.println(statusCode);
  113. } catch (IOException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
  114. Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, null, ex);
  115. }
  116.  
  117. }
  118. };
  119. thread.start();
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment