OpenVPN Simple Payment System - Version 3.1

From Melodic Parakeet, 6 Years ago, written in Bash, viewed 708 times. This paste is a reply to OpenVPN Simple Payment System - Version 3 from NAT - view diff
URL https://code.nat.moe/view/d8ef0858 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 "$(date +%Y-%m-%d)" +'%s') - $(date -ud "$1" +'%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=2; $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=2; ($_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.         online="$(cat openvpn-status.log | grep 10.8.0 | awk -F, '{print $2}')"
  56.         users=$1
  57.         [[ -z $1 ]] && users="$(cat usage.log revoked.log | awk -F" " '{print $1}' | sort | uniq )"
  58.         printf "%13s %10s %10s %10s %10s %10s %10s\n" Username Traffic Unstated Income Cost Revenue Balance
  59.         printf "%13s %10s %10s %10s %10s %10s %10s\n" -------- ------- -------- ------ ---- ------- -------
  60.         for user in $users
  61.         do
  62.                 [[ -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log revoked.log | grep "$user\ " | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
  63.                 [[ ! -z $NOREVOKE ]] && bytes=$(echo $(cat usage.log | grep "$user\ " | awk -F ":| " '{print $11}' | tr '\n' '+')0$(unstated $user) | bc)
  64.                 kbytes=$(echo "scale=2; $bytes/1024" | bc)
  65.                 mbytes=$(echo "scale=2; $kbytes/1024" | bc)
  66.                 gbytes=$(echo "scale=2; $mbytes/1024" | bc)
  67.                 usertype="$(cat user | grep "$user:" | awk -F: '{print $5}')"
  68.                 [[ -z $usertype || ! -z $STAT_ALL_AS_NORMAL ]] && usertype=NORMAL
  69.                 _special=$(cat extra_fee_policy | grep $usertype)
  70.                 #echo "[DEBUG] _special=$_special | usertype=$usertype" > /dev/stderr
  71.                 [[ ! -z $_special ]] && {
  72.                         _rate=$rate
  73.                         _sub_fee=$sub_fee
  74.                         rate=$(echo $_special | awk -F: '{print $3}')
  75.                         sub_fee=$(echo $_special | awk -F: '{print $2}')
  76.                 }
  77.                 #echo "[DEBUG] rate=$rate | sub_fee=$sub_fee" > /dev/stderr
  78.                 credit=$(cat user | grep "$user:" | awk -F":" '{print $2}')
  79.                 [[ -z $credit ]] && credit=0
  80.                 fee=$(echo "scale=2; $gbytes*$rate+$sub_fee" | bc)
  81.                 balance=$(echo "scale=2; $credit+$sub_fee-$fee" | bc)
  82.                 [[ ! -z $(echo $usertype | grep -E "TEST_USER|FREE_USER") ]] && {
  83.                         fee=0
  84.                         credit=0
  85.                         balance=0
  86.                 }
  87.                 total_balance=$(echo "scale=2; $total_balance+$balance"|bc)
  88.                 total_get=$(echo "scale=2; $total_get+$fee" | bc)
  89.                 cost=$(echo "scale=2; $gbytes*$orig_rate" | bc)
  90.                 total_pay=$(echo "scale=2; $total_pay+$cost" | bc)
  91.                 [[ ! -z $(echo $online | grep "$user") ]] && onlinetag="*" || unset onlinetag
  92.                 [[ ! -z $NEGATIVE && ! $balance == '-'* ]] || printf "%13s %10s %10s %10s %10s %10s %10s\n" "${onlinetag}${user}" "${mbytes} MB" "$(unstated $user info) MB" "${fee} ${unit}" "${cost} ${unit}" "$(echo "scale=3; $fee-$cost" | bc) $unit" "$balance $unit"
  93.  
  94.                 [[ ! -z $_special ]] && rate=$_rate && sub_fee=$_sub_fee
  95.         done
  96.         total_traffic=$(echo "scale=2; ($(cat usage.log | awk -F ":| " '{print $11}' | tr '\n' '+')0)/1024/1024/1024"|bc)
  97.         total_unstated=$(echo "scale=2; $(ALL=1 unstated)/1024" | bc )
  98.         printf "%13s %10s %10s %10s %10s %10s %10s\n" "Server" "N/A" "N/A" "0 $unit" "$pay_req $unit" "-$pay_req $unit" "N/A"
  99.         printf "%13s %10s %10s %10s %10s %10s %10s\n" "" "----------" "----------" "----------" "----------" "----------" "----------"
  100.         printf "%13s %10s %10s %10s %10s %10s %10s\n" "Total:" "$total_traffic GB" "$total_unstated GB" "$total_get $unit" "$total_pay $unit" "$(echo "scale=2; $total_get-$total_pay"|bc) $unit" "$total_balance $unit"
  101. }
  102.  
  103. function bill {
  104.         [[ -z $1 ]] && exit 1
  105.         traffic_fee="$(echo "scale=5; $(stat $1 | grep $1 | awk -F" " '{print $2}')/1024*$rate"|bc)"
  106.         credit=$(cat user | grep $1 | awk -F":" '{print $2}')
  107.         info=$(cat user | grep $1 | awk -F":" '{print $4}')
  108.         [[ -z $info ]] && info=$1
  109.         [[ -z $credit ]] && credit=0
  110.         credit_after=$(echo "scale=2; $(cat user | grep $1 | awk -F":" '{print $2}')-$traffic_fee-$sub_fee"|bc)
  111.         echo "Receipt for $1 ($info)"
  112.         printf "%23s %12s\n" Service Fee
  113.         printf "%23s %12s\n" ------- ---
  114.         printf "%23s %12s\n" "Avaliable Credit" "$credit $unit"
  115.         printf "%23s %12s\n" "Traffic Fee ($rate $unit/G)" "-$traffic_fee $unit"
  116.         printf "%23s %12s\n" "Subscription Fee" "-$sub_fee $unit"
  117.         printf "%23s %12s\n" "" "----------"
  118.         printf "%23s %12s\n" "Creadit Remaining:" "$credit_after $unit"
  119. }
  120.  
  121. function pay {
  122.         [[ -z $1 ]] && return 1
  123.         bill $1
  124.         read -p "User $1 paied? (yes/no)" yn
  125.         [[ $yn == 'yes' ]] && {
  126.                 echo "Removed all records for $1. Backup can be found in paid.log, usage.log.bak, revoked.log.bak."
  127.                 cat usage.log | grep $1 >> paid.log
  128.                 cp usage.log usage.log.bak
  129.                 cat usage.log.bak | grep -v $1 > usage.log
  130.                 cat revoked.log | grep $1 >> paid.log
  131.                 cp revoked.log revoked.log.bak
  132.                 cat revoked.log.bak | grep -v $1 > revoked.log
  133.         } || echo "Aborting."
  134. }
  135.  
  136. $*

Reply to "OpenVPN Simple Payment System - Version 3.1"

Here you can reply to the paste above

captcha