OpenVPN Simple Payment System - Version 2

From NAT, 6 Years ago, written in Bash, viewed 749 times. This paste is a reply to OpenVPN Simple Payment System from NAT - view diff
URL https://code.nat.moe/view/9573ea33 Embed
Download Paste or View Raw
  1. #!/bin/bash
  2.  
  3. cd $(dirname "$0")
  4.  
  5. rate='1.2'       # 1.2 CNY/Gb
  6. orig_rate='0.72' # Server Traffic: 0.72 CNY/g
  7. pay_req='40.80'  # Server Fee: 40.80 CNY/mo
  8. unit='CNY'
  9.  
  10. function fullstat {
  11.         STEP=2 stat
  12.         report
  13. }
  14.  
  15. function unstated {
  16.         _unstated=$(cat openvpn-status.log | grep $1 | grep -v 10.8.0 | awk -F, '{print $4}')
  17.         [[ -z $2 ]] && {
  18.                 [[ ! -z $_unstated ]] && echo "+$_unstated"
  19.         }
  20.         [[ ! -z $2 && ! -z $_unstated ]] && {
  21.                 echo "scale=3; $_unstated/1024/1024" | bc
  22.         }
  23.         [[ ! -z $2 && -z $_unstated ]] && {
  24.                 echo 0
  25.         }
  26. }
  27.  
  28. function report {
  29.         total_get=$(echo "scale=2; ($(cat paid.log usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024/1024*$rate"|bc)
  30.         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)
  31.         printf "%10s %10s %10s %10s %10s %10s\n" "Server" "N/A" "N/A" "0" "$pay_req $unit" "-$pay_req $unit"
  32.         printf "%10s %10s %10s %10s %10s %10s\n" "" "" "" "----------" "----------" "----------"
  33.         printf "%10s %10s %10s %10s %10s %10s\n" "Total:" "" "" "$total_get $unit" "$total_pay $unit" "$(echo "scale=3; $total_get-$total_pay"|bc) $unit"
  34. }
  35.  
  36. function stat {
  37.         users=$1
  38.         [[ -z $1 ]] && users="$(cat usage.log | awk -F" " '{print $1}' | sort | uniq )"
  39.  
  40.         printf "%10s %10s %10s %10s %10s %10s\n" Username Traffic Unstated Fee Cost Revenue
  41.         printf "%10s %10s %10s %10s %10s %10s\n" -------- ------- -------- --- ---- -------
  42.         for user in $users
  43.         do
  44.                 bytes=$(echo $(cat usage.log | grep $user | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
  45.                 kbytes=$(echo "scale=3; $bytes/1024" | bc)
  46.                 mbytes=$(echo "scale=3; $kbytes/1024" | bc)
  47.                 gbytes=$(echo "scale=3; $mbytes/1024" | bc)
  48.                 fee=$(echo "scale=2; $gbytes*$rate" | bc)
  49.                 cost=$(echo "scale=2; $gbytes*$orig_rate" | bc)
  50.                 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)"
  51.         done
  52.         [[ ! $STEP == "2" && -z $1 ]] && {
  53.                 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"
  54.         }
  55. }
  56.  
  57. function pay {
  58.         [[ -z $1 ]] && return 1
  59.         stat $1
  60.         read -p "User $1 paied? (yes/no)" yn
  61.         [[ $yn == 'yes' ]] && {
  62.                 echo "Removed all records for $1. Backup can be found in paid.log and usage.log.bak."
  63.                 cat usage.log | grep $1 >> paid.log
  64.                 cp usage.log usage.log.bak
  65.                 cat usage.log.bak | grep -v $1 > usage.log
  66.         } || echo "Aborting."
  67. }
  68. $*

Replies to OpenVPN Simple Payment System - Version 2 rss

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

Reply to "OpenVPN Simple Payment System - Version 2"

Here you can reply to the paste above

captcha