You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GeekQu_sspanel/app/Command/Job.php

958 lines
45 KiB
PHTML

9 years ago
<?php
namespace App\Command;
use App\Models\Node;
use App\Models\User;
use App\Models\RadiusBan;
use App\Models\LoginIp;
use App\Models\Speedtest;
use App\Models\Shop;
use App\Models\Bought;
use App\Models\Ip;
use App\Models\NodeInfoLog;
use App\Models\NodeOnlineLog;
use App\Models\TrafficLog;
use App\Models\DetectLog;
use App\Models\BlockIp;
use App\Models\TelegramSession;
use App\Models\EmailVerify;
use App\Services\Config;
use App\Services\Password;
use App\Utils\DNSoverHTTPS;
9 years ago
use App\Utils\Radius;
use App\Utils\Tools;
use App\Services\Mail;
use App\Utils\QQWry;
use App\Utils\GA;
use App\Utils\Telegram;
use ArrayObject;
9 years ago
use CloudXNS\Api;
use App\Models\Disconnect;
use App\Models\UnblockIp;
use Exception;
use RuntimeException;
9 years ago
class Job
{
public static function syncnode()
{
$nodes = Node::all();
foreach ($nodes as $node) {
if (in_array($node->sort, array(0, 1, 10, 11, 12, 13))) {
$server_list = explode(';', $node->server);
if (!Tools::is_ip($server_list[0]) && $node->changeNodeIp($server_list[0])) {
$node->save();
}
if (in_array($node->sort, array(0, 10, 12))) {
Tools::updateRelayRuleIp($node);
}
}
9 years ago
}
}
8 years ago
public static function backup($full = false)
9 years ago
{
8 years ago
$to = Config::get('auto_backup_email');
if ($to == null) {
return false;
}
if (!mkdir('/tmp/ssmodbackup/') && !is_dir('/tmp/ssmodbackup/')) {
throw new RuntimeException(sprintf('Directory "%s" was not created', '/tmp/ssmodbackup/'));
}
9 years ago
$db_address_array = explode(':', Config::get('db_host'));
8 years ago
if ($full) {
system('mysqldump --user=' . Config::get('db_username') . ' --password=' . Config::get('db_password') . ' --host=' . $db_address_array[0] . ' ' . (isset($db_address_array[1]) ? '-P ' . $db_address_array[1] : '') . ' ' . Config::get('db_database') . ' > /tmp/ssmodbackup/mod.sql');
} else {
system('mysqldump --user=' . Config::get('db_username') . ' --password=' . Config::get('db_password') . ' --host=' . $db_address_array[0] . ' ' . (isset($db_address_array[1]) ? '-P ' . $db_address_array[1] : '') . ' ' . Config::get('db_database') . ' announcement auto blockip bought code coupon disconnect_ip link login_ip payback radius_ban shop speedtest ss_invite_code ss_node ss_password_reset ticket unblockip user user_token email_verify detect_list relay paylist> /tmp/ssmodbackup/mod.sql', $ret);
system('mysqldump --opt --user=' . Config::get('db_username') . ' --password=' . Config::get('db_password') . ' --host=' . $db_address_array[0] . ' ' . (isset($db_address_array[1]) ? '-P ' . $db_address_array[1] : '') . ' -d ' . Config::get('db_database') . ' alive_ip ss_node_info ss_node_online_log user_traffic_log detect_log telegram_session >> /tmp/ssmodbackup/mod.sql', $ret);
if (Config::get('enable_radius') == 'true') {
$db_address_array = explode(':', Config::get('radius_db_host'));
system('mysqldump --user=' . Config::get('radius_db_user') . ' --password=' . Config::get('radius_db_password') . ' --host=' . $db_address_array[0] . ' ' . (isset($db_address_array[1]) ? '-P ' . $db_address_array[1] : '') . '' . Config::get('radius_db_database') . '> /tmp/ssmodbackup/radius.sql', $ret);
}
}
system('cp ' . BASE_PATH . '/config/.config.php /tmp/ssmodbackup/configbak.php', $ret);
9 years ago
echo $ret;
system('zip -r /tmp/ssmodbackup.zip /tmp/ssmodbackup/* -P ' . Config::get('auto_backup_passwd'), $ret);
$subject = Config::get('appName') . '-备份成功';
$text = '您好,系统已经为您自动备份,请查看附件,用您设定的密码解压。';
9 years ago
try {
Mail::send($to, $subject, 'news/backup.tpl', [
'text' => $text
], ['/tmp/ssmodbackup.zip'
9 years ago
]);
} catch (Exception $e) {
9 years ago
echo $e->getMessage();
}
system('rm -rf /tmp/ssmodbackup', $ret);
system('rm /tmp/ssmodbackup.zip', $ret);
9 years ago
8 years ago
if (Config::get('backup_notify') == 'true') {
Telegram::Send('备份完毕了喵~今天又是安全祥和的一天呢。');
8 years ago
}
9 years ago
}
public static function UserGa()
{
$users = User::all();
foreach ($users as $user) {
$ga = new GA();
$secret = $ga->createSecret();
8 years ago
$user->ga_token = $secret;
9 years ago
$user->save();
}
echo 'ok';
9 years ago
}
public static function syncnasnode()
{
$nodes = Node::all();
foreach ($nodes as $node) {
8 years ago
$rule = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $node->server);
if (!$rule && (!$node->sort || $node->sort == 10 || $node->sort == 12 || $node->sort == 13)) {
8 years ago
$ip = gethostbyname($node->server);
if ($ip == "127.0.0.1"){
$ip = DNSoverHTTPS::gethostbyName($node->server);
}
8 years ago
$node->node_ip = $ip;
9 years ago
$node->save();
Radius::AddNas($node->node_ip, $node->server);
}
}
}
public static function DailyJob()
{
$nodes = Node::all();
foreach ($nodes as $node) {
8 years ago
if ($node->sort == 0 || $node->sort == 10 || $node->sort == 11 || $node->sort == 12 || $node->sort == 13) {
if (date('d') == $node->bandwidthlimit_resetday) {
8 years ago
$node->node_bandwidth = 0;
9 years ago
$node->save();
}
}
}
NodeInfoLog::where('log_time', '<', time() - 86400 * 3)->delete();
NodeOnlineLog::where('log_time', '<', time() - 86400 * 3)->delete();
TrafficLog::where('log_time', '<', time() - 86400 * 3)->delete();
DetectLog::where('datetime', '<', time() - 86400 * 3)->delete();
Speedtest::where('datetime', '<', time() - 86400 * 3)->delete();
EmailVerify::where('expire_in', '<', time() - 86400 * 3)->delete();
system('rm ' . BASE_PATH . '/storage/*.png', $ret);
Telegram::Send('姐姐姐姐,数据库被清理了,感觉身体被掏空了呢~');
9 years ago
//auto reset
8 years ago
$boughts = Bought::all();
$boughted_users = array();
9 years ago
foreach ($boughts as $bought) {
$user = User::where('id', $bought->userid)->first();
9 years ago
if ($user == null) {
$bought->delete();
continue;
}
$shop = Shop::where('id', $bought->shopid)->first();
9 years ago
if ($shop == null) {
$bought->delete();
continue;
}
8 years ago
if ($shop->reset() != 0 && $shop->reset_value() != 0 && $shop->reset_exp() != 0) {
$boughted_users[] = $bought->userid;
if ((time() - $shop->reset_exp() * 86400 < $bought->datetime) && (int)((time() - $bought->datetime) / 86400) % $shop->reset() == 0 && (int)((time() - $bought->datetime) / 86400) != 0) {
echo('流量重置-' . $user->id . "\n");
$user->transfer_enable = Tools::toGB($shop->reset_value());
$user->u = 0;
$user->d = 0;
$user->last_day_t = 0;
$user->save();
$subject = Config::get('appName') . '-您的流量被重置了';
$to = $user->email;
$text = '您好,根据您所订购的订单 ID:' . $bought->id . ',流量已经被重置为' . $shop->reset_value() . 'GB';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
8 years ago
}
9 years ago
}
}
}
$users = User::all();
foreach ($users as $user) {
8 years ago
$user->last_day_t = ($user->u + $user->d);
9 years ago
$user->save();
8 years ago
if (in_array($user->id, $boughted_users)) {
continue;
}
if (date('d') == $user->auto_reset_day) {
$user->u = 0;
$user->d = 0;
$user->last_day_t = 0;
8 years ago
$user->transfer_enable = $user->auto_reset_bandwidth * 1024 * 1024 * 1024;
$user->save();
$subject = Config::get('appName') . '-您的流量被重置了';
$to = $user->email;
$text = '您好,根据管理员的设置,流量已经被重置为' . $user->auto_reset_bandwidth . 'GB';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
9 years ago
}
}
}
#https://github.com/shuax/QQWryUpdate/blob/master/update.php
$copywrite = file_get_contents('http://update.cz88.net/ip/copywrite.rar');
9 years ago
$adminUser = User::where('is_admin', '=', '1')->get();
9 years ago
$newmd5 = md5($copywrite);
$oldmd5 = file_get_contents(BASE_PATH . '/storage/qqwry.md5');
9 years ago
if ($newmd5 != $oldmd5) {
file_put_contents(BASE_PATH . '/storage/qqwry.md5', $newmd5);
$qqwry = file_get_contents('http://update.cz88.net/ip/qqwry.rar');
if ($qqwry != '') {
$key = unpack('V6', $copywrite)[6];
8 years ago
for ($i = 0; $i < 0x200; $i++) {
9 years ago
$key *= 0x805;
8 years ago
$key++;
$key &= 0xFF;
9 years ago
$qqwry[$i] = chr(ord($qqwry[$i]) ^ $key);
}
$qqwry = gzuncompress($qqwry);
rename(BASE_PATH . '/storage/qqwry.dat', BASE_PATH . '/storage/qqwry.dat.bak');
$fp = fopen(BASE_PATH . '/storage/qqwry.dat', 'wb');
9 years ago
if ($fp) {
fwrite($fp, $qqwry);
fclose($fp);
}
}
}
$iplocation = new QQWry();
$location = $iplocation->getlocation('8.8.8.8');
9 years ago
$Userlocation = $location['country'];
if (iconv('gbk', 'utf-8//IGNORE', $Userlocation) != '美国') {
unlink(BASE_PATH . '/storage/qqwry.dat');
rename(BASE_PATH . '/storage/qqwry.dat.bak', BASE_PATH . '/storage/qqwry.dat');
9 years ago
}
self::updatedownload();
9 years ago
}
8 years ago
// 定时任务开启的情况下,每天自动检测有没有最新版的后端,github源来自Miku
8 years ago
public static function updatedownload()
{
system('cd ' . BASE_PATH . '/public/ssr-download/ && git pull https://github.com/xcxnig/ssr-download.git');
8 years ago
}
9 years ago
public static function CheckJob()
{
//在线人数检测
$users = User::where('node_connector', '>', 0)->get();
$full_alive_ips = Ip::where('datetime', '>=', time() - 60)->orderBy('ip')->get();
9 years ago
$alive_ipset = array();
foreach ($full_alive_ips as $full_alive_ip) {
$full_alive_ip->ip = Tools::getRealIp($full_alive_ip->ip);
$is_node = Node::where('node_ip', $full_alive_ip->ip)->first();
8 years ago
if ($is_node) {
9 years ago
continue;
}
if (!isset($alive_ipset[$full_alive_ip->userid])) {
$alive_ipset[$full_alive_ip->userid] = new ArrayObject();
9 years ago
}
$alive_ipset[$full_alive_ip->userid]->append($full_alive_ip);
}
foreach ($users as $user) {
$alive_ips = ($alive_ipset[$user->id] ?? new ArrayObject());
9 years ago
$ips = array();
$disconnected_ips = explode(',', $user->disconnect_ip);
9 years ago
foreach ($alive_ips as $alive_ip) {
if (!isset($ips[$alive_ip->ip]) && !in_array($alive_ip->ip, $disconnected_ips)) {
8 years ago
$ips[$alive_ip->ip] = 1;
9 years ago
if ($user->node_connector < count($ips)) {
//暂时封禁
$isDisconnect = Disconnect::where('id', '=', $alive_ip->ip)->where('userid', '=', $user->id)->first();
if ($isDisconnect == null) {
$disconnect = new Disconnect();
$disconnect->userid = $user->id;
$disconnect->ip = $alive_ip->ip;
$disconnect->datetime = time();
$disconnect->save();
if ($user->disconnect_ip == null || $user->disconnect_ip == '') {
9 years ago
$user->disconnect_ip = $alive_ip->ip;
} else {
$user->disconnect_ip .= ',' . $alive_ip->ip;
9 years ago
}
$user->save();
}
}
}
}
}
//解封
$disconnecteds = Disconnect::where('datetime', '<', time() - 300)->get();
9 years ago
foreach ($disconnecteds as $disconnected) {
$user = User::where('id', '=', $disconnected->userid)->first();
$ips = explode(',', $user->disconnect_ip);
$new_ips = '';
9 years ago
$first = 1;
foreach ($ips as $ip) {
if ($ip != $disconnected->ip && $ip != '') {
9 years ago
if ($first == 1) {
$new_ips .= $ip;
$first = 0;
} else {
$new_ips .= ',' . $ip;
9 years ago
}
}
}
$user->disconnect_ip = $new_ips;
if ($new_ips == '') {
9 years ago
$user->disconnect_ip = null;
}
$user->save();
$disconnected->delete();
}
//自动续费
$boughts = Bought::where('renew', '<', time())->where('renew', '<>', 0)->get();
9 years ago
foreach ($boughts as $bought) {
$user = User::where('id', $bought->userid)->first();
9 years ago
if ($user == null) {
$bought->delete();
continue;
}
$shop = Shop::where('id', $bought->shopid)->first();
8 years ago
if ($shop == null) {
$bought->delete();
$subject = Config::get('appName') . '-续费失败';
8 years ago
$to = $user->email;
$text = '您好,系统为您自动续费商品时,发现该商品已被下架,为能继续正常使用,建议您登录用户面板购买新的商品。';
8 years ago
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
8 years ago
], [
]);
} catch (Exception $e) {
8 years ago
echo $e->getMessage();
}
continue;
}
8 years ago
if ($user->money >= $shop->price) {
$user->money -= $shop->price;
9 years ago
$user->save();
$shop->buy($user, 1);
8 years ago
$bought->renew = 0;
9 years ago
$bought->save();
8 years ago
$bought_new = new Bought();
$bought_new->userid = $user->id;
$bought_new->shopid = $shop->id;
$bought_new->datetime = time();
$bought_new->renew = time() + $shop->auto_renew * 86400;
$bought_new->price = $shop->price;
$bought_new->coupon = '';
9 years ago
$bought_new->save();
$subject = Config::get('appName') . '-续费成功';
9 years ago
$to = $user->email;
$text = '您好,系统已经为您自动续费,商品名:' . $shop->name . ',金额:' . $shop->price . ' 元。';
9 years ago
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
9 years ago
], [
]);
} catch (Exception $e) {
9 years ago
echo $e->getMessage();
}
if (file_exists(BASE_PATH . '/storage/' . $bought->id . '.renew')) {
unlink(BASE_PATH . '/storage/' . $bought->id . '.renew');
9 years ago
}
} elseif (!file_exists(BASE_PATH . '/storage/' . $bought->id . '.renew')) {
$subject = Config::get('appName') . '-续费失败';
$to = $user->email;
$text = '您好,系统为您自动续费商品名:' . $shop->name . ',金额:' . $shop->price . ' 元 时,发现您余额不足,请及时充值。充值后请稍等系统便会自动为您续费。';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
9 years ago
}
$myfile = fopen(BASE_PATH . '/storage/' . $bought->id . '.renew', 'wb+') or die('Unable to open file!');
$txt = '1';
fwrite($myfile, $txt);
fclose($myfile);
9 years ago
}
}
Ip::where('datetime', '<', time() - 300)->delete();
UnblockIp::where('datetime', '<', time() - 300)->delete();
BlockIp::where('datetime', '<', time() - 86400)->delete();
TelegramSession::where('datetime', '<', time() - 900)->delete();
9 years ago
$adminUser = User::where('is_admin', '=', '1')->get();
8 years ago
//节点掉线检测
if (Config::get('enable_detect_offline') == 'true') {
$nodes = Node::all();
foreach ($nodes as $node) {
if ($node->isNodeOnline() === false && !file_exists(BASE_PATH . '/storage/' . $node->id . '.offline')) {
if (Config::get('useScFtqq') == 'true' && Config::get('enable_detect_offline_useScFtqq') == 'true') {
8 years ago
$ScFtqq_SCKEY = Config::get('ScFtqq_SCKEY');
$text = '管理员您好,系统发现节点 ' . $node->name . ' 掉线了,请您及时处理。';
8 years ago
$postdata = http_build_query(
array(
'text' => Config::get('appName') . '-节点掉线了',
'desp' => $text
)
);
8 years ago
$opts = array('http' =>
array(
8 years ago
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
));
$context = stream_context_create($opts);
file_get_contents('https://sc.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context);
}
8 years ago
9 years ago
foreach ($adminUser as $user) {
echo 'Send offline mail to user: ' . $user->id;
$subject = Config::get('appName') . '-系统警告';
9 years ago
$to = $user->email;
$text = '管理员您好,系统发现节点 ' . $node->name . ' 掉线了,请您及时处理。';
9 years ago
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
9 years ago
], [
]);
} catch (Exception $e) {
9 years ago
echo $e->getMessage();
}
8 years ago
if (Config::get('enable_cloudxns') == 'true' && ($node->sort == 0 || $node->sort == 10 || $node->sort == 12 || $node->sort == 13)) {
$api = new Api();
$api->setApiKey(Config::get('cloudxns_apikey'));//修改成自己API KEY
$api->setSecretKey(Config::get('cloudxns_apisecret'));//修改成自己的SECERET KEY
9 years ago
$api->setProtocol(true);
8 years ago
$domain_json = json_decode($api->domain->domainList());
9 years ago
foreach ($domain_json->data as $domain) {
8 years ago
if (strpos($domain->domain, Config::get('cloudxns_domain')) !== false) {
$domain_id = $domain->id;
9 years ago
}
}
8 years ago
$record_json = json_decode($api->record->recordList($domain_id, 0, 0, 2000));
9 years ago
foreach ($record_json->data as $record) {
if (($record->host . '.' . Config::get('cloudxns_domain')) == $node->server) {
8 years ago
$record_id = $record->record_id;
9 years ago
8 years ago
$Temp_node = Node::where('node_class', '<=', $node->node_class)->where(
static function ($query) use ($node) {
$query->where('node_group', '=', $node->node_group)
->orWhere('node_group', '=', 0);
9 years ago
}
)->whereRaw('UNIX_TIMESTAMP()-`node_heartbeat`<300')->first();
8 years ago
if ($Temp_node != null) {
9 years ago
$api->record->recordUpdate($domain_id, $record->host, $Temp_node->server, 'CNAME', 55, 60, 1, '', $record_id);
}
$notice_text = '喵喵喵~ ' . $node->name . ' 节点掉线了喵~域名解析被切换到了 ' . $Temp_node->name . ' 上了喵~';
9 years ago
}
}
} else {
$notice_text = '喵喵喵~ ' . $node->name . ' 节点掉线了喵~';
9 years ago
}
}
Telegram::Send($notice_text);
$myfile = fopen(BASE_PATH . '/storage/' . $node->id . '.offline', 'wb+') or die('Unable to open file!');
$txt = '1';
9 years ago
fwrite($myfile, $txt);
fclose($myfile);
} elseif ($node->isNodeOnline() === true && file_exists(BASE_PATH . '/storage/' . $node->id . '.offline')) {
if (Config::get('useScFtqq') == 'true' && Config::get('enable_detect_offline_useScFtqq') == 'true') {
8 years ago
$ScFtqq_SCKEY = Config::get('ScFtqq_SCKEY');
$text = '管理员您好,系统发现节点 ' . $node->name . ' 恢复上线了。';
8 years ago
$postdata = http_build_query(
array(
'text' => Config::get('appName') . '-节点掉线了',
'desp' => $text
)
);
8 years ago
$opts = array('http' =>
array(
8 years ago
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
));
$context = stream_context_create($opts);
file_get_contents('https://sc.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context);
}
8 years ago
foreach ($adminUser as $user) {
echo 'Send offline mail to user: ' . $user->id;
$subject = Config::get('appName') . '-系统提示';
8 years ago
$to = $user->email;
$text = '管理员您好,系统发现节点 ' . $node->name . ' 恢复上线了。';
8 years ago
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
8 years ago
], [
]);
} catch (Exception $e) {
8 years ago
echo $e->getMessage();
}
9 years ago
8 years ago
if (Config::get('enable_cloudxns') == 'true' && ($node->sort == 0 || $node->sort == 10 || $node->sort == 12 || $node->sort == 13)) {
$api = new Api();
$api->setApiKey(Config::get('cloudxns_apikey'));//修改成自己API KEY
$api->setSecretKey(Config::get('cloudxns_apisecret'));//修改成自己的SECERET KEY
8 years ago
$api->setProtocol(true);
$domain_json = json_decode($api->domain->domainList());
9 years ago
8 years ago
foreach ($domain_json->data as $domain) {
if (strpos($domain->domain, Config::get('cloudxns_domain')) !== false) {
$domain_id = $domain->id;
}
9 years ago
}
8 years ago
$record_json = json_decode($api->record->recordList($domain_id, 0, 0, 2000));
9 years ago
8 years ago
foreach ($record_json->data as $record) {
if (($record->host . '.' . Config::get('cloudxns_domain')) == $node->server) {
8 years ago
$record_id = $record->record_id;
9 years ago
8 years ago
$api->record->recordUpdate($domain_id, $record->host, $node->getNodeIp(), 'A', 55, 600, 1, '', $record_id);
}
9 years ago
}
8 years ago
$notice_text = '喵喵喵~ ' . $node->name . ' 节点恢复了喵~域名解析被切换回来了喵~';
9 years ago
} else {
$notice_text = '喵喵喵~ ' . $node->name . ' 节点恢复了喵~';
9 years ago
}
}
Telegram::Send($notice_text);
unlink(BASE_PATH . '/storage/' . $node->id . '.offline');
8 years ago
}
9 years ago
}
}
9 years ago
//登录地检测
if (Config::get('login_warn') == 'true') {
9 years ago
$iplocation = new QQWry();
$Logs = LoginIp::where('datetime', '>', time() - 60)->get();
9 years ago
foreach ($Logs as $log) {
$UserLogs = LoginIp::where('userid', '=', $log->userid)->orderBy('id', 'desc')->take(2)->get();
8 years ago
if ($UserLogs->count() == 2) {
9 years ago
$i = 0;
$Userlocation = '';
9 years ago
foreach ($UserLogs as $userlog) {
if ($i == 0) {
8 years ago
$location = $iplocation->getlocation($userlog->ip);
$ip = $userlog->ip;
9 years ago
$Userlocation = $location['country'];
$i++;
} else {
8 years ago
$location = $iplocation->getlocation($userlog->ip);
$nodes = Node::where('node_ip', 'LIKE', $ip . '%')->first();
$nodes2 = Node::where('node_ip', 'LIKE', $userlog->ip . '%')->first();
8 years ago
if ($Userlocation != $location['country'] && $nodes == null && $nodes2 == null) {
$user = User::where('id', '=', $userlog->userid)->first();
echo 'Send warn mail to user: ' . $user->id . '-' . iconv('gbk', 'utf-8//IGNORE', $Userlocation) . '-' . iconv('gbk', 'utf-8//IGNORE', $location['country']);
$subject = Config::get('appName') . '-系统警告';
9 years ago
$to = $user->email;
$text = '您好,系统发现您的账号在 ' . iconv('gbk', 'utf-8//IGNORE', $Userlocation) . ' 有异常登录,请您自己自行核实登录行为。有异常请及时修改密码。';
9 years ago
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
9 years ago
], [
]);
} catch (Exception $e) {
9 years ago
echo $e->getMessage();
}
}
}
}
}
}
}
$users = User::all();
foreach ($users as $user) {
if (($user->transfer_enable <= $user->u + $user->d || $user->enable == 0 || (strtotime($user->expire_in) < time() && strtotime($user->expire_in) > 644447105)) && RadiusBan::where('userid', $user->id)->first() == null) {
8 years ago
$rb = new RadiusBan();
$rb->userid = $user->id;
9 years ago
$rb->save();
Radius::Delete($user->email);
}
if (strtotime($user->expire_in) < time() && !file_exists(BASE_PATH . '/storage/' . $user->id . '.expire_in')) {
$user->transfer_enable = 0;
$user->u = 0;
$user->d = 0;
$user->last_day_t = 0;
8 years ago
$subject = Config::get('appName') . '-您的用户账户已经过期了';
$to = $user->email;
$text = '您好,系统发现您的账号已经过期了。';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
9 years ago
}
$myfile = fopen(BASE_PATH . '/storage/' . $user->id . '.expire_in', 'wb+') or die('Unable to open file!');
$txt = '1';
fwrite($myfile, $txt);
fclose($myfile);
} elseif (strtotime($user->expire_in) > time() && file_exists(BASE_PATH . '/storage/' . $user->id . '.expire_in')) {
unlink(BASE_PATH . '/storage/' . $user->id . '.expire_in');
9 years ago
}
9 years ago
8 years ago
//余量不足检测
if (!file_exists(BASE_PATH . '/storage/traffic_notified/') && !mkdir($concurrentDirectory = BASE_PATH . '/storage/traffic_notified/') && !is_dir($concurrentDirectory)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
8 years ago
}
if (Config::get('notify_limit_mode') != 'false') {
$user_traffic_left = $user->transfer_enable - $user->u - $user->d;
8 years ago
$under_limit = 'false';
if ($user->transfer_enable != 0) {
if (Config::get('notify_limit_mode') == 'per' &&
$user_traffic_left / $user->transfer_enable * 100 < Config::get('notify_limit_value')) {
$under_limit = 'true';
$unit_text = '%';
}
} elseif (Config::get('notify_limit_mode') == 'mb' &&
8 years ago
Tools::flowToMB($user_traffic_left) < Config::get('notify_limit_value')) {
$under_limit = 'true';
$unit_text = 'MB';
}
if ($under_limit == 'true' && !file_exists(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid')) {
$subject = Config::get('appName') . ' - 您的剩余流量过低';
$to = $user->email;
8 years ago
$text = '您好,系统发现您剩余流量已经低于 ' . Config::get('notify_limit_value') . $unit_text . ' 。';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
$myfile = fopen(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid', 'wb+') or die('Unable to open file!');
$txt = '1';
8 years ago
fwrite($myfile, $txt);
fclose($myfile);
} catch (Exception $e) {
echo $e->getMessage();
}
} elseif (($under_limit == 'false') && file_exists(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid')) {
unlink(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid');
}
}
8 years ago
if (Config::get('account_expire_delete_days') >= 0 &&
strtotime($user->expire_in) + Config::get('account_expire_delete_days') * 86400 < time()
) {
$subject = Config::get('appName') . '-您的用户账户已经被删除了';
$to = $user->email;
$text = '您好,系统发现您的账户已经过期 ' . Config::get('account_expire_delete_days') . ' 天了,帐号已经被删除。';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
9 years ago
}
8 years ago
$user->kill_user();
continue;
9 years ago
}
8 years ago
if (Config::get('auto_clean_uncheck_days') > 0 &&
max($user->last_check_in_time, strtotime($user->reg_date)) + (Config::get('auto_clean_uncheck_days') * 86400) < time() &&
$user->class == 0 &&
$user->money <= Config::get('auto_clean_min_money')
) {
$subject = Config::get('appName') . '-您的用户账户已经被删除了';
$to = $user->email;
$text = '您好,系统发现您的账号已经 ' . Config::get('auto_clean_uncheck_days') . ' 天没签到了,帐号已经被删除。';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
9 years ago
}
$user->kill_user();
continue;
9 years ago
}
8 years ago
if (Config::get('auto_clean_unused_days') > 0 &&
max($user->t, strtotime($user->reg_date)) + (Config::get('auto_clean_unused_days') * 86400) < time() &&
$user->class == 0 &&
$user->money <= Config::get('auto_clean_min_money')
) {
$subject = Config::get('appName') . '-您的用户账户已经被删除了';
$to = $user->email;
$text = '您好,系统发现您的账号已经 ' . Config::get('auto_clean_unused_days') . ' 天没使用了,帐号已经被删除。';
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
9 years ago
}
$user->kill_user();
continue;
9 years ago
}
8 years ago
if ($user->class != 0 &&
strtotime($user->class_expire) < time() &&
strtotime($user->class_expire) > 1420041600
) {
$text = '您好,系统发现您的账号等级已经过期了。';
$reset_traffic = Config::get('class_expire_reset_traffic');
if ($reset_traffic >= 0) {
$user->transfer_enable = Tools::toGB($reset_traffic);
$user->u = 0;
$user->d = 0;
$user->last_day_t = 0;
$text .= '流量已经被重置为' . $reset_traffic . 'GB';
}
$subject = Config::get('appName') . '-您的账户等级已经过期了';
8 years ago
$to = $user->email;
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
], [
]);
} catch (Exception $e) {
echo $e->getMessage();
9 years ago
}
8 years ago
$user->class = 0;
9 years ago
}
$user->save();
}
$rbusers = RadiusBan::all();
foreach ($rbusers as $sinuser) {
8 years ago
$user = User::find($sinuser->userid);
9 years ago
if ($user == null) {
$sinuser->delete();
continue;
}
8 years ago
if ($user->enable == 1 && (strtotime($user->expire_in) > time() || strtotime($user->expire_in) < 644447105) && $user->transfer_enable > $user->u + $user->d) {
9 years ago
$sinuser->delete();
Radius::Add($user, $user->passwd);
}
}
}
8 years ago
public static function detectGFW()
{
//节点被墙检测
$last_time = file_get_contents(BASE_PATH . '/storage/last_detect_gfw_time');
8 years ago
for ($count = 1; $count <= 12; $count++) {
if (time() - $last_time >= Config::get('detect_gfw_interval')) {
$file_interval = fopen(BASE_PATH . '/storage/last_detect_gfw_time', 'wb');
8 years ago
fwrite($file_interval, time());
fclose($file_interval);
$nodes = Node::all();
$adminUser = User::where('is_admin', '=', '1')->get();
8 years ago
foreach ($nodes as $node) {
if ($node->node_ip == '' ||
8 years ago
$node->node_ip == null ||
file_exists(BASE_PATH . '/storage/' . $node->id . 'offline') == true) {
8 years ago
continue;
}
$api_url = Config::get('detect_gfw_url');
$api_url = str_replace(array('{ip}', '{port}'), array($node->node_ip, Config::get('detect_gfw_port')), $api_url);
8 years ago
//因为考虑到有v2ray之类的节点,所以不得不使用ip作为参数
$result_tcping = false;
$detect_time = Config::get('detect_gfw_count');
8 years ago
for ($i = 1; $i <= $detect_time; $i++) {
$json_tcping = json_decode(file_get_contents($api_url), true);
if (eval('return ' . Config::get('detect_gfw_judge') . ';')) {
$result_tcping = true;
break;
}
}
if ($result_tcping == false) {
//被墙了
echo($node->id . ':false' . PHP_EOL);
8 years ago
//判断有没有发送过邮件
if (file_exists(BASE_PATH . '/storage/' . $node->id . '.gfw')) {
8 years ago
continue;
}
foreach ($adminUser as $user) {
echo 'Send gfw mail to user: ' . $user->id . '-';
$subject = Config::get('appName') . '-系统警告';
8 years ago
$to = $user->email;
$text = '管理员您好,系统发现节点 ' . $node->name . ' 被墙了,请您及时处理。';
8 years ago
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
8 years ago
], [
]);
} catch (Exception $e) {
8 years ago
echo $e->getMessage();
}
if (Config::get('enable_cloudxns') == 'true' && ($node->sort == 0 || $node->sort == 10 || $node->sort == 12 || $node->sort == 13)) {
$api = new Api();
$api->setApiKey(Config::get('cloudxns_apikey'));
8 years ago
//修改成自己API KEY
$api->setSecretKey(Config::get('cloudxns_apisecret'));
8 years ago
//修改成自己的SECERET KEY
$api->setProtocol(true);
$domain_json = json_decode($api->domain->domainList());
foreach ($domain_json->data as $domain) {
if (strpos($domain->domain, Config::get('cloudxns_domain')) !== false) {
$domain_id = $domain->id;
}
}
$record_json = json_decode($api->record->recordList($domain_id, 0, 0, 2000));
foreach ($record_json->data as $record) {
if (($record->host . '.' . Config::get('cloudxns_domain')) == $node->server) {
8 years ago
$record_id = $record->record_id;
$Temp_node = Node::where('node_class', '<=', $node->node_class)->where(
static function ($query) use ($node) {
$query->where('node_group', '=', $node->node_group)
->orWhere('node_group', '=', 0);
}
)->whereRaw('UNIX_TIMESTAMP()-`node_heartbeat`<300')->first();
8 years ago
if ($Temp_node != null) {
$api->record->recordUpdate($domain_id, $record->host, $Temp_node->server, 'CNAME', 55, 60, 1, '', $record_id);
}
$notice_text = '喵喵喵~ ' . $node->name . ' 节点被墙了喵~域名解析被切换到了 ' . $Temp_node->name . ' 上了喵~';
8 years ago
}
}
} else {
$notice_text = '喵喵喵~ ' . $node->name . ' 节点被墙了喵~';
8 years ago
}
}
Telegram::Send($notice_text);
$file_node = fopen(BASE_PATH . '/storage/' . $node->id . '.gfw', 'wb+');
8 years ago
fclose($file_node);
} else {
//没有被墙
echo($node->id . ':true' . PHP_EOL);
if (file_exists(BASE_PATH . '/storage/' . $node->id . '.gfw') == false) {
8 years ago
continue;
}
foreach ($adminUser as $user) {
echo 'Send gfw mail to user: ' . $user->id . '-';
$subject = Config::get('appName') . '-系统提示';
8 years ago
$to = $user->email;
$text = '管理员您好,系统发现节点 ' . $node->name . ' 溜出墙了。';
8 years ago
try {
Mail::send($to, $subject, 'news/warn.tpl', [
'user' => $user, 'text' => $text
8 years ago
], [
]);
} catch (Exception $e) {
8 years ago
echo $e->getMessage();
}
if (Config::get('enable_cloudxns') == 'true' && ($node->sort == 0 || $node->sort == 10 || $node->sort == 12 || $node->sort == 13)) {
$api = new Api();
$api->setApiKey(Config::get('cloudxns_apikey'));
8 years ago
//修改成自己API KEY
$api->setSecretKey(Config::get('cloudxns_apisecret'));
8 years ago
//修改成自己的SECERET KEY
$api->setProtocol(true);
$domain_json = json_decode($api->domain->domainList());
foreach ($domain_json->data as $domain) {
if (strpos($domain->domain, Config::get('cloudxns_domain')) !== false) {
$domain_id = $domain->id;
}
}
$record_json = json_decode($api->record->recordList($domain_id, 0, 0, 2000));
foreach ($record_json->data as $record) {
if (($record->host . '.' . Config::get('cloudxns_domain')) == $node->server) {
8 years ago
$record_id = $record->record_id;
$api->record->recordUpdate($domain_id, $record->host, $node->getNodeIp(), 'A', 55, 600, 1, '', $record_id);
}
}
$notice_text = '喵喵喵~ ' . $node->name . ' 节点恢复了喵~域名解析被切换回来了喵~';
8 years ago
} else {
$notice_text = '喵喵喵~ ' . $node->name . ' 节点恢复了喵~';
8 years ago
}
}
Telegram::Send($notice_text);
unlink(BASE_PATH . '/storage/' . $node->id . '.gfw');
8 years ago
}
}
break;
}
echo($node->id . 'interval skip' . PHP_EOL);
sleep(3);
8 years ago
}
}
9 years ago
}