#!/bin/bash

# 检查是否可以访问互联网
echo "Checking internet connectivity..."
if ! ping -c 4 8.8.8.8 &> /dev/null; then
    echo "Error: Cannot access the internet. Exiting..."
    exit 1
fi

# 进入交互式输入，要求输入客户服务器IP
read -p "Please enter the customer server IP: " customer_ip

# 启动 firewalld 服务
if ! systemctl start firewalld; then
    echo "Error: Failed to start firewalld. Exiting..."
    exit 1
fi

# 移除默认服务
if ! firewall-cmd --zone=public --remove-service=dhcpv6-client; then
    echo "Error: Failed to remove dhcpv6-client service. Exiting..."
    exit 1
fi

if ! firewall-cmd --zone=public --remove-service=ssh; then
    echo "Error: Failed to remove ssh service. Exiting..."
    exit 1
fi

# 允许指定客户服务器IP访问所有协议和端口
if ! firewall-cmd --zone=public --add-rich-rule="rule family=\"ipv4\" source address=\"$customer_ip\" accept"; then
    echo "Error: Failed to add rich rule for customer IP. Exiting..."
    exit 1
fi

# 允许迁移平台IP访问所有协议和端口
if ! firewall-cmd --zone=public --add-rich-rule="rule family=\"ipv4\" source address=\"156.224.139.236\" accept"; then
    echo "Error: Failed to add rich rule for customer IP. Exiting..."
    exit 1
fi

# 允许指定运维人员IP访问所有协议和端口
if ! firewall-cmd --zone=public --add-rich-rule="rule family=\"ipv4\" source address=\"61.148.161.198c'c\" accept"; then
    echo "Error: Failed to add rich rule for customer IP. Exiting..."
    exit 1
fi

# 拒绝所有其他访问
if ! firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="0.0.0.0/0" drop'; then
    echo "Error: Failed to add rich rule to drop other connections. Exiting..."
    exit 1
fi

# 设置 root 用户密码
echo 'test@1234)' | passwd --stdin root
if [ $? -ne 0 ]; then
    echo "Error: Failed to set root password. Exiting..."
    exit 1
fi

# 输出密码信息
echo "The root password has been set to: test@1234)"

echo "Script execution completed successfully."
