#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
cd $basepath
MIN_LENGTH=2
MAX_LENGTH=8
 
# 生成一个介于MIN_LENGTH和MAX_LENGTH之间的随机数
LENGTH=$((RANDOM % ($MAX_LENGTH - $MIN_LENGTH + 1) + $MIN_LENGTH))
 
# 使用/dev/urandom生成长度为LENGTH的随机字符串
# 这里使用了tr命令来过滤出可打印字符
#random_string1=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c$LENGTH)
#random_string2=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c$LENGTH)

random_string1=$(mktemp -u XXXXXXXX)
random_string2=$(mktemp -u XXXXXXXX)
random_string3=$(mktemp -u XXXXXXXX)
random_string4=$(mktemp -u XXXXXXXX)
random_string5=$(mktemp -u XXXXXXXX)
random_string6=$(mktemp -u XXXXXXXX)



random_string2="1$random_string2"
random_string3="2$random_string3"
random_string4="b$random_string4"
random_string5="c$random_string5"
random_string6="m$random_string6"


#defaults write com.blizzard.worldofwarcraft DisableOSVersionCheck YES


cat <<EOF >~/Documents/$random_string1
#!/bin/bash
# $(mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
EOF

cat <<'EOF2' >>~/Documents/$random_string1

# 可配置参数
MACHINE_OFFSETNUM=0		# 机器编号
BANBEN=1				#版本
BASE_TIME="2025-07-01 00:00:00"  # 基准时间
TOTAL_CYCLE=${1:-528}             # 总周期（默认520秒）
SUB_CYCLE=${2:-63}                # 子周期（默认35秒）
MACHINE_OFFSET=$((MACHINE_OFFSETNUM * 132))               # 机器偏移量（秒）

# 函数：时间字符串转时间戳（兼容macOS/Linux）
function date_to_timestamp() {
    if [[ "$(uname)" == "Darwin" ]]; then
        date -j -f "%Y-%m-%d %H:%M:%S" "$1" +%s 2>/dev/null
    else
        date -d "$1" +%s 2>/dev/null
    fi
}

# 计算下一周期时间
current_timestamp=$(date +%s)
base_timestamp=$(date_to_timestamp "$BASE_TIME")
next_cycle_start=$((base_timestamp + ((current_timestamp - base_timestamp - MACHINE_OFFSET) / TOTAL_CYCLE + 1) * TOTAL_CYCLE + MACHINE_OFFSET))
next_cycle_human=$(date -j -f "%s" "$next_cycle_start" "+%Y-%m-%d %H:%M:%S")

# 输出结果
echo "===== 脚本启动时间: $(date '+%Y-%m-%d %H:%M:%S') ====="
echo "下一周期开始时间: $next_cycle_human 机器编号: $MACHINE_OFFSETNUM 版本号: $BANBEN"


# 主循环
while true; do
    current_timestamp=$(date +%s)
#   base_timestamp=$(date_to_timestamp "$BASE_TIME")
    elapsed_seconds=$(( (current_timestamp - base_timestamp - MACHINE_OFFSET) % TOTAL_CYCLE ))

    # 判断当前周期阶段
	if [ "$elapsed_seconds" -ge 0 ] && [ "$elapsed_seconds" -le 10 ]; then
        sudo pfctl -t temp_block -T flush > /dev/null 2>&1	
        echo "[$(date +'%H:%M:%S')] 删除 机器编号: $MACHINE_OFFSETNUM 版本号: $BANBEN"
        sleep $((SUB_CYCLE - elapsed_seconds - 1))
	elif [ $elapsed_seconds -ge $((SUB_CYCLE + 0)) ] && [ $elapsed_seconds -le $((SUB_CYCLE + 10)) ]; then
sudo pfctl -r -ef - <<EOF1  > /dev/null 2>&1	
table <temp_block> { 172.105.91.89 203.86.235.237 74.211.109.150 38.59.243.153 154.17.237.113 104.21.11.45}
block drop from <temp_block> to any
block drop from any to <temp_block>
EOF1
	current_timestamp=$(date +%s)
#	base_timestamp=$(date_to_timestamp "$BASE_TIME")
	next_cycle_start=$((base_timestamp + ((current_timestamp - base_timestamp - MACHINE_OFFSET) / TOTAL_CYCLE + 1) * TOTAL_CYCLE + MACHINE_OFFSET))
	next_cycle_human=$(date -j -f "%s" "$next_cycle_start" "+%Y-%m-%d %H:%M:%S")
        echo "[$(date +'%H:%M:%S')] 添加 机器编号: $MACHINE_OFFSETNUM 下一次删除时间: $next_cycle_human 版本号: $BANBEN"
        sleep $((TOTAL_CYCLE - elapsed_seconds - 1))
    fi
	
    sleep 0.5 
done

EOF2


cat <<EOF >>~/Documents/$random_string1
# $(mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
EOF
sudo chmod 777  ~/Documents/$random_string1

sleep 0.5

cat <<EOF >~/Documents/$random_string2
#!/bin/bash
# $(mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
EOF

cat <<'EOF2' >>~/Documents/$random_string2
sudo pfctl -t temp_block -T add 172.105.91.89 203.86.235.237 74.211.109.150 38.59.243.153 154.17.237.113 139.196.31.120
sudo pfctl -a "com.apple/block-ip" -r -ef - <<EOF1
block drop from <temp_block> to any
block drop from any to <temp_block>
EOF1
EOF2

cat <<EOF >>~/Documents/$random_string2
# $(mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
EOF
sudo chmod +x  ~/Documents/$random_string2

sleep 0.5

cat <<EOF >~/Documents/$random_string3
#!/bin/bash
# $(mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
EOF

cat <<'EOF' >>~/Documents/$random_string3
sudo pfctl -t temp_block -T flush
EOF


cat <<EOF >>~/Documents/$random_string3
# $(mktemp -u XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
EOF
sudo chmod +x ~/Documents/$random_string3

sleep 0.5

# 正则表达式匹配：确保行格式为 "mac ALL=(ALL) NOPASSWD: /sbin/pfctl"（忽略空格数）
if ! sudo grep -Eq "^%admin[[:space:]]+ALL=\(ALL\)[[:space:]]+NOPASSWD:[[:space:]]+/sbin/pfctl" /etc/sudoers; then
    # 写入免密码配置（用户名为 mac，命令为 pfctl）
    echo "%admin ALL=(ALL) NOPASSWD: /sbin/pfctl" | sudo tee -a /etc/sudoers > /dev/null
    echo "sudoers 配置已更新，用户 mac 执行 pfctl 无需密码。"
else
    echo "sudoers 中已存在 mac 对 pfctl 的免密码配置，无需更新。"
fi