JuanFi onLogin/onLogout v6

  • no need to define hotspot folder! (copyright)
  • recommended for beginners!

Tested On:

  • hAP Lite 6.48.7 Stable
  • hEX GR3 7.12.1 Stable
  • RB5009 7.8 Long Term
  • CCR1009 6.49.10 Long Term

WARNING:

  • test first before deploy!

Author:

  • Chloe Renae & Edmar Lozada

Facebook Contact:

Copy script below and paste to hotspot user profile onLogin.

# juanfi_hs_61f_full_onLogin
# by: Chloe Renae & Edmar Lozada
# ------------------------------
local isTelegram  "0" ;# 0=disable or 1=enable
local iTGBotToken "xxxxxxxxxx:xxxxxxxxxxxxx-xxxxxxxxxxxxxxx-xxxxx" ;# Telegram Bot Token
local iTGrpChatID "xxxxxxxxxxxxxx" ;# Telegram Group Chat ID

local iUser $username
local iDMac $"mac-address"
local iDInt $interface
local aUser [/ip hotspot user get $iUser]
local iMail ($aUser->"email")
local eReplace do={
  local iRet
  for i from=0 to=([len $1]-1) do={
    local x [pick $1 $i]; if ($x=$2) do={set x $3}; set iRet ($iRet.$x)
  }; return $iRet
}
local eLogoutUser do={
  /ip hotspot active remove [find user=$iUser]
  /ip hotspot cookie remove [find user=$iUser]
  /ip hotspot cookie remove [find mac-address=$iDMac]
}
# Cancel user-login if Invalid User eMail/Profile
if (!($iMail~"new" || $iMail~"extend" || $iMail~"active")) do={
  log error "( $iUser ) ONLOGIN ERROR: /ip hotspot user email=[$iMail] => INVALID EMAIL!"
  local iTime [$eReplace [/system clock get time] ":" ""]
  /ip hotspot user set [find name=$iUser] name="$iUser-$iTime" disable=yes
  $eLogoutUser iUser=$iUser iDMac=$iDMac; return 0
}

