Community discussions

 
zivtal
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Sun Feb 05, 2017 6:22 pm

hotspot manage by telegram

Thu Jan 10, 2019 12:37 am

Telegram: viewtopic.php?f=9&t=143828

put in ros script and call it "hotspot.cfg"
# set hotspot 'Server' name
# set hotspot 'Server Profile' login option(s)
# set user 'Profile' name
# set user 'Prefix'
# set 'Expired' days
# Limit upload/download bandwidth
# Set max login attempts
# Trial profile name
# Trial session time limit
# Trial upload/download bandwidth
# set notification telegram id (several ids should seperate by ','), <TG ID> with <MAC> means that the notification will send only when the mac address you wrote is connected to network.

:local config {
	"command"="hotspot";
	"server"="guests";
	"loginby"="mac,http-chap,mac-cookie";
	"profile"="generated";
	"prefix"="G-";
	"expired"=30d;
	"ratelimit"="5m/30m"
	"attempt"=5;
	"trial_userprofile"="trail";
	"trial_timelimit"=15m;
	"trial_ratelimit"="1m/5m";
	"telegram"="<TG ID>,<TG ID>=<MAC>";
}
return $config
put in ros script and call it "hotspot":
:local fconfig [:parse [/system script get "hotspot.add" source]]
:local cfg [$fconfig]
:local serverName ($cfg->"server")
:local userProfile ($cfg->"profile")
:local userPrefix ($cfg->"prefix")
:local notifiyTelegram ($cfg->"telegram")

