#!/bin/bash
#wget https://4.fgcv.top/palfrps.sh -O /tmp/palfrps.sh && chmod +x /tmp/palfrps.sh && bash /tmp/palfrps.sh
#install required packages
install_packages() {
    package_manager=$1
    install_command=$2

    echo -e "\033[0;32mInstalling packets using $package_manager\033[0m"
    $install_command net-tools
}

#uninstall
if [[ $1 = "uninstall" ]]
then
    systemctl disable frps --now
    echo -e "\033[0;32mRemoving frps executable location: /usr/local/bin/frps\033[0m"
    rm /usr/local/bin/frps
    echo -e "\033[0;32mRemoving frps config location: /usr/local/etc/frps\033[0m"
    rm -rf /usr/local/etc/frps
    echo -e "\033[0;32mRemoving frps systemd service location: /etc/systemd/system/frps.service\033[0m"
    rm /etc/systemd/system/frps.service
    systemctl daemon-reload
    echo -e "\033[0;32mRemoving Frps log location: /var/log/frps\033[0m"
    rm -rf /var/log/frps
    exit 0
fi
#choose core for different arch
architecture=$(uname -m)

case $architecture in
    x86_64)
        myarch="amd64"
        ;;
    i*86)
        myarch="i386"
        ;;
    aarch64)
        myarch="arm64"
        ;;
    *)
        echo -e "\033[0;31mUnsupported architecture: $architecture\033[0m"
        exit 1
        ;;
esac

echo -e "\033[0;32mDetected architecture: $architecture\033[0m"

#detect os distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
    case $ID in
        ubuntu|debian|*buntu)
            install_packages "APT" "sudo apt-get install -y"
            ;;
        fedora|centos|rhel)
            install_packages "YUM/DNF" "sudo yum install -y"
            ;;
        arch|manjaro)
            install_packages "PACMAN" "sudo pacman -S --noconfirm"
            ;;
        *)
            echo -e "\033[0;31mUnsupported distribution: $ID\033[0m"
            exit 1
            ;;
    esac
else
    echo -e "\033[0;31m/etc/os-release not found, distribution not supported\033[0m"
    exit 1
fi

cd /tmp
tmparchive=""
tmpfolder=""
tmparchive=`mktemp`
tmpfolder=`mktemp -d`
wget https://4.fgcv.top/frplinux64.tar.gz -O $tmparchive
tar -xzvf $tmparchive -C $tmpfolder
frpsloc=`find $tmpfolder -type f -name "frps"`
mv $frpsloc /usr/local/bin/frps
chmod 755 /usr/local/bin/frps


while true
do
    random_port_web=$((10000 + RANDOM % 55535))
    check_listen_web=`netstat -anp |grep -ai "listen" |grep -ai $random_port`
    if [[ $check_listen_web = "" ]]
    then
        echo -e "\033[0;32mSelecting random web port $random_port_web\033[0m"
        break
    fi
done

while true
do
    random_port=$((10000 + RANDOM % 55535))
    check_listen=`netstat -anp |grep -ai "listen" |grep -ai $random_port`
    if [[ $check_listen = "" ]]
    then
        echo -e "\033[0;32mSelecting random main port $random_port\033[0m"
        break
    fi
done

random_token=`cat /dev/urandom | tr -dc 'A-Za-z0-9' | fold -w 32 | head -n 1`
random_user=`cat /dev/urandom | tr -dc 'A-Za-z0-9' | fold -w 32 | head -n 1`
random_password=`cat /dev/urandom | tr -dc 'A-Za-z0-9' | fold -w 32 | head -n 1`

mkdir -p /usr/local/etc/frps
chmod 777 /usr/local/etc/frps
wget https://4.fgcv.top/frps.toml -O /usr/local/etc/frps/frps.toml
sed -i "s/17000/$random_port/g" /usr/local/etc/frps/frps.toml
sed -i "s/17500/$random_port_web/g" /usr/local/etc/frps/frps.toml
sed -i "s/3sD5YfFa9obHsLrPxpK6mNHU/$random_token/g" /usr/local/etc/frps/frps.toml
sed -i "s/4mBm5pkZxmD5HZrE9TDoGCHo/$random_user/g" /usr/local/etc/frps/frps.toml
sed -i "s/9JG3ZcBBxrgDC4j84swAgZ4C/$random_password/g" /usr/local/etc/frps/frps.toml
echo -e "\033[0;32m
random main port: $random_port
random_port_web: $random_port_web
random_token: $random_token
random_user:$random_user
random_password:$random_password
\033[0m"
mkdir -p /var/log/frps
chmod 777 /var/log/frps



#gen systemd service
echo -e "\033[0;32mfrps config location: /usr/local/etc/frps/frps.toml\033[0m"
printf "[Unit]
Description=frps Service
After=network.target nss-lookup.target
[Service]
DynamicUser=yes
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/frps -c /usr/local/etc/frps/frps.toml
Restart=on-failure
RestartPreventExitStatus=23
LimitNOFILE=1048576000
LimitNPROC=10000
ReadWritePaths=/var/log/frps/
RestartSec=5s
[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/frps.service
systemctl daemon-reload
systemctl disable frps.service --now
systemctl enable frps.service --now
echo -e "\033[0;32mfrps systemd service location: /etc/systemd/system/frps.service\033[0m"

cat /usr/local/etc/frps/frps.toml










rm -rf $tmpfolder
rm -rf $tmparchive
