File size: 2,839 Bytes
05afd0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash

INSTALL_DIR="/usr/local/proxylite"
DOTNET_DIR="$INSTALL_DIR/dotnet60"
SERVICE_DIR="$INSTALL_DIR/service"
DOTNET_URL="https://app.proxylite.ru/thirdparty/dotnet-runtime-6.0.10-linux-x64.tar.gz"
PROXYSERVICE_URL="https://app.proxylite.ru/netcoreapp-latest.tar"
PID_FILE="/var/run/proxylite.pid"

function check_root() {
    if [[ $(id -u) -ne 0 ]]; then
        echo "This script must be run as root"
        exit 1
    fi
}

function check_dependencies() {
    for cmd in wget tar; do
        if ! command -v $cmd &> /dev/null; then
            echo "Error: $cmd is not installed"
            exit 1
        fi
    done
}

function install_proxylite() {
    local user_id=$1

    echo "Installing ProxyLite"
    
    mkdir -p $DOTNET_DIR $SERVICE_DIR

    echo "Downloading .NET 6.0"
    wget $DOTNET_URL -O /tmp/dotnet.tar.gz
    echo "Unpacking .NET 6.0"
    tar xzf /tmp/dotnet.tar.gz -C $DOTNET_DIR
    rm /tmp/dotnet.tar.gz
    chmod +x $DOTNET_DIR/dotnet

    echo "Downloading ProxyService"
    wget $PROXYSERVICE_URL -O /tmp/proxyservice.tar
    echo "Unpacking ProxyService"
    tar xf /tmp/proxyservice.tar -C $SERVICE_DIR
    rm /tmp/proxyservice.tar

    echo $user_id > $SERVICE_DIR/userid.ini

    echo "ProxyLite installed successfully"
}

function start_proxylite() {
    if [[ -f $PID_FILE ]]; then
        echo "ProxyLite is already running"
        return
    fi

    echo "Starting ProxyLite"
    nohup $DOTNET_DIR/dotnet $SERVICE_DIR/ProxyService.Core.dll > /dev/null 2>&1 &
    echo $! > $PID_FILE
    echo "ProxyLite started with PID $(cat $PID_FILE)"
}

function stop_proxylite() {
    if [[ ! -f $PID_FILE ]]; then
        echo "ProxyLite is not running"
        return
    fi

    echo "Stopping ProxyLite"
    kill $(cat $PID_FILE)
    rm $PID_FILE
    echo "ProxyLite stopped"
}

function uninstall_proxylite() {
    echo "Uninstalling ProxyLite"
    stop_proxylite
    rm -rf $INSTALL_DIR
    echo "ProxyLite uninstalled"
}

function update_user_id() {
    local user_id=$1
    echo "Updating user ID to $user_id"
    echo $user_id > $SERVICE_DIR/userid.ini
    if [[ -f $PID_FILE ]]; then
        stop_proxylite
        start_proxylite
    fi
}

check_root
check_dependencies

if [[ $1 =~ ^[0-9]+$ ]]; then
    # If the first argument is a number, treat it as an install command
    install_proxylite $1
    start_proxylite
elif [[ $1 == "uninstall" ]]; then
    uninstall_proxylite
elif [[ $1 == "stop" ]]; then
    stop_proxylite
elif [[ $1 == "start" ]]; then
    start_proxylite
elif [[ $1 == "restart" ]]; then
    stop_proxylite
    start_proxylite
elif [[ $1 == "update" && $2 =~ ^[0-9]+$ ]]; then
    update_user_id $2
else
    echo "Usage: $0 <user_id> (to install and start)"
    echo "       $0 {start|stop|restart|uninstall}"
    echo "       $0 update <user_id>"
    exit 1
fi

exit 0