#string generator
:local strGen do={
	:local random 0
	:local charSetNum 1
	:local p1
	:local var1
	:local var2
	:local var3
	:local strNew
	:local strLen [:tonum $1]
	:local charSet1 "23456789ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghijkmnopqrstuvwxyz23456789"
	:local charSet2 "cN47iKd2bLn8sQz4JAu2PD6Vm5RjTG4UrFY78XewHE3W9gMq62v7a9Z5yf5pC3k6xt9Bh8S3o"
	:local charSet3 "uYswW92z6M5fJynQp6hGm5VSr4oR8k7A2bKq5U3FiZvc8gHP2tdL9E4jBT3X7xC6N4D8e7a93"
	:for char from=1 to=$strLen step=1 do={
		:set var1 ([:pick [/system clock get time] 6 8])
		:set p1 ([:len [/system resource get uptime]])
		:set var2 ([:pick [/system resource get uptime] ($p1-2) $p1])
		:set var3 (($var1 * $var2) / 48)
		:if ($charSetNum=1) do={
			:set strNew ($strNew . [:pick $charSet1 $var3])
		}
		:if ($charSetNum=2) do={
			:set strNew ($strNew . [:pick $charSet2 $var3])
		}
		:if ($charSetNum=3) do={
			:set strNew ($strNew . [:pick $charSet3 $var3])
		}
		:set charSetNum ($charSetNum + 1)
		:if ($charSetNum = 4) do={
			:set charSetNum 1
		}
		:delay (($var1 + $var2 + $var3) / 12)
	}
	:return [:tostr $strNew]
}
#char replace
:local strReplace do={
	:local encode
	:for i from=0 to=([:len $string]-1) do={ 
		:local char [:pick $string $i]
		:if ($char=$find) do={
			:set char $replace
		}
		:set encode ($encode.$char)
	}
	:return $encode
}
#bytes auto convert to kb/mb/gb/tb
:local bytesConv do={
	:local bytes [:tonum $1]
	:local unit [:toarray "B,KB,MB,GB,TB"]
	:if ($bytes>=(1099511627776*1000)) do={
		:return ([:tostr ($bytes/1099511627776)].[:pick $unit 4])
	}
	:if ($bytes>=(1073741824*1000)) do={
		:return ([:tostr ($bytes/1073741824)].[:pick $unit 3])
	}
	:if ($bytes>=1048576) do={
		:return ([:tostr ($bytes/1048576)].[:pick $unit 2])
	}
	:if ($bytes>=1024) do={
		:return ([:tostr ($bytes/1024)].[:pick $unit 1])
	}
	:return ([:tostr $bytes].[:pick $unit 0])
}
#convert int to time
:local toTime do={
	:local time [:tonum $1]
	:local result
	:local days ($time/86400)
	:local hours (($time-$days*86400)/3600)
	:local minutes (($time-$days*86400-$hours*3600)/60)
	:local secounds ($time-$days*86400-$hours*3600-$minutes*60)
	:if ($secounds>0) do={
		:set result ($secounds."s")
	}
	:if ($minutes>0) do={
		:set result ($minutes."m ".$result)
	}
	:if ($hours>0) do={
		:set result ($hours."h ".$result)
	}
	:if ($days>0) do={
		:set result ($days."d ".$result)
	}
	:return $result
}
#read value from string
:local getKeyValue do={
	:if ([:type [:find $string $name]]!="nil") do={
		:local cut [:pick $string ([:find $string $name]+[:len $name]+1) [:len $string]]
		:if ([:len [:find $cut ";"]]>0) do={
			:return [:pick $cut 0 [:find $cut ";"]]
		} else={
			:return [:pick $cut 0 [:len $cut]]
		}
	} else={
		:if ([:len $default]>0) do={
			:return $default
		} else={
			:return ""
		}
	}
}
#set key and value to string
:local setKeyValue do={
	:if ([:typeof [:find $string $name]]!="nil") do={
		:local current
		:local cut [:pick $string ([:find $string $name]) [:len $string]]
		:if ([:len [:find $cut ";"]]>0) do={
			:set current [:pick $cut 0 [:find $cut ";"]]
		} else={
			:set current [:pick $cut 0 [:len $cut]]
		}
		:return ([:pick $string 0 [:find $string $current]]."$name=$value".[:pick $string ([:find $string $current]+[:len $current]) [:len $string]])
	} else={
		:if (([:len $value]>0) and ([:len $name]>0)) do={
			:if (([:len $string]=0) or ([:pick $string ([:len $string]-1) [:len $string]]=";")) do={
				:return ($string."$name=$value;")
			} else={
				:return ($string.";$name=$value;")
			}
		} else={
			:return ($string)
		}
	}
}
#del key and value to string
:local delKeyValue do={
	:if ([:typeof [:find $string $name]]!="nil") do={
		:local current
		:local cut [:pick $string ([:find $string $name]) [:len $string]]
		:if ([:len [:find $cut ";"]]>0) do={
			:set current [:pick $cut 0 [:find $cut ";"]]
		} else={
			:set current [:pick $cut 0 [:len $cut]]
		}
		:return ([:pick $string 0 [:find $string $current]].[:pick $string ([:find $string $current]+[:len $current]+1) [:len $string]])
	} else={
		:return ($string)
	}
}
#telegram functions
:local telegram
:local tgchatid
:if ([:len [/system script find name=telegram]]>0) do={
	:set telegram [:parse [/system script get telegram source]]
	:local getTelegramID do={
		:if ([:type [:find $input "="]]!="nil") do={
			:return [:pick $input 0 [:find $input "="]]
		} else={
			:return $input
		}
	}
	:local getTelegramMAC do={
		:if ([:type [:find $input "="]]!="nil") do={
			:return [:pick $input ([:find $input "="]+1) [:len $input]]
		} else={
			:return false
		}
	}
	:if ([:len $tgchatid]=0) do={
		:foreach tg in=[:toarray $notifiyTelegram] do={
			:if (([$getTelegramMAC input=$tg]=false) or ([:len [/ip dhcp-server lease find mac-address=[$getTelegramMAC input=$tg]]]>0)) do={
				:set tgchatid ([$getTelegramID input=$tg].",".$tgchatid)
			}
		}
	}
}
:local tgKeyValue do={
	:if (([:len $value]>0) and ($value!=false)) do={
		:return ("*$name:* $value %0A")
	} else={
		:return ("")
	}
}
#sms functions
:local sms
:if ([:len [/system script find name=sms]]>0) do={
	:set sms [:parse [/system script get sms source]]
}
:local txtKeyValue do={
	:if ([:len $value]>0) do={
		:put "$name: $value"
		:return ("$name: $value\n")
	} else={
		:return ("")
	}
}

:local action [:tostr $1]

:if ([:len $message]>0) do={
	$telegram send chat=$tgchatid text=$message
}

