Как передать весь трафик моего Android-приложения через Tor программно? - PullRequest
0 голосов
/ 11 октября 2019

Я мог бы получить доступ к веб-адресу через HttpGet, используя эту библиотеку Tor .

Но я хочу перенаправить весь трафик моего приложения через установленный мной порт tor. Кто-нибудь может помочь мне, как это сделать?

DNS Resolver:

static class FakeDnsResolver implements DnsResolver {
    @Override
    public InetAddress[] resolve(String host) throws UnknownHostException {
        return new InetAddress[] { InetAddress.getByAddress(new byte[] { 1, 1, 1, 1 }) };
    }
}

HTTPClient:

    public HttpClient getNewHttpClient() {

    Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", new MyConnectionSocketFactory())
            .register("https", new MySSLConnectionSocketFactory(SSLContexts.createSystemDefault()))
            .build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg,new FakeDnsResolver());
    return HttpClients.custom()
            .setConnectionManager(cm)
            .build();
}

Asynctask: я не могу сохранить цену в хранилище:

private class TorTask extends android.os.AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... strings) {
        String fileStorageLocation = "torfiles";
        com.msopentech.thali.toronionproxy.OnionProxyManager onionProxyManager =
        new com.msopentech.thali.android.toronionproxy.AndroidOnionProxyManager(getApplicationContext(), fileStorageLocation);
        int totalSecondsPerTorStartup = 4 * 60;
        int totalTriesPerTorStartup = 5;
        try {
            boolean ok = onionProxyManager.startWithRepeat(totalSecondsPerTorStartup, totalTriesPerTorStartup);
            if (!ok)
                System.out.println("Couldn't start tor");

            while (!onionProxyManager.isRunning())
                Thread.sleep(90);
            System.out.println("Tor initialized on port " + onionProxyManager.getIPv4LocalHostSocksPort());

            HttpClient httpClient = getNewHttpClient();
            int port = onionProxyManager.getIPv4LocalHostSocksPort();
            InetSocketAddress socksaddr = new InetSocketAddress("127.0.0.1", port);
            HttpClientContext context = HttpClientContext.create();
            context.setAttribute("socks.address", socksaddr);

            HttpGet httpGet = new HttpGet("https://www.remove.bg/images/samples/combined/s6.jpg");
            HttpResponse httpResponse = httpClient.execute(httpGet, context);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream httpResponseStream = httpEntity.getContent();
            OutputStream os = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/example.jpg");

            byte[] b = new byte[2048];
            int length;

            while ((length = httpResponseStream.read(b)) != -1) {
                os.write(b, 0, length);
            }

            httpResponseStream.close();
            os.close();
            httpResponseStream.close();
        }
        catch (Exception e) {
            e.printStackTrace();

        }
        return "done!";
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onPostExecute(String result) {

    }
}

Теперь я хочу отправить и получить все свои запросы через порт tor.

...