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)"
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 OpenVPN Simple Payment System - Version 2 
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}