はなふぶき

【Window】Tor(洋葱头路由)+Privoxy 网络实践(附带Java实例代码)

1.背景

平时我们需要访问onion后缀的网站,需要通过Tor(The Onion Router,洋葱路由器)。一般来说安装Tor Broswer就可以满足需要。但是项目我要做的是通过程序来获取onion网站里面的内容。Tor官网推荐使用的是Stem来操作Tor,但是Stem 是python的库,我却对Java较为熟悉。于是就想用Tor+Privoxy搭建网络,然后在Java中使用上述的代理网络获取onion网站的数据。

一开始因为走错路(最开始在ubuntu下配置,发现都无法成功,原因未明,足足弄了一天=.=),后来转到Window下配置,发现很顺利!所以记录一下配置过程。

2.环境搭建

2.1 Tor

去Tor官网下载最新版本(https://www.torproject.org/download/download  ),如果上面的地址访问不了(被墙,你懂的),可以使用这个地址https://tor.eff.org/download/download.html.en  。下载后直接使用默认选项进行安装,这里安装目录为:C:\tor-win32-0.2.7.6。安装完后, 启动 C:\tor-win32-0.2.7.6\Tor\tor.exe

注意:

目前Tor需要VPN才能正常使用,所以启动tor.exe前先连接VPN,否则无法使用Tor网络。

2.2 Privoxy

Window平台下直接从官网下载安装包(http://www.silvester.org.uk/privoxy/Windows/3.0.23/ ),这里的安装目录为:C:\Program Files (x86)\Privoxy,安装好后打开目录下的config.txt,添加如下配置项:

forward-socks5 / localhost:9050 .

如下图所示:

其中9050是Tor默认监听端口,如果你修改了这里也得跟着修改。

此时重启Privoxy即可。这里需要注意的是,Privoxy默认使用的是8118端口。

3.使用Tor网络

3.1 在Firefox中使用

Firefox–选项–高级–网络–连接,然后点击“设置”,进入代理设置界面。

填写如下信息即可

保存后,不出意外就可以访问onion网络了。onion网站可以再这里找到:http://thehiddenwiki.org/

上一张效果图

3.2 在程序中使用

跟Firefox中同样的原理,想要在Java中使用Tor,简单的就是使用代理(之前试过Orchid,silvertunnel_ng这类框架,都没有起作用,后来就没有继续尝试,转用Privoxy,如果有朋友有可行的方案,望分享)。

直接上代码:

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.NameValuePair;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

/**

* 使用tor代理下载onion网页

* @author nerve

*/

public class TorHttpClient {

public static void main(String[] args) {

// 创建HttpClientBuilder 

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); 

// HttpClient 

        CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); 

// 依次是代理地址,代理端口号,协议类型 

        HttpHost proxy = new HttpHost(“127.0.0.1”, 8118, “http”); 

        RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); 

// 请求地址 

        HttpGet httpPost = new HttpGet(“http://mobil7rab6nuf7vx.onion/”); 

httpPost.setConfig(config); 

try { 

            CloseableHttpResponse response = closeableHttpClient.execute(httpPost);

            HttpEntity httpEntity = response.getEntity(); 

            System.out.println(“response code=”+response.getStatusLine().getStatusCode());

if (httpEntity != null) { 

// 打印响应内容 

                System.out.println(“response content: \n”

                        + EntityUtils.toString(httpEntity, “UTF-8”)); 

            } 

// 释放资源 

closeableHttpClient.close(); 

        } catch (Exception e) { 

e.printStackTrace(); 

        }

    }

}

引用的Jar包:

OK。

码字很辛苦,转载请注明来自はなふぶき博客《【Window】Tor(洋葱头路由)+Privoxy 网络实践(附带Java实例代码)》

评论