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