# Check Valid Entry
if ($iMail~"new" || $iMail~"extend") do={
  local aNote [toarray ($aUser->"comment")]
  local iValidty [totime ($aNote->0)]
  local iSaleAmt [tonum ($aNote->1)]
  local iExtCode ($aNote->2)
  local iVendTag ($aNote->3)
  # Cancel user-login if Invalid Comment
  if (!($iValidty>=0 && ($iExtCode=0 || $iExtCode=1))) do={
    log error "( $iUser ) ONLOGIN ERROR! /ip hotspot user comment=[$aNote] => INVALID COMMENT!"
    local iTime [$eReplace [/system clock get time] ":" ""]
    /ip hotspot user set [find name=$iUser] name="$iUser-$iTime" disable=yes
    $eLogoutUser iUser=$iUser iDMac=$iDMac; return 0
  }
  local iMacFile  [$eReplace $iDMac ":" ""]
  local iProfile ($aUser->"profile")
  local iUsrTime ($aUser->"limit-uptime")
  local iHotSpot [/ip hotspot profile get [.. get [find interface=$iDInt] profile] html-directory]
  local iDevName ""
  if ([/ip dhcp-server lease find mac-address=$iDMac]!="") do={
    set iDevName [pick [/ip dhcp-server lease get [find mac-address=$iDMac] host-name] 0 15]
  }

  # Add User Scheduler
  do {
  if ([/system scheduler find name=$iUser]="") do={
    /system scheduler add name=$iUser interval=0 \
    on-event=("# EXPIRE ( $iProfile ) #\r\n".\
              "local iUser \"$iUser\"; local iDMac \"$iDMac\"\r\n".\
              "local iMacFile \"$iMacFile\"; local iHotSpot \"$iHotSpot\"\r\n".\
              "local aUser; local iUserTime 0s; local iUsedTime 0s; local iValidty 0s\r\n".\
              "do {\r\n".\
              "/ip hotspot active remove [find user=\$iUser]\r\n".\
              "if ([/ip hotspot user find name=\$iUser]!=\"\") do={\r\n".\
              "  set aUser [/ip hotspot user get \$iUser]\r\n".\
              "  set aUser [/ip hotspot user get \$iUser]\r\n".\
              "  set iUserTime (\$aUser->\"limit-uptime\")\r\n".\
              "  set iUsedTime (\$aUser->\"uptime\")\r\n".\
              "  if ([/system scheduler find name=\$iUser]!=\"\") do={\r\n".\
              "    set iValidty [/system scheduler get [find name=\$iUser] interval]\r\n".\
              "  } else={log error \"( \$iUser ) SCHEDULER ERROR! /system scheduler [\$iUser] => NOT FOUND!\"}\r\n".\
              "} else={log error \"( \$iUser ) SCHEDULER ERROR! /ip hotspot user [\$iUser] => NOT FOUND!\"}\r\n".\
              "log debug \"EXPIRE-HS ( Validity ) => user=[\$iUser] mac=[\$iDMac] usertime=[\$iUserTime] uptime=[\$iUsedTime] validity=[\$iValidty]\"\r\n".\
              "/ip hotspot cookie remove [find user=\$iUser]\r\n".\
              "/ip hotspot cookie remove [find mac-address=\$iDMac]\r\n".\
              "/system scheduler  remove [find name=\$iUser]\r\n".\
              "/ip hotspot user   remove [find name=\$iUser]\r\n".\
              "/file remove [find name=\"\$iHotSpot/data/\$iMacFile.txt\"]\r\n".\
              "} on-error={log error \"( \$iUser ) SCHEDULER ERROR! Expire User Module\"}\r\n".\
              "# END #\r\n")
    local x 10;while (($x>0)&&([/system scheduler find name=$iUser]="")) do={set x ($x-1);delay 1s}
    set iExtCode 0
  }
  } on-error={log error "( $iUser ) ONLOGIN ERROR! Add User Scheduler Module"}

  # Cancel user-login if user-scheduler NOT FOUND!
  do {
  if ([/system scheduler find name=$iUser]="") do={
    log error "( $iUser ) ONLOGIN ERROR! /system scheduler user=[$iUser] => NOT FOUND!"
    $eLogoutUser iUser=$iUser iDMac=$iDMac; return 0
  }
  } on-error={log error "( $iUser ) ONLOGIN ERROR! Cancel user-login Module"}

  # Create User Log/Validity/Interval/Comment/eMail/BugFix
  if ($iExtCode=0) do={ set iExtCode "ADDNEW" }
  if ($iExtCode=1) do={ set iExtCode "EXTEND" }
  set iValidty ($iValidty + [/system scheduler get [find name=$iUser] interval])
  if ($iValidty!=0s && $iValidty<$iUsrTime) do={ set iValidty ($iUsrTime+10s) }; #BugFix ( Validity < UserTime )
  if ($iValidty!=0s && $iValidty=$iUsrTime) do={ set iValidty ($iValidty+10s) }; #BugFix ( Validity = UserTime )
  local cUserTime $iUsrTime; if ([len $iUsrTime]=0) do={ set cUserTime "UNLI" }
  local cValidity $iValidty; if ($iValidty=0s) do={ set cValidity "NONE" }
  /system scheduler set [find name=$iUser] interval=$iValidty
  /ip hotspot user set [find name=$iUser] comment=""
  /ip hotspot user set [find name=$iUser] email="$iSaleAmt@$iMacFile.juanfi.active"
  log warning "$iExtCode-HS ( $iVendTag ) => user=[$iUser] mac=[$iDMac] usertime=[$cUserTime] validity=[$cValidity] amt=[$iSaleAmt]"

  # Set User Date/Time Variables
  local iDateBeg [/system scheduler get [find name=$iUser] start-date]
  local iTimeBeg [/system scheduler get [find name=$iUser] start-time]
  local iUserBeg ($iDateBeg." ".[pick $iTimeBeg 0 5])
  local iNextRun [/system scheduler get [find name=$iUser] next-run]
  local iUserEnd "NO EXPIRATION"
  if ([len $iNextRun]>1) do={
    set iUserEnd [pick $iNextRun 0 ([len $iNextRun]-3)]
  }
  log info "( $iUser ) beg=[$iUserBeg] end=[$iUserEnd] interface=[$interface] vendo=[$iVendTag] device=[$iDevName]"

  # Add User Data File
  local eAddData do={
    local iUser $1; local iMacFile $2; local iUserEnd $3; local iHotSpot $4
    do {
    if ([/file find name="$iHotSpot/data"]!="") do={
      /file print file="$iHotSpot/data/$iMacFile.txt" where name="$iMacFile.txt"
      local x 10;while (($x>0)&&([/file find name="$iHotSpot/data/$iMacFile.txt"]="")) do={set x ($x-1);delay 1s}
      if ([/file find name="$iHotSpot/data/$iMacFile.txt"]!="") do={
        /file set "$iHotSpot/data/$iMacFile" contents="$iUser#$iUserEnd"
      } else={log error "( $iUser ) ONLOGIN ERROR! /file [$iHotSpot/data/$iMacFile.txt] => NOT FOUND!"}
    } else={log error "( $iUser ) ONLOGIN ERROR! /file [$iHotSpot/data] => NOT FOUND!"}
    } on-error={log error "( $iUser ) ONLOGIN ERROR! Add User Data File Module"}
  }
  $eAddData $iUser $iMacFile $iUserEnd $iHotSpot

  # Update Sales ( Today/Month )
  local eAddSales do={
    local iUser $1; local iSaleAmt $2; local iSalesName $3; local iSalesComment $4
    do {
    if ([/system script find name=$iSalesName]="") do={
      log error "( $iUser ) ONLOGIN ERROR! /system script [$iSalesName] => AUTOCREATE!"
      /system script add name=$iSalesName source="0"
      local x 10;while (($x>0)&&([/system script find name=$iSalesName]="")) do={set x ($x-1);delay 1s}
    }
    } on-error={log error "( $iUser ) ONLOGIN ERROR! AutoCreate $iSalesName Module"}
    local iTotalAmt
    do {
    if ([/system script find name=$iSalesName]!="") do={
      local iSaveAmt [tonum [/system script get [find name=$iSalesName] source]]
      set $iTotalAmt ( $iSaleAmt + $iSaveAmt )
      /system script set [find name=$iSalesName] source="$iTotalAmt" comment=$iSalesComment
    } else={log error "( $iUser ) ONLOGIN ERROR! /system script [$iSalesName] => NOT FOUND!"}
    } on-error={log error "( $iUser ) ONLOGIN ERROR! Update $iSalesName Module"}
    return $iTotalAmt
  }
  local iSalesToday [$eAddSales $iUser $iSaleAmt "todayincome" "JuanFi Sales Today ( TOTAL )"]
  local iSalesMonth [$eAddSales $iUser $iSaleAmt "monthlyincome" "JuanFi Sales Month ( TOTAL )"]

  # Telegram Reporting
  do {
  if ($isTelegram=1) do={
    local iUActive [/ip hotspot active print count-only]
    local iMessage ("<<== $iExtCode ==>>%0A".\
                    "User: $iUser%0A".\
                    "IP: $address%0A".\
                    "MAC: $iDMac%0A".\
                    "Device: $iDevName%0A".\
                    "Active Users: $iUActive%0A%0A".\
                    "Vendo Name : $iVendTag%0A".\
                    "User Time  : $cUserTime%0A".\
                    "Beg: $iUserBeg%0A".\
                    "End: $iUserEnd%0A".\
                    "Sale Amount: $iSaleAmt%0A".\
                    "Total Today: $iSalesToday%0A".\
                    "Total Month: $iSalesMonth%0A".\
                    "<<=====================>>")
    local iMessage [$eReplace ($iMessage) " " "%20"]
    do {/tool fetch url="https://api.telegram.org/bot$iTGBotToken/sendmessage?chat_id=$iTGrpChatID&text=$iMessage" keep-result=no} on-error={}
  }
  } on-error={log error "( $iUser ) ONLOGIN ERROR! Telegram Reporting Module"}

  # User Comment Info
  /ip hotspot user  set [find name=$iUser] comment="+ end=[$iUserEnd] beg=[$iUserBeg] mac=[$iDMac] validity=[$cValidity] vendo=[$iVendTag] amt=[$iSaleAmt]"
  /system scheduler set [find name=$iUser] comment="+ ( $interface ) beg=[$iUserBeg] end=[$iUserEnd] mac=[$iDMac] usertime=[$cUserTime] amt=[$iSaleAmt]"
}

