This commit is contained in:
21
AllowIp/LICENSE
Normal file
21
AllowIp/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 fuzqing
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
106
AllowIp/Plugin.php
Normal file
106
AllowIp/Plugin.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||||
/**
|
||||
* 后台管理IP白名单
|
||||
*
|
||||
* @package AllowIp
|
||||
* @author Fuzqing
|
||||
* @version 1.0.1
|
||||
* @link https://huangweitong.com
|
||||
*/
|
||||
class AllowIp_Plugin implements Typecho_Plugin_Interface
|
||||
{
|
||||
/**
|
||||
* 插件版本号
|
||||
* @var string
|
||||
*/
|
||||
const _VERSION = '1.0.1';
|
||||
|
||||
/**
|
||||
* 激活插件方法,如果激活失败,直接抛出异常
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function activate()
|
||||
{
|
||||
Typecho_Plugin::factory('admin/common.php')->begin = array('AllowIp_Plugin', 'check');
|
||||
Typecho_Plugin::factory('Widget_Login')->loginSucceed = array('AllowIp_Plugin', 'check');
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用插件方法,如果禁用失败,直接抛出异常
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function deactivate(){}
|
||||
|
||||
/**
|
||||
* 获取插件配置面板
|
||||
*
|
||||
* @access public
|
||||
* @param Typecho_Widget_Helper_Form $form 配置面板
|
||||
* @return void
|
||||
*/
|
||||
public static function config(Typecho_Widget_Helper_Form $form)
|
||||
{
|
||||
/** 允许登陆后台的ip */
|
||||
$allow_ip = new Typecho_Widget_Helper_Form_Element_Text('allow_ip', NULL, NULL, _t('后台管理IP白名单'),'请输入ip地址,如果有多个请使用逗号隔开');
|
||||
$form->addInput($allow_ip);
|
||||
/** 跳转链接 */
|
||||
$location_url = new Typecho_Widget_Helper_Form_Element_Text('location_url', NULL, 'https://www.google.com/', _t('跳转链接'),'请输入标准的URL地址,IP白名单外的IP访问后台将会跳转至这个URL');
|
||||
$form->addInput($location_url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人用户的配置面板
|
||||
*
|
||||
* @access public
|
||||
* @param Typecho_Widget_Helper_Form $form
|
||||
* @return void
|
||||
*/
|
||||
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
|
||||
|
||||
/**
|
||||
* 检测ip白名单
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static function check()
|
||||
{
|
||||
static $realip = NULL;
|
||||
//判断服务器是否允许$_SERVER,不允许就使用getenv获取
|
||||
$realip = isset($_SERVER) ? $_SERVER['REMOTE_ADDR'] : getenv("REMOTE_ADDR");
|
||||
|
||||
if($realip !== NULL){
|
||||
$config = json_decode(json_encode(unserialize(Helper::options()->plugin('AllowIp'))));
|
||||
if(empty($config->allow_ip)) {
|
||||
$options = Typecho_Widget::widget('Widget_Options');
|
||||
$config_url = trim($options->siteUrl,'/').'/'.trim(__TYPECHO_ADMIN_DIR__,'/').'/options-plugin.php?config=AllowIp';
|
||||
echo '<span style="text-align: center;display: block;margin: auto;font-size: 1.5em;color:#1abc9c">您还没有设置后台管理IP白名单,<a href="'.$config_url.'">马上去设置</a></span>';
|
||||
} else {
|
||||
$allow_ip_arr = str_replace(',',',',$config->allow_ip);
|
||||
$allow_ip = explode(',', $allow_ip_arr);
|
||||
|
||||
//如果允许所有IP都通行的话,就打开下一行注释
|
||||
//$allow_ip[] = '0.0.0.0';
|
||||
|
||||
$location_url = trim($config->location_url) ? trim($config->location_url) : 'https://www.google.com/';
|
||||
if(!in_array('0.0.0.0', $allow_ip)) {
|
||||
if(!in_array($realip, $allow_ip)) {
|
||||
Typecho_Cookie::delete('__typecho_uid');
|
||||
Typecho_Cookie::delete('__typecho_authCode');
|
||||
@session_destroy();
|
||||
header('Location: '.$location_url);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
AllowIp/README.md
Normal file
19
AllowIp/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Typecho 后台IP白名单插件 AllowIp
|
||||
|
||||
## 插件简介
|
||||
|
||||
设置后台管理IP白名单,也就是说只允许IP白名单内的IP登陆访问后台。<br>
|
||||
至于为什么要写这个插件是因为博客服务器用的是国外的vps,搭建了酸酸乳,在网站挂上了ssr来访问速度都一样的。
|
||||
|
||||
## 安装方法
|
||||
|
||||
1. 到[releases](https://github.com/fuzqing/AllowIp-Typecho-Plugin/releases)中下载最新版本插件;
|
||||
2. 将下载的压缩包进行解压,文件夹重命名为`AllowIp`,上传至`Typecho`插件目录中;
|
||||
3. 后台激活插件,设置IP地址。
|
||||
|
||||
## 注意
|
||||
|
||||
如果你的IP设置不对,或者访问网站时候没有使用酸酸乳之类的代理,<br>
|
||||
请打开"Plugin.php"文件中这一行的注释<br>
|
||||
//$allow_ip[] = '0.0.0.0';<br>
|
||||
打开保存之后就可以登陆了,不过这样子这个IP白名单插件也就没意义了。
|
||||
Reference in New Issue
Block a user