Linux 上 socket5 代理服务器搭建

2020-11-16  本文已影响0人  kakarotto

编写脚本搭建代理服务

#/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

注:脚本中的 用户名 密码 端口号 需要自定义去设置。

测试

# curl 测试
curl 要访问的网址 --socks5 0.0.0.0:端口号 -U 用户名:密码
# python 脚本测试
import socket  
import requests  # pip install requests
import socks  # pip install pysocks

# 代理服务器IP(域名)
socks5_proxy_host = '代理服务器ip'
# 代理服务器端口号
socks5_proxy_port = 端口号

# 设置代理
socks.set_default_proxy(socks.SOCKS5, socks5_proxy_host, socks5_proxy_port, username=用户名, password=密码)
socket.socket = socks.socksocket

# 某个必须通过代理才能访问的网站
url = '被测网站'
resp = requests.get(url)

# 查看响应结果

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("关闭socket失败",e.getMessage());
            }
        }
        return false;
    }
 
 
    private static Proxy createProxy() {
        // 设置认证
        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;
    }
 }

仅供学习记录!
https://blog.csdn.net/JOJOY_tester/article/details/89411532

上一篇下一篇

猜你喜欢

热点阅读