Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestTor {
- private static TorClient client;
- public static void main(String[] args) {
- startOrchid();
- }
- private static void startOrchid() {
- //listen on 127.0.0.1:9150 (default)
- client = new TorClient();
- client.addInitializationListener(createInitalizationListner());
- client.start();
- client.enableSocksListener();//or client.enableSocksListener(yourPortNum);
- }
- private static void stopOrchid() {
- client.stop();
- }
- public static TorInitializationListener createInitalizationListner() {
- return new TorInitializationListener() {
- @Override
- public void initializationProgress(String message, int percent) {
- System.out.println(">>> [ " + percent + "% ]: " + message);
- }
- @Override
- public void initializationCompleted() {
- List<Router> list = client.getDirectory().getAllRouters();
- System.out.println("size = " + list.size());
- System.out.println("Tor is ready to go!");
- doTests();
- }
- };
- }
- private static void doTests() {
- //Working
- testHttpURLConnection();
- //Not working
- testCloseableHttpClient();
- }
- private static void testHttpURLConnection() {
- new Thread(() -> {
- try {
- URL url = new URL("https://wtfismyip.com/");
- Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 9150));
- HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
- uc.setConnectTimeout(10000);
- Map<String, List<String>> map = uc.getHeaderFields();
- System.out.println("status = " + uc.getResponseCode());
- for (Map.Entry<String, List<String>> entry : map.entrySet()) {
- System.out.println("Key : " + entry.getKey()
- + " ,Value : " + entry.getValue());
- }
- } catch (MalformedURLException ex) {
- Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, null, ex);
- }
- }).start();
- }
- private static void testCloseableHttpClient() {
- Thread thread = new Thread() {
- @Override
- public void run() {
- try {
- HttpGet headMethod = new HttpGet("https://wtfismyip.com/");
- HttpHost proxy = new HttpHost("127.0.0.1", 9150);
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(5 * 1000)
- .setConnectTimeout(5 * 1000)
- .build();
- headMethod.setConfig(requestConfig);
- RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
- connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
- SSLContext sslContext
- = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
- @Override
- public boolean isTrusted(final X509Certificate[] chain, String authType) {
- return true;
- }
- }).build();
- SSLConnectionSocketFactory sslsf
- = new SniSSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
- connRegistryBuilder.register("https", sslsf);
- Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
- PoolingHttpClientConnectionManager connectionManager = new SniPoolingHttpClientConnectionManager(connRegistry);
- connectionManager.setMaxTotal(10);
- connectionManager.setDefaultMaxPerRoute(10);
- CloseableHttpClient httpClient = HttpClientBuilder
- .create()
- .setDefaultRequestConfig(requestConfig)
- .setConnectionManager(connectionManager)
- .setProxy(proxy)
- .disableContentCompression()
- .build();
- HttpResponse httpResponse = httpClient.execute(headMethod);
- int statusCode = httpResponse.getStatusLine().getStatusCode();
- System.out.println(statusCode);
- } catch (IOException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
- Logger.getLogger(TestTor.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- };
- thread.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment