OpenVPN Simple Payment System - Version 3

From NAT, 6 Years ago, written in Bash, viewed 825 times. This paste is a reply to Re: OpenVPN Simple Payment System - Version 2 from NAT - view diff
URL https://code.nat.moe/view/499f55a8 Embed
Download Paste or View Raw
  1. #!/bin/bash
  2.  
  3. cd $(dirname "$0")
  4.  
  5. provider='Nat'
  6. target='China Unicom'
  7. rate='1.2'       # 1.2 CNY/Gb
  8. orig_rate='0.72' # Server Traffic: 0.72 CNY/g
  9. pay_req='40.80'  # Server Fee: 40.80 CNY/mo
  10. sub_fee='3'      # Subscription fee: 8 CNY/mo
  11. unit='CNY'
  12.  
  13. function days {
  14.         echo $(( ( $(date -ud "$1" +'%s') - $(date -ud "$(date +%Y-%m-%d)" +'%s') )/60/60/24 ))
  15. }
  16.  
  17. function checkbill {
  18.         for user in $(cat user)
  19.         do
  20.                 uname=$(echo $user | awk -F: '{print $1}')
  21.                 lastpay=$(echo $user | awk -F: '{print $3}')
  22.                 contact=$(echo $user | awk -F: '{print $4}')
  23.                 [[ $(days $lastpay) -gt 29 ]] && echo "$uname has upcoming bill, contact $contact." && upcoming=1
  24.                 [[ ! -z $1 && ! $(days $lastpay) -gt 29 ]] && echo "$uname has no upcoming bill. Contact: $contact."
  25.         done
  26.         [[ -z $upcoming ]] && echo "No upcoming bills for $provider's Traffic Resell."
  27. }
  28.  
  29. function unstated {
  30.         [[ ! -z $1 ]] && _unstated=$(cat openvpn-status.log | grep $1 | grep -v 10.8.0 | awk -F, '{print $4}'|tr '\n' '+')0
  31.         [[ $ALL == 1 ]] && {
  32.                 users="$(cat usage.log | awk -F" " '{print $1}' | sort | uniq )"
  33.                 let sum=0
  34.                 for user in $users
  35.                 do
  36.                         sum=$(echo "scale=3; $sum+$(ALL=0 unstated $user info)"|bc)
  37.                 done
  38.                 echo $sum
  39.         }
  40.         [[ -z $2 ]] && {
  41.                 [[ ! -z $_unstated ]] && echo "+$_unstated"
  42.         }
  43.         [[ ! -z $2 && ! -z $_unstated ]] && {
  44.                 echo "scale=3; ($_unstated)/1024/1024" | bc
  45.         }
  46.         [[ ! -z $2 && -z $_unstated ]] && {
  47.                 echo 0
  48.         }
  49. }
  50.  
  51. function stat {
  52.         total_get=0
  53.         total_pay=$pay_req
  54.         total_balance=0
  55.         users=$1
  56.         [[ -z $1 ]] && users="$(cat usage.log revoked.log | awk -F" " '{print $1}' | sort | uniq )"
  57.         printf "%13s %10s %10s %10s %10s %10s %10s\n" Username Traffic Unstated Income Cost Revenue Balance
  58.         printf "%13s %10s %10s %10s %10s %10s %10s\n" -------- ------- -------- ------ ---- ------- -------
  59.         for user in $users
  60.         do
  61.                 [[ -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log revoked.log | grep $user | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
  62.                 [[ ! -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log | grep $user | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
  63.                 kbytes=$(echo "scale=2; $bytes/1024" | bc)
  64.                 mbytes=$(echo "scale=2; $kbytes/1024" | bc)
  65.                 gbytes=$(echo "scale=2; $mbytes/1024" | bc)
  66.                 credit=$(cat user | grep $user | awk -F":" '{print $2}')
  67.                 [[ -z $credit ]] && credit=0
  68.                 fee=$(echo "scale=2; $gbytes*$rate+$sub_fee" | bc)
  69.                 balance=$(echo "scale=2; $credit+3-$fee" | bc)
  70.                 [[ ! -z $(cat user | grep $user | grep -E "TEST_USER|FREE_USER") ]] && {
  71.                         fee=0
  72.                         credit=0
  73.                         balance=0
  74.                 }
  75.                 total_balance=$(echo "scale=2; $total_balance+$balance"|bc)
  76.                 total_get=$(echo "scale=2; $total_get+$fee" | bc)
  77.                 cost=$(echo "scale=2; $gbytes*$orig_rate" | bc)
  78.                 total_pay=$(echo "scale=2; $total_pay+$cost" | bc)
  79.                 printf "%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"
  80.         done
  81.         total_traffic=$(echo "scale=2; ($(cat usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024"|bc)
  82.         total_unstated=$(ALL=1 unstated)
  83.         printf "%13s %10s %10s %10s %10s %10s %10s\n" "Server" "N/A" "N/A" "0 $unit" "$pay_req $unit" "-$pay_req $unit" "N/A"
  84.         printf "%13s %10s %10s %10s %10s %10s %10s\n" "" "----------" "----------" "----------" "----------" "----------" "----------"
  85.         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"
  86. }
  87.  
  88. function bill {
  89.         [[ -z $1 ]] && exit 1
  90.         traffic_fee="$(echo "scale=5; $(stat $1 | grep $1 | awk -F" " '{print $2}')/1024*$rate"|bc)"
  91.         credit=$(cat user | grep $1 | awk -F":" '{print $2}')
  92.         info=$(cat user | grep $1 | awk -F":" '{print $4}')
  93.         [[ -z $info ]] && info=$1
  94.         [[ -z $credit ]] && credit=0
  95.         credit_after=$(echo "scale=2; $(cat user | grep $1 | awk -F":" '{print $2}')-$traffic_fee-$sub_fee"|bc)
  96.         echo "Receipt for $1 ($info)"
  97.         printf "%23s %12s\n" Service Fee
  98.         printf "%23s %12s\n" ------- ---
  99.         printf "%23s %12s\n" "Avaliable Credit" "$credit $unit"
  100.         printf "%23s %12s\n" "Traffic Fee ($rate $unit/G)" "-$traffic_fee $unit"
  101.         printf "%23s %12s\n" "Subscription Fee" "-$sub_fee $unit"
  102.         printf "%23s %12s\n" "" "----------"
  103.         printf "%23s %12s\n" "Creadit Remaining:" "$credit_after $unit"
  104. }
  105.  
  106. function pay {
  107.         [[ -z $1 ]] && return 1
  108.         bill $1
  109.         read -p "User $1 paied? (yes/no)" yn
  110.         [[ $yn == 'yes' ]] && {
  111.                 echo "Removed all records for $1. Backup can be found in paid.log, usage.log.bak, revoked.log.bak."
  112.                 cat usage.log | grep $1 >> paid.log
  113.                 cp usage.log usage.log.bak
  114.                 cat usage.log.bak | grep -v $1 > usage.log
  115.                 cat revoked.log | grep $1 >> paid.log
  116.                 cp revoked.log revoked.log.bak
  117.                 cat revoked.log.bak | grep -v $1 > revoked.log
  118.         } || echo "Aborting."
  119. }
  120. $*

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