編寫腳本搭建代理服務(wù)
#/bin/bash
yum -y install gcc openldap-devel pam-devel openssl-devel
mkdir -p /data/ss5
cd /data/ss5
wget https://nchc.dl.sourceforge.net/project/ss5/ss5/3.8.9-8/ss5-3.8.9-8.tar.gz
tar -xf ss5-3.8.9-8.tar.gz
cd ss5-3.8.9/
./configure
make
make install
echo 'auth 0.0.0.0/0 - u
permit - 0.0.0.0/0 - 0.0.0.0/0 - - - - - ' > /etc/opt/ss5/ss5.conf
echo '用戶名 密碼' > /etc/opt/ss5/ss5.passwd
echo 'SS5_OPTS=" -u root -b 0.0.0.0:端口號"' > /etc/sysconfig/ss5
chmod u+x /etc/rc.d/init.d/ss5
chkconfig --add ss5
chkconfig ss5 on;
service ss5 start
注:腳本中的 用戶名
密碼
端口號
需要自定義去設(shè)置纸淮。
測試
# curl 測試
curl 要訪問的網(wǎng)址 --socks5 0.0.0.0:端口號 -U 用戶名:密碼
# python 腳本測試
import socket
import requests # pip install requests
import socks # pip install pysocks
# 代理服務(wù)器IP(域名)
socks5_proxy_host = '代理服務(wù)器ip'
# 代理服務(wù)器端口號
socks5_proxy_port = 端口號
# 設(shè)置代理
socks.set_default_proxy(socks.SOCKS5, socks5_proxy_host, socks5_proxy_port, username=用戶名, password=密碼)
socket.socket = socks.socksocket
# 某個必須通過代理才能訪問的網(wǎng)站
url = '被測網(wǎng)站'
resp = requests.get(url)
# 查看響應(yīng)結(jié)果
print(resp.status_code)
print(resp.text)
// java 測試
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.net.SocksProxy;
import java.io.IOException;
import java.net.*;
import java.util.concurrent.Callable;
public class SocketCheckThread implements Callable<CheckStatus>
{
private Logger logger = LoggerFactory.getLogger(SocketCheckThread.class);
/**
* 等待超時時間
*/
private final int SEC_WAIT = 5;
private String ip;
public SocketCheckThread(String ip)
{
this.ip = ip;
}
@Override
public CheckStatus call()
{
CheckStatus checkStatus = new CheckStatus();
checkStatus.setIp(ip);
checkStatus.setStatus(check());
return checkStatus;
}
private boolean check()
{
Socket socket = null;
try {
Proxy proxy = createProxy();
socket = new Socket(proxy);
socket.connect(new InetSocketAddress(this.ip ,80), SEC_WAIT * 1000);
return true;
}
catch (Exception e) {
logger.debug("檢測超時{}",this.ip,e);
}
finally
{
try
{
socket.close();
}
catch (IOException e)
{
logger.debug("關(guān)閉socket失敗",e.getMessage());
}
}
return false;
}
private static Proxy createProxy() {
// 設(shè)置認證
String proxyAddr = ip;
int proxyPort = port;
Authenticator.setDefault(new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
SocksProxy proxy = SocksProxy.create(new InetSocketAddress(proxyAddr, proxyPort),5);
return proxy;
}
}
僅供學(xué)習(xí)記錄做修!
https://blog.csdn.net/JOJOY_tester/article/details/89411532