一.代理服务器的基本信息
本文整理了vps拨号的配置和目前使用方式。
1.信息
1.1基本信息
host port user pass
1.2 squid
user pwd
2.实例
登陆实例:`ssh -p port user@host`
二. squid代理配置及信息
1.配置流程
1.1利用yum安装代理服务器
`yum -y install squid`
1.2修改配置文件
允许ip使用代理服务器上squid,其他ip访问需认证
#使用帐号密码认证方式使用代理
auth_param basic program /usr/lib64/squid/ncsa_auth /etc/squid/squid_user.txt
auth_param basic children 5
auth_param basic realm Welcome to pycredit's proxy-only web server
#定义授权组
acl normal src ip1
acl normal src ip2
acl squid_user proxy_auth REQUIRED
#定义端口
acl Safe_ports port 80 # http
acl Safe_ports port 443 # https
#拒绝所有非定义的端口
http_access deny !Safe_ports
#允许授权组
http_access allow normal
http_access allow squid_user
#拒绝其它所有未定义的
http_access deny all
# Squid端口
http_port 3128
#缓存设置
cache_dir ufs /var/spool/squid 100 16 256 read-only
cache_mem 0 MB
coredump_dir /var/spool/squid
1.3生成密码文件
htpasswd -c /etc/squid/squid_user.txt admin
目前用户名密码为admin:***
三.测试
1.拨号流程
pppoe-start 拨号
pppoe-stop 断开拨号
pppoe-status拨号连接状态
2.获取当前ip
pppoe-status|grep inet |awk {'print $2'}
3. 客户端使用代理
3.1直接使用代理
curl -x ip:3128 http://www.baidu.com
2.2认证使用代理
curl -x ip:3128 --proxy-user user:pwd www.baidu.com
四.其他
1.代理切换及获取
为方便使用,现代理服务器配置客户端的免密登陆。
1.1获取当前ip
`ssh -p 21103 user@host 'pppoe-status|grep inet' |awk {'print $2'}`
1.2更改ip并获取
`ssh -p 21103 user@host 'pppoe-stop;pppoe-start;pppoe-status|grep inet' |awk {'print $2'}`
2. java代理认证访问测试样例
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
class ProxyAuthenticator extends Authenticator {
private String user, password;
public ProxyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
注意:下面代码仅仅实现HTTP请求链接,每一次请求都是无状态保留的,仅仅是这次请求是更换IP的,如果下次请求的IP地址会改变
如果是多线程访问的话,只要将下面的代码嵌入到你自己的业务逻辑里面,那么每次都会用新的IP进行访问,如果担心IP有重复
自己可以维护IP的使用情况,并做校验。
public class ProxyTest {
public static void main(String args[]) throws Exception {
//要访问的目标页面
String targetUrl = "http://test.abuyun.com/proxy.php";
//String targetUrl = "http://proxy.abuyun.com/switch-ip";
//String targetUrl = "http://proxy.abuyun.com/current-ip";
//代理服务器
String proxyServer = "";
int proxyPort = 3128;
//代理隧道验证信息
String proxyUser = "";
String proxyPass = "";
try {
URL url = new URL(targetUrl);
Authenticator.setDefault(new ProxyAuthenticator(proxyUser, proxyPass));
//创建代理服务器地址对象
InetSocketAddress addr = new InetSocketAddress(proxyServer, proxyPort);
//创建HTTP类型代理对象
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
//设置通过代理访问目标页面
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
//设置IP切换头
// connection.setRequestProperty("Proxy-Switch-Ip","yes");
System.out.println(2);
//解析返回数据
byte[] response = readStream(connection.getInputStream());
System.out.println(new String(response));
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
}
}
/**
*将输入流转换成字符串
*
* @paraminStream
* @return
* @throws Exception
*/
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
}
3. phantomjs中使用
–proxy-auth=username:password
–proxy=ip:port