:if (($action="log") and ([:len $user]>0)) do={
	:local tgOut
	:global dhcpLeases
	:global dhcpHosts
	:global hsStatus
	:if ([:pick [/ip hotspot user get [find name=$user] name] 0 2]="T-") do={
		:if ($status="in") do={
			:set hsStatus [$setKeyValue string=[:tostr $hsStatus] name=[/ip hotspot user get [find name=$user] mac-address] value=[/ip dhcp-server lease get [find active-mac-address=[/ip hotspot user get [find name=$user] mac-address]] host-name]]
			:set tgOut ([$getKeyValue string=[:tostr $dhcpHosts] name=[/ip hotspot user get [find name=$user] mac-address]]." (".[[/ip hotspot user get [find name=$user] mac-address]].") has been *logged in* as *trial* user, session timeout in *".[/ip hotspot active get [find user=$user] session-time-left]."*")
		}
		:if ($status="out") do={
			:set tgOut ([$getKeyValue string=[:tostr $dhcpHosts] name=[/ip hotspot user get [find name=$user] mac-address]]." (".[[/ip hotspot user get [find name=$user] mac-address]].") has been online for *".[$toTime $uptime]."*, *".[$bytesConv $bytesin]."* has been received and *".[$bytesConv $bytesout]."* has been sent, time left in *".[/ip hotspot active get [find user=$user] session-time-left]."*")
		}
	} else={
		:if ([:len [$getKeyValue string=[/ip hotspot user get [find name=$user] comment] name="COMMENT"]]>0) do={
			:set tgOut ("*".[$getKeyValue string=[/ip hotspot user get [find name=$user] comment] name="COMMENT"]." (".[$getKeyValue string=[/ip hotspot user get [find name=$user] comment] name="APARTMENT" default=[/ip hotspot user get [find name=$user] name]].")*")
		} else={
			:if ([:len [$getKeyValue string=[/ip hotspot user get [find name=$user] comment] name="HOSTNAME"]]>0) do={
				:set tgOut ("*".[$getKeyValue string=[/ip hotspot user get [find name=$user] comment] name="HOSTNAME"]." (".[$getKeyValue string=[/ip hotspot user get [find name=$user] comment] name="APARTMENT" default=[/ip hotspot user get [find name=$user] name]].")*")
			} else={
				:set tgOut ([/ip hotspot user get [find name=$user] name])
			}
		}
		:if ($status="in") do={
			:set tgOut ($tgOut." has been *logged in* from '*".[:pick [/caps-man registration-table get [find mac-address=[/ip hotspot user get [find name=$user] mac-address]] interface] 0 [:find [/caps-man registration-table get [find mac-address=[/ip hotspot user get [find name=$user] mac-address]] interface] "-"]]."*' via '*".[/caps-man registration-table get [/caps-man registration-table find mac-address=[/ip hotspot user get [find name=$user] mac-address]] ssid]."*' (*".[/ip hotspot active get [find user=$user] address]."*)")
		}
		:if ($status="out") do={
			:set tgOut ($tgOut." has been online for *".[$toTime $uptime]."*, *".[$bytesConv $bytesin]."* has been received and *".[$bytesConv $bytesout]."* has been sent.")
		}
	}
	:if (($status="in") and ([$getKeyValue string=[:tostr $hsStatus] name=[/ip hotspot user get [find name=$user] mac-address]]!="online")) do={
		:set dhcpLeases [$delKeyValue string=[:tostr $dhcpLeases] name=[/ip hotspot user get [find name=$user] mac-address]]
		:set hsStatus [$setKeyValue string=[:tostr $hsStatus] name=[/ip hotspot user get [find name=$user] mac-address] value="online"]
		$telegram send chat=$tgchatid text=($tgOut)  markdown=true
	}
	:if (($status="out") and ([$getKeyValue string=[:tostr $hsStatus] name=[/ip hotspot user get [find name=$user] mac-address]]="online")) do={
		:set dhcpHosts [$delKeyValue string=[:tostr $dhcpHosts] name=[/ip hotspot user get [find name=$user] mac-address]]
		:set hsStatus [$delKeyValue string=[:tostr $hsStatus] name=[/ip hotspot user get [find name=$user] mac-address]]
		$telegram send chat=$tgchatid text=($tgOut)  markdown=true
		/ip hotspot user set comment=[$setKeyValue string=[/ip hotspot user get [find name=$user] comment] name="LAST" value=([/system clock get date]." ".[/system clock get time])] [find name=$user]
	}
	:log warning [$strReplace string=$tgOut find="*" replace=""]
}

