Re: OpenVPN Simple Payment System - Version 2

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

cd $(dirname "$0")

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
unit='CNY'

function fullstat {
        STEP=2 stat
        report
}

function unstated {
        _unstated=$(cat openvpn-status.log | grep $1 | grep -v 10.8.0 | awk -F, '{print $4}')
        [[ -z $2 ]] && {
                [[ ! -z $_unstated ]] && echo "+$_unstated"
        }
        [[ ! -z $2 && ! -z $_unstated ]] && {
                echo "scale=3; $_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 {
        users=$1
        [[ -z $1 ]] && users="$(cat usage.log | awk -F" " '{print $1}' | sort | uniq )"

        printf "%10s %10s %10s %10s %10s %10s\n" Username Traffic Unstated Fee Cost Revenue
        printf "%10s %10s %10s %10s %10s %10s\n" -------- ------- -------- --- ---- -------
        for user in $users
        do
                bytes=$(echo $(cat usage.log | grep $user | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
                kbytes=$(echo "scale=3; $bytes/1024" | bc)
                mbytes=$(echo "scale=3; $kbytes/1024" | bc)
                gbytes=$(echo "scale=3; $mbytes/1024" | bc)
                fee=$(echo "scale=2; $gbytes*$rate" | bc)
                cost=$(echo "scale=2; $gbytes*$orig_rate" | bc)
                printf "%10s %10s %10s %10s %10s %10s\n" ${user} "${mbytes} MB" "$(unstated $user info) MB" "${fee} ${unit}" "${cost} ${unit}" "$(echo "scale=3; $fee-$cost" | bc)"
bc) $unit"
        done
        [[ #[[ ! $STEP == "2" && -z $1 ]] && {
                
{
        #        
printf "%10s %10s %10s %10s %10s %10s\n" "" "" "" "Total:" "$(echo "scale=2; ($(cat paid.log usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024/1024*$rate"|bc) $unit"
        }
#}
}

function pay {
        [[ -z $1 ]] && return 1
        stat $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 usage.log.bak."
                cat usage.log | grep $1 >> paid.log
                cp usage.log usage.log.bak
                cat usage.log.bak | grep -v $1 > usage.log
        } || echo "Aborting."
}
$*

Replies to Re: OpenVPN Simple Payment System - Version 2 rss

Title Name Language When
OpenVPN Simple Payment System - Version 3 NAT bash 6 Years ago.

Reply to "Re: OpenVPN Simple Payment System - Version 2"

Here you can reply to the paste above

captcha