OpenVPN Simple Payment System - Version 3

From NAT, 6 Years ago, written in Bash, viewed 840 times. This paste is a reply to Re: OpenVPN Simple Payment System - Version 2 from NAT - go back
URL https://code.nat.moe/view/499f55a8/diff Embed
Viewing differences between Re: OpenVPN Simple Payment System - Version 2 and OpenVPN Simple Payment System - Version 3
#!/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 fullstat days {
        STEP=2 stat
        report
echo $(( ( $(date -ud "$1" +'%s') - $(date -ud "$(date +%Y-%m-%d)" +'%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}')
$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; $sum+$(ALL=0 unstated $user info)"|bc)
                done
                echo $sum
        }
        [[ -z $2 ]] && {
                [[ ! -z $_unstated ]] && echo "+$_unstated"
        }
        [[ ! -z $2 && ! -z $_unstated ]] && {
                echo "scale=3; $_unstated/1024/1024" ($_unstated)/1024/1024" | bc
        }
        [[ ! -z $2 && -z $_unstated ]] && {
                echo 0
        }
}

function report {
        total_get=$(echo "scale=2; ($(cat paid.log usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024/1024*$rate"|bc)
        total_pay=$(echo "scale=2; ($(cat paid.log usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024/1024*$orig_rate+$pay_req"|bc)
        printf "%10s %10s %10s %10s %10s %10s\n" "Server" "N/A" "N/A" "0" "$pay_req $unit" "-$pay_req $unit"
        printf "%10s %10s %10s %10s %10s %10s\n" "" "" "" "----------" "----------" "----------"
        printf "%10s %10s %10s %10s %10s %10s\n" "Total:" "" "" "$total_get $unit" "$total_pay $unit" "$(echo "scale=3; $total_get-$total_pay"|bc) $unit" 
}

function stat {
        total_get=0
        total_pay=$pay_req
        total_balance=0
        users=$1
        [[ -z $1 ]] && users="$(cat usage.log revoked.log | awk -F" " '{print $1}' | sort | uniq )"

        
)"
        
printf "%10s "%13s %10s %10s %10s %10s %10s %10s\n" Username Traffic Unstated Fee Income Cost Revenue
        
Revenue Balance
        
printf "%10s "%13s %10s %10s %10s %10s %10s %10s\n" -------- ------- -------- --- ------ ---- ------- -------
        for user in $users
        do
                [[ -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log revoked.log | grep $user | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
                [[ ! -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log | grep $user | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
                
kbytes=$(echo "scale=3; "scale=2; $bytes/1024" | bc)
                mbytes=$(echo "scale=3; "scale=2; $kbytes/1024" | bc)
                gbytes=$(echo "scale=3; "scale=2; $mbytes/1024" | bc)
                credit=$(cat user | grep $user | awk -F":" '{print $2}')
                [[ -z $credit ]] && credit=0
                
fee=$(echo "scale=2; $gbytes*$rate" $gbytes*$rate+$sub_fee" | bc)
                balance=$(echo "scale=2; $credit+3-$fee" | bc)
                [[ ! -z $(cat user | grep $user | 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)
                
printf "%10s "%13s %10s %10s %10s %10s %10s %10s\n" ${user} "${mbytes} MB" "$(unstated $user info) MB" "${fee} ${unit}" "${cost} ${unit}" "$(echo "scale=3; $fee-$cost" | bc) $unit" "$balance $unit"
        done
        #[[ ! $STEP == "2" && -z $1 ]] && {
        #        printf "%10s %10s %10s %10s %10s %10s\n" "" "" "" "Total:" "$(echo 
total_traffic=$(echo "scale=2; ($(cat paid.log usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024/1024*$rate"|bc) '+')0)/1024/1024"|bc)
        total_unstated=$(ALL=1 unstated)
        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" "$total_unstated MB" "$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
        stat 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 and 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."
}
$*

Replies to OpenVPN Simple Payment System - Version 3 rss

Title Name Language When
OpenVPN Simple Payment System - Version 3.1 Melodic Parakeet bash 6 Years ago.

Reply to "OpenVPN Simple Payment System - Version 3"

Here you can reply to the paste above

captcha