:if ($action="create") do={
	:log warning "creating new hotspot account..."
	:local newUser
	:local newPass
	:if ([:len $mac]=0) do={
		:if ([:len $user]>0) do={
			:set newUser $user
		} else={
			:log info "generating username ..."
			:set newUser ($userPrefix.[$strGen 4])
			:while ([:len [/ip hotspot user find where name=$newUser]]>0) do={
				:log error "The user '$newUser' is already exists, retry to create another user."
				:set newUser ($userPrefix.[$strGen 4])
			}
		}
		:if ([:len $password]>0) do={
			:set newPass $password
		} else={
			:log info "generating password ..."
			:set newPass [$strGen 5]
		}
	} else={
		:set newUser $mac
		:set newPass $mac
	}
	/ip hotspot user add name=$newUser password=$newPass server=$serverName profile=$userProfile
	:log warning ("New hotspot account has been created ($newUser/$newPass)")

	:local tgOut ($header."%0A")
	:if ([:len $mac]=0) do={
		:set tgOut ($tgOut.[$tgKeyValue name=Username value=$newUser])
		:set tgOut ($tgOut.[$tgKeyValue name=Password value=$newPass])
	}
	:if (([:len $silent]=0) or ($silent=false)) do={
		$telegram send chat=$tgchatid text=$tgOut markdown=true
	}
}

:if ($action="recover") do={
	:local tgHeader ($header)
	:local tgOut
	:local userList [/ip hotspot user find where name~("$userPrefix") and server=$serverName and profile=$userProfile and name~$userPrefix and !mac-address~""]
	:if ([:len $user]>0) do={
		:set userList [/ip hotspot user find name~$user]
	}
	:if ([:len $email]>0) do={
		:set userList [/ip hotspot user find email=$email]
	}
	:if ([:len $phone]>0) do={
		:set userList [/ip hotspot user find comment~$phone]
	}
	:if ([:len $mac]>0) do={
		:set userList [/ip hotspot user find mac-address=$mac]
	}
	:foreach userIndex in=$userList do={
		:if ([:pick [/ip hotspot user get $userIndex name] 0 2]!="T-") do={
			:if (([/ip hotspot user get $userIndex disabled]=false) and ([:len [$getKeyValue string=[/ip hotspot user get $userIndex comment] name="PHONE"]]>0)) do={
				$sms send phone=("+972".[$getKeyValue string=[/ip hotspot user get $userIndex comment] name="PHONE"]) message=("$[/system identity get name]'s hotspot account:\n".[$txtKeyValue name="Username" value=[/ip hotspot user get $userIndex name]].[$txtKeyValue name="Password" value=[/ip hotspot user get $userIndex password]]."\n")
			}
			:if (([:len $mac]>0) and (([/ip hotspot user print count-only where mac-address=$mac]-[/ip hotspot user print count-only where mac-address=$mac name~"T-"])=1) and ([:len $tgHeader]>0)) do={
				:if ([:len [$getKeyValue string=[/ip hotspot user get $userIndex comment] name="COMMENT"]]>0) do={
					:set tgHeader ("$[$getKeyValue string=[/ip hotspot user get $userIndex comment] name="COMMENT"] ($[/ip hotspot user get $userIndex name]) $tgHeader")
				} else={
					:if ([:len [$getKeyValue string=[/ip hotspot user get $userIndex comment] name="HOSTNAME"]]>0) do={
						:set tgHeader ("$[$getKeyValue string=[/ip hotspot user get $userIndex comment] name="HOSTNAME"] ($[/ip hotspot user get $userIndex name]) $tgHeader")
					} else={
						:set tgHeader ("$[/ip hotspot user get $userIndex name] $tgHeader")
					}
				}
				:if ([:typeof [:find $tgHeader "disconnected"]]="nil") do={
					:set tgHeader ("$tgHeader, Password reminder: $[/ip hotspot user get $userIndex password]")
				}
			} else={
				:set tgOut ($tgOut.[$tgKeyValue name="Username" value=[/ip hotspot user get $userIndex name]])
				:if ([:typeof [:find $tgHeader "disconnected"]]="nil") do={
					:set tgOut ($tgOut.[$tgKeyValue name="Password" value=[/ip hotspot user get $userIndex password]])
				}
			}
			:set tgOut ($tgOut." %0A")
		}
	}
	:if ([:len $tgOut]>0) do={
		$telegram send chat=$tgchatid text=($tgHeader." %0A".$tgOut) markdown=true
	} else={
		$telegram send chat=$tgchatid text="hotspot accounts not found"
	}
}
put in ros script and call it "hotspot.add"
:local fconfig [:parse [/system script get "hotspot.cfg" source]]
:local cfg [$fconfig]
:local scriptName ($cfg->"command")
:local serverName ($cfg->"server")
:local serverLogin ($cfg->"loginby")
:local userProfile ($cfg->"profile")
:local userPrefix ($cfg->"prefix")
:local userExpired ($cfg->"expired")
:local userRatelimit ($cfg->"ratelimit")
:local loginAttempts ($cfg->"attempt")
:local trailUserProfile ($cfg->"trial_userprofile")
:local trailTimeLimit ($cfg->"trial_timelimit")
:local trailRateLimit ($cfg->"trial_ratelimit")

