记录一次阿里云ECS搭建代理服务器的过程

[参考资料](Tinyproxy安装与配置(ip代理) - 林先生 (downdawn.com))

1.一键安装脚本

vim proxy.sh
#! /bin/bash

# 配置文件
CONFIG_FILE="/etc/tinyproxy/tinyproxy.conf"

# 下载
wget -P /opt/software https://github.com/tinyproxy/tinyproxy/releases/download/1.11.0-rc1/tinyproxy-1.11.0-rc1.tar.gz

# 解压
mkdir /opt/module
cd /opt/software
tar -zxvf tinyproxy-1.11.0-rc1.tar.gz -C /opt/module/

# 编译安装
cd /opt/module/tinyproxy-1.11.0-rc1/
yum -y install gcc

./configure
make
make install

# 添加配置
mkdir /etc/tinyproxy
#cp /usr/local/etc/tinyproxy/tinyproxy.conf /etc/tinyproxy/tinyproxy.conf
echo "User nobody" >> $CONFIG_FILE
echo "Group nobody" >> $CONFIG_FILE
echo "Port 8888" >> $CONFIG_FILE
echo "Timeout 20" >> $CONFIG_FILE
echo "DefaultErrorFile \"/usr/local/share/tinyproxy/default.html\"" >> $CONFIG_FILE
echo "StatFile \"/usr/local/share/tinyproxy/stats.html\"" >> $CONFIG_FILE
echo "LogFile \"/var/log/tinyproxy/tinyproxy.log\"" >> $CONFIG_FILE
echo "LogLevel Info" >> $CONFIG_FILE
echo "MaxClients 500" >> $CONFIG_FILE
echo "ViaProxyName \"tinyproxy\"" >> $CONFIG_FILE
echo "BasicAuth 你自己的用户名 你自己的密码" >> $CONFIG_FILE

# 创建日志
mkdir /var/log/tinyproxy
touch /var/log/tinyproxy/tinyproxy.log
chmod 777 /var/log/tinyproxy/tinyproxy.log

echo '启动'
ps -ef|grep tinyproxy|grep -v grep|awk '{print "kill -9 "$2}'|sh
nohup tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf > /dev/null 2>&1 &

添加权限

chmod 777 proxy.sh

运行proxy.sh

sh proxy.sh

检测

tinyproxy -v

2.控制程序脚本

vim /usr/bin/tp
#!/bin/bash
if [ $# -lt 1 ]
then
    echo "No Args Input..."
    exit ;
fi
case $1 in
"start")
        echo " =================== 启动 ==================="
        nohup tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf > /dev/null 2>&1 &
;;
"stop")
        echo " =================== 关闭 ==================="
        ps -ef|grep tinyproxy|grep -v grep|awk '{print "kill -9 "$2}'|sh
;;
"restart")
        echo " =================== 重启 ==================="
        ps -ef|grep tinyproxy|grep -v grep|awk '{print "kill -9 "$2}'|sh
        nohup tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf > /dev/null 2>&1 &
;;
"status")
        echo " =================== 状态 ==================="
        ps -ef|grep tinyproxy|grep -v grep
;;
*)
    echo "Input Args Error..."
;;
esac

添加权限

chmod 777 /usr/bin/tp

测试脚本

tp start
tp stop
tp status
tp restart

查看日志

tail -f /var/log/tinyproxy/tinyproxy.log

3.测试tinyproxy

# 不加验证参数不会正常返回
curl -x http://127.0.0.1:8888 http://httpbin.org/get
Proxy Authentication Required

# 正常返回
curl -x http://admin:123456@127.0.0.1:8888 http://httpbin.org/get

注意,如果外部测试无反应,请检测是否开通服务器安全组和防火墙的8888端口。

iptables开放8888端口

iptables -A INPUT -p tcp --dport 8888 -j ACCEPT
# 查询端口是否开放
firewall-cmd --query-port=8888/tcp
# 开放80端口
firewall-cmd --permanent --add-port=8888/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

加入是阿里云ECS还需要去配置该实例的安全组规则

JAVA代码测试

public class TinyProxyTest {
    
    public static void main(String[] args) throws IOException {

        final String authUser = "代理认证用户名";
        final String authPassword = "代理认证密码";
        
        //当Jsoup请求https资源时,需要解注释下面两句

        //System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "false");
        //System.setProperty("jdk.http.auth.proxying.disabledSchemes", "false");

        Authenticator.setDefault(
                new Authenticator() {
                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(
                                authUser, authPassword.toCharArray());
                    }
                }
        );

        Document doc = Jsoup.connect("http://www.bilibili.com").proxy("代理ip", 代理端口)
                //.header("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 10; COL-AL10 Build/HUAWEICOL-AL10)")
                .ignoreContentType(true).timeout(120 * 1000)
                .get();
        System.out.println(doc);
    }
}

posted @ 2022-10-19 15:19  镇魂帆-张  阅读(835)  评论(0编辑  收藏  举报