OpenVPN Simple Payment System - Version 3.1

From Melodic Parakeet, 6 Years ago, written in Bash, viewed 717 times. This paste is a reply to OpenVPN Simple Payment System - Version 3 from NAT - go back
URL https://code.nat.moe/view/d8ef0858/diff Embed
Viewing differences between OpenVPN Simple Payment System - Version 3 and OpenVPN Simple Payment System - Version 3.1
#!/bin/bash

cd $(dirname "$0")

provider='Nat'
target='China Unicom'
rate='1.2'       # 1.2 CNY/Gb
orig_rate='0.72' # Server Traffic: 0.72 CNY/g
pay_req='40.80'  # Server Fee: 40.80 CNY/mo
sub_fee='3'      # Subscription fee: 8 CNY/mo
unit='CNY'

function days {
        echo $(( ( $(date -ud "$1" +'%s') - $(date -ud "$(date +%Y-%m-%d)" +'%s') - $(date -ud "$1" +'%s') )/60/60/24 ))
}

function checkbill {
        for user in $(cat user)
        do
                uname=$(echo $user | awk -F: '{print $1}')
                lastpay=$(echo $user | awk -F: '{print $3}')
                contact=$(echo $user | awk -F: '{print $4}')
                [[ $(days $lastpay) -gt 29 ]] && echo "$uname has upcoming bill, contact $contact." && upcoming=1
                [[ ! -z $1 && ! $(days $lastpay) -gt 29 ]] && echo "$uname has no upcoming bill. Contact: $contact."
        done
        [[ -z $upcoming ]] && echo "No upcoming bills for $provider's Traffic Resell."
}