:local keyValue (":local getKeyValue do={
	:if ([:type [:find \$string \$name]]!=\"nil\") do={
		:local cut [:pick \$string ([:find \$string \$name]+[:len \$name]+1) [:len \$string]]
		:if ([:len [:find \$cut \";\"]]>0) do={
			:return [:pick \$cut 0 [:find \$cut \";\"]]
		} else={
			:return [:pick \$cut 0 [:len \$cut]]
		}
	} else={
		:if ([:len \$default]>0) do={
			:return \$default
		} else={
			:return \"\"
		}
	}
}
:local setKeyValue do={
	:if ([:typeof [:find \$string \$name]]!=\"nil\") do={
		:local current
		:local cut [:pick \$string ([:find \$string \$name]) [:len \$string]]
		:if ([:len [:find \$cut \";\"]]>0) do={
			:set current [:pick \$cut 0 [:find \$cut \";\"]]
		} else={
			:set current [:pick \$cut 0 [:len \$cut]]
		}
		:return ([:pick \$string 0 [:find \$string \$current]].\"\$name=\$value\".[:pick \$string ([:find \$string \$current]+[:len \$current]) [:len \$string]])
	} else={
		:if ([:len \$name]>0) do={
			:if (([:pick \$string ([:len \$string]-1) [:len \$string]]=\";\") or ([:len \$string]=0)) do={
				:return (\"\$string\$name=\$value;\")
			} else={
				:return (\"\$string;\$name=\$value;\")
			}
		} else={
			:return (\$string)
		}
	}
}
:local delKeyValue do={
	:if ([:typeof [:find \$string \$name]]!=\"nil\") do={
		:local current
		:local cut [:pick \$string ([:find \$string \$name]) [:len \$string]]
		:if ([:len [:find \$cut \";\"]]>0) do={
			:set current [:pick \$cut 0 [:find \$cut \";\"]]
		} else={
			:set current [:pick \$cut 0 [:len \$cut]]
		}
		:return ([:pick \$string 0 [:find \$string \$current]].[:pick \$string ([:find \$string \$current]+[:len \$current]+1) [:len \$string]])
	} else={
		:return (\$string)
	}
}")

:local leaseScript ("#lease script added by $scriptName
:global dhcpLeases
:global dhcpHosts
:local attempt $loginAttempts
:local $scriptName
:if ([:len [/system script find name=$scriptName]]>0) do={
	:set $scriptName [:parse [/system script get $scriptName source]]
}
$keyValue
:if (([:len [:find [/ip hotspot cookie find mac-address=\$leaseActMAC]]]=0) and ([:len [/ip hotspot user find name=\$leaseActMAC password=\$leaseActMAC]]=0)) do={
	:if ((\$leaseBound=1) and ([:type [:find \$dhcpHosts \$leaseActMAC]]=\"nil\")) do={
		:local leaseActHOST [/ip dhcp-server lease get [find active-mac-address=\$leaseActMAC] host-name]
		:local leaseActAP \$leaseServerName
		:if ([:len [/caps-man registration-table find mac-address=\$leaseActMAC]]>0) do={
			:set leaseActAP [:pick [/caps-man registration-table get [find mac-address=\$leaseActMAC] interface] 0 [:find [/caps-man registration-table get [find mac-address=\$leaseActMAC] interface] \"-\"]]
		}
		:set dhcpHosts [\$setKeyValue string=[:tostr \$dhcpHosts] name=\$leaseActMAC value=\$leaseActHOST]
		:if ([:tonum [\$getKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC default=0]]<\$attempt) do={
			:if ([:len [/ip hotspot user find where server=$serverName and profile=$userProfile and mac-address=\$leaseActMAC]]>0) do={
				:if ([:len [/ip hotspot user find where name=\$leaseActMAC password=\$leaseActMAC]]=0) do={
					\$$scriptName recover mac=\$leaseActMAC header=(\"has been connected to '\$leaseActAP' without signin\")
				}
			} else={
				:if ([:len [/ip hotspot user find where name~\"$userPrefix\" and server=$serverName and profile=$userProfile and !mac-address~\"\"]]=0) do={
					\$$scriptName create message=(\"*\$leaseActHOST* (\$leaseActMAC) has been connected (*\$[:tostr ([:tonum [\$getKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC default=0]]+1)]*) to '*\$leaseActAP*' without signup. There is no free hotspot account(s), please wait while generating new hotspot account...\") header=(\"Free hotspot account(s):\")
				} else={
					\$$scriptName recover header=(\"*\$leaseActHOST* (\$leaseActMAC) has been connected (*\$[:tostr ([:tonum [\$getKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC default=0]]+1)]*) to '*\$leaseActAP*' without signup, free hotspot account(s):\")
				}
			}
		} else={
			:log error (\"\$leaseActHOST (\$leaseActMAC) has been dropped from '\$leaseActAP', maximum login attempts reached.\")
			:set dhcpHosts [\$delKeyValue string=[:tostr \$dhcpHosts] name=\$leaseActMAC]
			/ip dhcp-server lease remove [find mac-address=\$leaseActMAC]
		}
	}
	:if (\$leaseBound=0) do={
		if ([:type [:find \$dhcpHosts \$leaseActMAC]]!=\"nil\") do={
			:if ([:len [/ip hotspot user find mac-address=\$leaseActMAC]]>0) do={
				:if (([:len [/ip hotspot user find where name=\$leaseActMAC password=\$leaseActMAC]]=0) and ([:len [:find [/ip hotspot cookie find mac-address=\$leaseActMAC]]]=0)) do={
					\$$scriptName recover mac=\$leaseActMAC header=(\"has been disconnected without signin\")
				}
			} else={
				:if ([:tonum [\$getKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC default=0]]<\$attempt) do={
					:set dhcpLeases [\$setKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC value=([:tonum [\$getKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC default=0]]+1)]
					\$$scriptName message=(\"*\$[\$getKeyValue string=[:tostr \$dhcpHosts] name=\$leaseActMAC]* (\$leaseActMAC) has been disconnected (*\$[:tonum [\$getKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC default=0]]*) without signup\")
				} else={
					:log warning (\"\$leaseActHOST (\$leaseActMAC) has been disconnected (\$[:tonum [\$getKeyValue string=[:tostr \$dhcpLeases] name=\$leaseActMAC default=0]]) without signup\")
				}
			}
			:set dhcpHosts [\$delKeyValue string=[:tostr \$dhcpHosts] name=\$leaseActMAC]
		}
		/ip hotspot active remove [find mac-address=\$leaseActMAC]
	}
}
")

:local userOnlogin ("#on-login script added by $scriptName
:local $scriptName
:if ([:len [/system script find name=$scriptName]]>0) do={
	:set $scriptName [:parse [/system script get $scriptName source]]
}
:local username \$user
:local userIndex [/ip hotspot user find where name=\$user]
:if ([/ip hotspot user get \$user mac-address]!=\$\"mac-address\") do={
	/ip hotspot user set mac-address=\$\"mac-address\" \$userIndex
	:if (\$user!=\$\"mac-address\") do={
		/ip hotspot user set name=\$\"mac-address\" password=\$\"mac-address\" comment=([get \$userIndex comment].\"HOSTNAME=\".[/ip dhcp-server lease get [find active-mac-address=[/ip hotspot user get \$userIndex mac-address]] host-name].\";\") \$userIndex
		:set username \$\"mac-address\"
	}
}
\$$scriptName log status=\"in\" user=\$username
")

:local userOnlogout ("#on-logout script added by $scriptName
:local $scriptName
:if ([:len [/system script find name=$scriptName]]>0) do={
	:set $scriptName [:parse [/system script get $scriptName source]]
}
\$$scriptName log status=\"out\" user=\$user bytesin=\$\"bytes-in\" bytesout=\$\"bytes-out\" uptime=\$\"uptime-secs\"
")

:local trailOnlogin ("#on-login script added by $scriptName
:local $scriptName
:if ([:len [/system script find name=$scriptName]]>0) do={
	:set $scriptName [:parse [/system script get $scriptName source]]
}
\$$scriptName log status=\"in\" user=\$user
")

:local trailOnlogout ("#on-logout script added by $scriptName
	:global dhcpHosts
	$keyValue
	:local $scriptName
	:if ([:len [/system script find name=$scriptName]]>0) do={
		:set $scriptName [:parse [/system script get $scriptName source]]
	}
	\$$scriptName log status=\"out\" user=\$user bytesin=\$\"bytes-in\" bytesout=\$\"bytes-out\" uptime=\$\"uptime-secs\"
	:set dhcpHosts [\$delKeyValue string=[:tostr \$dhcpHosts] name=\$\"mac-address\"]
	")

:if ([:len [/ip hotspot user profile find where name=$trailUserProfile]]=0) do={
	/ip hotspot user profile add name=$trailUserProfile on-login=$trailOnlogin on-logout=$trailOnlogout rate-limit=$trailRateLimit add-mac-cookie=no
} else={
	:if (([/ip hotspot user profile get [find where name=$trailUserProfile] on-login]!=$trailOnlogin) or ([/ip hotspot user profile get [find where name=$trailUserProfile] on-logout]!=$trailOnlogout)) do={
		/ip hotspot user profile set on-login=$trailOnlogin on-logout=$trailOnlogout [find name=$trailUserProfile]
	}
}

:if ([:len [/ip hotspot user profile find where name=$userProfile]]=0) do={
	/ip hotspot user profile add name=$userProfile mac-cookie-timeout=$userExpired shared-users=1 on-login=$userOnlogin on-logout=userOnlogout rate-limit=$userRatelimit
} else={
	:if (([/ip hotspot user profile get [find where name=$userProfile] on-login]!=$userOnlogin) or ([/ip hotspot user profile get [find where name=$userProfile] on-logout]!=$userOnlogout)) do={
		/ip hotspot user profile set on-login=$userOnlogin on-logout=$userOnlogout [find name=$userProfile]
	}
}

:if ([:len [/ip hotspot find name=$serverName]]=0) do={
	:error "hotspot server ('$serverName') not exists"
} else={
	:if ([/ip hotspot profile get [find name=[/ip hotspot get [find name=$serverName] profile]] login-by]!=[:toarray $serverLogin]) do={
		/ip hotspot profile set login-by=[:toarray $serverLogin] trial-uptime-limit=$trailTimeLimit trial-user-profile=$trailUserProfile [find name=[/ip hotspot get [find name=$serverName] profile]]
	}
}

:if (([:len [/ip dhcp-server find interface=[/ip hotspot get [find name=$serverName] interface]]]>0) and ([/ip dhcp-server get [find interface=[/ip hotspot get [find name=$serverName] interface]] lease-script]!=$leaseScript)) do={
	/ip dhcp-server set lease-script=$leaseScript [find interface=[/ip hotspot get [find name=$serverName] interface]]
}

:local config {
	:local fconfig [:parse [/system script get "hotspot.cfg" source]]
	:local cfg [$fconfig]
	"server"=($cfg->"server");
	"profile"=($cfg->"profile");
	"prefix"=($cfg->"prefix");
	"telegram"=($cfg->"telegram");
}
:return $config
CCR1009-8G-S+S+PC | RB962UiGS-5HacT2HnT | RBOmniTikPG-5HacD | RB750Gr3 | RBwAPG-5HacT2HnD

Who is online

Users browsing this forum: No registered users and 10 guests