#!/bin/bash
#定义颜色
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m'   # Black
Red='\033[0;31m'     # Red
Green='\033[0;32m'   # Green
Yellow='\033[0;33m'  # Yellow
Blue='\033[0;34m'    # Blue
Magenta='\033[0;35m' # Magenta
Cyan='\033[0;36m'    # Cyan
White='\033[0;37m'   # White
#echo -e "${Green}I like Linux${Color_Off} normal."
#定义用户密码文件
#####################################################################
User_Password_file="/etc/squid/passwords"
squid_file="/etc/squid/"
https_port="7020" #定义好连接的端口
ssl_subj="/C=CN/ST=GD/L=SZ/O=Roffice/OU=Technology Department/CN=cloud.roffice.com.cn/emailAddress=admin@roffice.com.cn"
ssl_key="squid.key"
ssl_csr="squid.csr"
ssl_crt="squid.crt"
user_name="test"  #设置登录账号
user_password="P@ssw0rd"  # 设置登录密码
#user_password=$(for _ in {1..30};do tr -dc '~`!@#$%^&*()_+-={}:"<>?[];,./A-Za-z0-9"'"'" </dev/urandom |head -c 32;echo ;done |grep ^[a-zA-Z0-9])
#####################################################################
#install squid
echo -e "${Green}1 install squid software${Color_Off}"
yum install squid -y
#install httpd-tools
echo -e "${Green}2 install httpd-tools software${Color_Off}"
yum install httpd-tools -y
#install stunnel
#yum install stunnel -y
#install firewalld
echo -e "${Green}3 install firewalld software${Color_Off}"
yum install firewalld -y
#启动firewalld服务
echo -e "${Green}4 Start firewalld Services${Color_Off}"
# 使用 ip link show 命令获取所有网络接口信息，并提取网卡名称
interfaces=$(ip link show | grep -Eo '^[0-9]+: [a-zA-Z0-9]+:' | awk '{print $2}' | sed 's/://')
# 遍历输出每个网卡名称
for interface in $interfaces
do
firewall-cmd --zone=public --add-interface=${interface}
done
systemctl start firewalld
systemctl enable firewalld
systemctl status firewalld
#启动squid服务
echo -e "${Green}5 Start squid Services${Color_Off}"
systemctl start squid
systemctl enable squid
systemctl status squid
#创建用户和密码
echo -e "${Green}6 Create squid proxy User {${user_name} ${user_password}}${Color_Off}"
htpasswd -cdb ${User_Password_file} ${user_name} ${user_password}  
#openssl生成证书
echo -e "${Green}7 Openssl generates a certificate and private key {${ssl_crt} ${ssl_key}}${Color_Off} "
cd ${squid_file}
openssl genrsa -out ${ssl_key}
openssl req -new -key ${ssl_key} -out ${ssl_csr} -subj "${ssl_subj}"
openssl req -x509 -key ${ssl_key} -in ${ssl_csr} -out ${ssl_crt} -days 3650
#openssl x509 -in ${ssl_crt} -text -noout
#备份配置文件squid.conf
echo -e "${Green}8 Back up the configuration file {squid.conf}${Color_Off} "
cp /etc/squid/squid.conf{,.bak}
#初始化Squid代理服务
echo -e "${Green}9 Initializes the Squid proxy service${Color_Off} "
echo -e "${Green}10 Set the Squid proxy service authentication module ${Color_Off}"
sed -i 's/http_port 3128/#http_port 3128/g' /etc/squid/squid.conf
sed -i "4i auth_param basic program /usr/lib64/squid/basic_ncsa_auth ${User_Password_file}" squid.conf
sed -i "5i auth_param basic children 5" squid.conf
sed -i "6i auth_param basic credentialsttl 3 hours" squid.conf
sed -i "7i auth_param basic realm Example.com's Squid proxy-caching" squid.conf
sed -i "8i acl basic_user_auth proxy_auth REQUIRED" squid.conf
sed -i "58i http_access allow basic_user_auth " squid.conf
echo -e "${Green}11 Set Squid proxy service ports and ssl certificates${Color_Off} "
sed -i "65i https_port ${https_port} cert=/etc/squid/${ssl_crt} key=/etc/squid/${ssl_key}" squid.conf
systemctl restart squid
squid -k check
#设置firewall端口
echo -e "${Green}12 The firewall Squid proxy service port {${https_port}} is opened ${Color_Off}"
sudo firewall-cmd --zone=public --add-port="${https_port}/tcp" --permanent  > /dev/null 
sudo firewall-cmd --reload  > /dev/null 
sudo firewall-cmd --list-port  > /dev/null 
#查看服务状态
squid_status=$(systemctl status squid | grep -w Active | awk {'print $2'})
#获取IP地址
getIP=$(curl -s https://www.ipuu.net/ipuu/user/getIP)
out_ipaddress=$(echo ${getIP} | grep -o '"data":"[^"]*' | awk -F'"' '{print $4}')
in_ipaddress=$(ifconfig | grep -E 'inet [0-9]' | awk '{print $2}'| grep -v 127.0.0.1)
echo -e "---------------------------------------------------"
echo -e "
${Color_Off}1 安装stunnel隧道软件;
${Color_Off}2 请在stunnel.conf设置信息:
[httpssl]
client = yes
accept  = ${Green}127.0.0.1:${https_port}${Color_Off}
connect = ${Green}${in_ipaddress}:${https_port}${Color_Off}

"---------------------------------------------------"

${Color_Off}1 在浏览器设置代理信息
${Color_Off}代理地址  : ${Green}127.0.0.1${Color_Off}
${Color_Off}代理端口  : ${Green}${https_port}${Color_Off}
${Color_Off}代理账号  : ${Green}${user_name}${Color_Off}
${Color_Off}代理密码  : ${Green}${user_password}${Color_Off}
"
echo -e "---------------------------------------------------"