Copy script below and paste to hotspot user profile onLogout.

# juanfi_hs_61f_onLogout_full
# by: Chloe Renae & Edmar Lozada
# ------------------------------

local iUser $username
local aUser [/ip hotspot user get $iUser]
local iUserTime ($aUser->"limit-uptime")
local iUsedTime ($aUser->"uptime")
local iValidity 0s

# Check Expiration
if ($cause="traffic limit reached" || ($iUserTime>0 && $iUserTime<=$iUsedTime)) do={
  local iDMac $"mac-address"
  local iDInt $interface
  local iHotSpot [/ip hotspot profile get [.. get [find interface=$iDInt] profile] html-directory]
  local iMacFile ""
  for i from=0 to=([len $iDMac]-1) do={
    local x [pick $iDMac $i]
    if ($x=":") do={set x ""}
    set iMacFile ($iMacFile.$x)
  }
  do {
  if ([/system scheduler find name=$iUser]!="") do={
    set iValidity [/system scheduler get [find name=$iUser] interval]
  } else={log error "( $iUser ) ONLOGOUT ERROR! /system scheduler [$iUser] => NOT FOUND!"}
  if ([len $iUserTime]=0) do={ set iUserTime "unli" }
  log debug "EXPIRE USER ( TimeLimit ) => user=[$iUser] mac=[$iDMac] usertime=[$iUserTime] uptime=[$iUsedTime] validity=[$iValidity]"
  /ip hotspot cookie remove [find user=$iUser]
  /ip hotspot cookie remove [find mac-address=$iDMac]
  /system scheduler  remove [find name=$iUser]
  /ip hotspot user   remove [find name=$iUser]
  /file remove [find name="$iHotSpot/data/$iMacFile.txt"]
  } on-error={log error "( $iUser ) ONLOGOUT ERROR! Expire User Module"}
}