function unstated {
        [[ ! -z $1 ]] && _unstated=$(cat openvpn-status.log | grep $1 | grep -v 10.8.0 | awk -F, '{print $4}'|tr '\n' '+')0
        [[ $ALL == 1 ]] && {
                users="$(cat usage.log | awk -F" " '{print $1}' | sort | uniq )"
                let sum=0
                for user in $users
                do
                        sum=$(echo "scale=3; "scale=2; $sum+$(ALL=0 unstated $user info)"|bc)
                done
                echo $sum
        }
        [[ -z $2 ]] && {
                [[ ! -z $_unstated ]] && echo "+$_unstated"
        }
        [[ ! -z $2 && ! -z $_unstated ]] && {
                echo "scale=3; "scale=2; ($_unstated)/1024/1024" | bc
        }
        [[ ! -z $2 && -z $_unstated ]] && {
                echo 0
        }
}

function stat {
        total_get=0
        total_pay=$pay_req
        total_balance=0
        online="$(cat openvpn-status.log | grep 10.8.0 | awk -F, '{print $2}')"
        users=$1
        [[ -z $1 ]] && users="$(cat usage.log revoked.log | awk -F" " '{print $1}' | sort | uniq )"
        printf "%13s %10s %10s %10s %10s %10s %10s\n" Username Traffic Unstated Income Cost Revenue Balance
        printf "%13s %10s %10s %10s %10s %10s %10s\n" -------- ------- -------- ------ ---- ------- -------
        for user in $users
        do
                [[ -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log revoked.log | grep $user "$user\ " | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
                [[ ! -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log | grep $user "$user\ " | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
                kbytes=$(echo "scale=2; $bytes/1024" | bc)
                mbytes=$(echo "scale=2; $kbytes/1024" | bc)
                gbytes=$(echo "scale=2; $mbytes/1024" | bc)
                usertype="$(cat user | grep "$user:" | awk -F: '{print $5}')"
                [[ -z $usertype || ! -z $STAT_ALL_AS_NORMAL ]] && usertype=NORMAL
                _special=$(cat extra_fee_policy | grep $usertype)
                #echo "[DEBUG] _special=$_special | usertype=$usertype" > /dev/stderr
                [[ ! -z $_special ]] && {
                        _rate=$rate
                        _sub_fee=$sub_fee
                        rate=$(echo $_special | awk -F: '{print $3}')
                        sub_fee=$(echo $_special | awk -F: '{print $2}')
                }
                #echo "[DEBUG] rate=$rate | sub_fee=$sub_fee" > /dev/stderr
                
credit=$(cat user | grep $user "$user:" | awk -F":" '{print $2}')
                [[ -z $credit ]] && credit=0
                fee=$(echo "scale=2; $gbytes*$rate+$sub_fee" | bc)
                balance=$(echo "scale=2; $credit+3-$fee" $credit+$sub_fee-$fee" | bc)
                [[ ! -z $(cat user | grep $user $(echo $usertype | grep -E "TEST_USER|FREE_USER") ]] && {
                        fee=0
                        credit=0
                        balance=0
                }
                total_balance=$(echo "scale=2; $total_balance+$balance"|bc)
                total_get=$(echo "scale=2; $total_get+$fee" | bc)
                cost=$(echo "scale=2; $gbytes*$orig_rate" | bc)
                total_pay=$(echo "scale=2; $total_pay+$cost" | bc)
                [[ ! -z $(echo $online | grep "$user") ]] && onlinetag="*" || unset onlinetag
                [[ ! -z $NEGATIVE && ! $balance == '-'* ]] || 
printf "%13s %10s %10s %10s %10s %10s %10s\n" ${user} "${onlinetag}${user}" "${mbytes} MB" "$(unstated $user info) MB" "${fee} ${unit}" "${cost} ${unit}" "$(echo "scale=3; $fee-$cost" | bc) $unit" "$balance $unit"
$unit"

                [[ ! -z $_special ]] && rate=$_rate && sub_fee=$_sub_fee
        done
        total_traffic=$(echo "scale=2; ($(cat usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024"|bc)
        total_unstated=$(ALL=1 unstated)
'+')0)/1024/1024/1024"|bc)
        total_unstated=$(echo "scale=2; $(ALL=1 unstated)/1024" | bc )
        printf "%13s %10s %10s %10s %10s %10s %10s\n" "Server" "N/A" "N/A" "0 $unit" "$pay_req $unit" "-$pay_req $unit" "N/A"
        printf "%13s %10s %10s %10s %10s %10s %10s\n" "" "----------" "----------" "----------" "----------" "----------" "----------"
        printf "%13s %10s %10s %10s %10s %10s %10s\n" "Total:" "$total_traffic MB" GB" "$total_unstated MB" GB" "$total_get $unit" "$total_pay $unit" "$(echo "scale=2; $total_get-$total_pay"|bc) $unit" "$total_balance $unit"
}

function bill {
        [[ -z $1 ]] && exit 1
        traffic_fee="$(echo "scale=5; $(stat $1 | grep $1 | awk -F" " '{print $2}')/1024*$rate"|bc)"
        credit=$(cat user | grep $1 | awk -F":" '{print $2}')
        info=$(cat user | grep $1 | awk -F":" '{print $4}')
        [[ -z $info ]] && info=$1
        [[ -z $credit ]] && credit=0
        credit_after=$(echo "scale=2; $(cat user | grep $1 | awk -F":" '{print $2}')-$traffic_fee-$sub_fee"|bc)
        echo "Receipt for $1 ($info)"
        printf "%23s %12s\n" Service Fee
        printf "%23s %12s\n" ------- ---
        printf "%23s %12s\n" "Avaliable Credit" "$credit $unit"
        printf "%23s %12s\n" "Traffic Fee ($rate $unit/G)" "-$traffic_fee $unit"
        printf "%23s %12s\n" "Subscription Fee" "-$sub_fee $unit"
        printf "%23s %12s\n" "" "----------"
        printf "%23s %12s\n" "Creadit Remaining:" "$credit_after $unit"
}

function pay {
        [[ -z $1 ]] && return 1
        bill $1
        read -p "User $1 paied? (yes/no)" yn
        [[ $yn == 'yes' ]] && {
                echo "Removed all records for $1. Backup can be found in paid.log, usage.log.bak, revoked.log.bak."
                cat usage.log | grep $1 >> paid.log
                cp usage.log usage.log.bak
                cat usage.log.bak | grep -v $1 > usage.log
                cat revoked.log | grep $1 >> paid.log
                cp revoked.log revoked.log.bak
                cat revoked.log.bak | grep -v $1 > revoked.log
        } || echo "Aborting."
}
}

$*

Reply to "OpenVPN Simple Payment System - Version 3.1"

Here you can reply to the paste above

captcha