Compare commits

...

26 Commits

Author SHA1 Message Date
HITSword 1980a74c07 修复stop后没删conf文件
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-17 21:52:10 +08:00
HITSword bef9a052d1 修复Nginx日志文件过多导致Too many open files错误
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-17 16:10:42 +08:00
HITSword e4877e246d 备份AMProxy的旧反向代理
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-12 19:30:10 +08:00
hitsword f81df14fde 判断方法写错
Signed-off-by: hitsword <admin@hitsword.com>
2020-01-09 21:34:58 +08:00
hitsword 7b630c9021 安装和卸载后应该是restart
Signed-off-by: hitsword <admin@hitsword.com>
2020-01-09 21:12:04 +08:00
hitsword bbc7996777 删除多余的baks目录
Signed-off-by: hitsword <admin@hitsword.com>
2020-01-09 21:07:02 +08:00
hitsword 1d3e40923b 优化日志和备份目录。卸载时直接删除日志目录
Signed-off-by: hitsword <admin@hitsword.com>
2020-01-09 21:06:23 +08:00
hitsword 36c875fde8 优化make的时候判断日志文件是否存在,不存在则创建
Signed-off-by: hitsword <admin@hitsword.com>
2020-01-09 20:39:09 +08:00
hitsword c8c430d070 优化配置格式和卸载插件时删除日志。
Signed-off-by: hitsword <admin@hitsword.com>
2020-01-09 20:30:49 +08:00
HITSword 25d60bda15 修复卸载插件不删除日志的问题。
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 19:33:10 +08:00
HITSword 97d6577e19 Merge branch 'master' of ssh://git.icn.me:16622/hitsword/AMH-HYProxy
# Conflicts:
#	hyproxy-cli.php
2020-01-09 19:18:53 +08:00
HITSword 43e4ef1453 修复mysql保留字段index问题导致无法写入
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 19:14:31 +08:00
HITSword 30b98aeae3 修复mysql保留字段index问题导致无法写入
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 19:13:07 +08:00
HITSword 50ab250f13 debug
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 18:56:05 +08:00
HITSword bce86e9cfa 增加还原功能
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 18:52:15 +08:00
HITSword c56f452345 卸载时自动备份所有反向代理
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 18:31:12 +08:00
HITSword ac3e0ca8c9 修复卸载时没清理日志的问题。
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 18:11:30 +08:00
HITSword 5a3b977f1b 格式代码
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 17:39:19 +08:00
HITSword 764b9c0aaa 修复文件名错误
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 17:30:21 +08:00
HITSword 0331af549d 优化代码。
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 17:21:19 +08:00
HITSword d130bedea6 开启独立日志.
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-09 15:54:51 +08:00
HITSword 9f50b69cc4 优化代码
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-08 18:30:35 +08:00
HITSword 4b5161ee97 自动删除冲突插件
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-08 16:55:20 +08:00
HITSword c2a1a43625 优化判断,减少报错。
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-08 16:20:32 +08:00
HITSword 2e8057646f 加强判断,减少报错
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-08 16:14:41 +08:00
HITSword 8913c6c55d 修复删除了ngx_cache_purge-2.3源码导致再次编译nginx时失败。
Signed-off-by: HITSword <admin@huayizhiyun.com>
2020-01-08 15:23:09 +08:00
14 changed files with 1484 additions and 1167 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
.idea

View File

@ -20,19 +20,36 @@ function amh_module_install()
if amh_module_status ; then if amh_module_status ; then
exit; exit;
else else
#Fix 'Too many open files' Error
if ! cat /etc/security/limits.conf | grep -q '* soft nofile 40960'; then
echo '* soft nofile 40960' >> /etc/security/limits.conf;
fi;
if ! cat /etc/security/limits.conf | grep -q '* hard nofile 40960'; then
echo '* hard nofile 40960' >> /etc/security/limits.conf;
fi;
#Delete other version #Delete other version
for module_name in `ls /root/amh/modules/ | grep 'HYProxy' | grep -v 'HYProxy-1.0'`; do for module_name in `ls /root/amh/modules/ | grep 'HYProxy' | grep -v 'HYProxy-1.0'`; do
#amh module $module_name uninstall force; #amh module $module_name uninstall force;
#amh module $module_name delete y; #amh module $module_name delete y;
rm -rf /root/amh/modules/$module_name; rm -rf /root/amh/modules/$module_name;
done; done;
#Delete conflict modules
if ls /root/amh/modules/ | grep -q 'limit_req2_nginx_module-1.2'; then
amh module limit_req2_nginx_module-1.2 uninstall force;
amh module limit_req2_nginx_module-1.2 delete y;
fi;
#install PDO_MYSQL module and set InnoDB_Engine On #install PDO_MYSQL module and set InnoDB_Engine On
if ! ls /root/amh/modules/ | grep -q 'PDO_MYSQL-1.0.2'; then
amh module download PDO_MYSQL-1.0.2; amh module download PDO_MYSQL-1.0.2;
fi;
if ! cat /etc/php.ini | grep -q 'pdo_mysql.so'; then
amh module PDO_MYSQL-1.0.2 install force; amh module PDO_MYSQL-1.0.2 install force;
fi;
amh SetParam mysql InnoDB_Engine On; amh SetParam mysql InnoDB_Engine On;
#install nginx module #install nginx module
Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`; Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`; nginx_configure='--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --with-ipv6';
[ -f /usr/local/nginx/sbin/nginx ] && nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`;
new_nginx_configure=$nginx_configure; new_nginx_configure=$nginx_configure;
if ! echo "$new_nginx_configure" | grep 'with-http_stub_status_module' > /dev/null; then if ! echo "$new_nginx_configure" | grep 'with-http_stub_status_module' > /dev/null; then
new_nginx_configure="${new_nginx_configure} --with-http_stub_status_module"; new_nginx_configure="${new_nginx_configure} --with-http_stub_status_module";
@ -61,8 +78,7 @@ function amh_module_install()
\cp -a ./objs/nginx /usr/local/nginx/sbin/; \cp -a ./objs/nginx /usr/local/nginx/sbin/;
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`; kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`;
cd /usr/local/; cd /usr/local/;
rm -rf tengine-2.3.2 tengine-2.3.2.tar.gz; rm -rf tengine-2.3.2 tengine-2.3.2.tar.gz ngx_cache_purge-2.3.tar.gz;
rm -rf ngx_cache_purge-2.3 ngx_cache_purge-2.3.tar.gz;
#rm -rf /usr/local/nginx/sbin/nginx-old; #rm -rf /usr/local/nginx/sbin/nginx-old;
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`; kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`;
if grep -q -e "Tengine.*<br />" /home/wwwroot/index/web/View/index.php; then if grep -q -e "Tengine.*<br />" /home/wwwroot/index/web/View/index.php; then
@ -85,7 +101,7 @@ function amh_module_install()
sed -i "/include vhost/a\ \tinclude hyproxy/*.conf;" /usr/local/nginx/conf/nginx.conf; sed -i "/include vhost/a\ \tinclude hyproxy/*.conf;" /usr/local/nginx/conf/nginx.conf;
mysql -uroot -p$MysqlPass < ./hyproxy.sql && touch ./InstallComplete && amh module HYProxy-1.0 admin cache,1:2:2,256m,10g,4h,1d; mysql -uroot -p$MysqlPass < ./hyproxy.sql && touch ./InstallComplete && amh module HYProxy-1.0 admin cache,1:2:2,256m,10g,4h,1d;
amh nginx reload; amh nginx restart;
amh_module_status; amh_module_status;
fi; fi;
} }
@ -238,7 +254,6 @@ function amh_module_admin()
elif [ "$action" == 'del' ]; then elif [ "$action" == 'del' ]; then
php ${module_dir}hyproxy-cli.php --action='del' --server_name=${domain} php ${module_dir}hyproxy-cli.php --action='del' --server_name=${domain}
rm -f "$domain_conf" && echo "[OK] successfully delete $domain HYProxy.";
amh nginx reload; amh nginx reload;
elif [ "$action" == 'start' ]; then elif [ "$action" == 'start' ]; then
php ${module_dir}hyproxy-cli.php --action='edit' --server_name=${domain} --field_name='status' --value='start'; php ${module_dir}hyproxy-cli.php --action='edit' --server_name=${domain} --field_name='status' --value='start';
@ -270,9 +285,13 @@ function amh_module_uninstall()
{ {
if amh_module_status ; then if amh_module_status ; then
#backup proxys
php ${module_dir}hyproxy-cli.php --action='backup';
#uninstall nginx modules #uninstall nginx modules
Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`; Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`; nginx_configure='--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --with-ipv6';
[ -f /usr/local/nginx/sbin/nginx ] && nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`;
new_nginx_configure=$nginx_configure; new_nginx_configure=$nginx_configure;
if echo "$new_nginx_configure" | grep 'add-module=/usr/local/ngx_cache_purge-2.3' > /dev/null; then if echo "$new_nginx_configure" | grep 'add-module=/usr/local/ngx_cache_purge-2.3' > /dev/null; then
new_nginx_configure=`echo $new_nginx_configure | sed "s|--add-module=/usr/local/ngx_cache_purge-2.3||"`; new_nginx_configure=`echo $new_nginx_configure | sed "s|--add-module=/usr/local/ngx_cache_purge-2.3||"`;
@ -298,14 +317,14 @@ function amh_module_uninstall()
MysqlPass=`cat /home/wwwroot/index/web/Amysql/Config.php | awk '{ FS="\047Password\047] = \047"; RS="\047;" } { print $2}' | sed '/^$/d'`; MysqlPass=`cat /home/wwwroot/index/web/Amysql/Config.php | awk '{ FS="\047Password\047] = \047"; RS="\047;" } { print $2}' | sed '/^$/d'`;
cd /usr/local/; cd /usr/local/;
rm -rf ngx_cache_purge-2.3 ngx_cache_purge-2.3.tar.gz; rm -rf ngx_cache_purge-2.3 ngx_cache_purge-2.3.tar.gz;
rm -rf /usr/local/nginx/conf/hyproxy /home/hyproxy_cache /home/hyproxy_cache_tmp; rm -rf /usr/local/nginx/conf/hyproxy /home/hyproxy_cache /home/hyproxy_cache_tmp /home/hyproxy_logs;
rm -f /home/wwwroot/index/web/Controller/hyproxy.php /home/wwwroot/index/web/Model/hyproxys.php /home/wwwroot/index/web/View/{hyproxy_category.php,hyproxy_list.php,hyproxy_cache.php,hyproxy_cache_index.php,hyproxy_cache_del.php}; rm -f /home/wwwroot/index/web/Controller/hyproxy.php /home/wwwroot/index/web/Model/hyproxys.php /home/wwwroot/index/web/View/{hyproxy_category.php,hyproxy_list.php,hyproxy_cache.php,hyproxy_cache_index.php,hyproxy_cache_del.php};
sed -i "/include hyproxy/d" /usr/local/nginx/conf/nginx.conf; sed -i "/include hyproxy/d" /usr/local/nginx/conf/nginx.conf;
sed -i "/proxy_/d" /usr/local/nginx/conf/nginx.conf; sed -i "/proxy_/d" /usr/local/nginx/conf/nginx.conf;
rm -f /root/amh/modules/HYProxy-1.0/InstallComplete; rm -f /root/amh/modules/HYProxy-1.0/InstallComplete;
mysql -uroot -p${MysqlPass} -B -N -e "DROP TABLE amh.module_hyproxy;DROP TABLE amh.module_hyproxy_cache" ; mysql -uroot -p${MysqlPass} -B -N -e "DROP TABLE amh.module_hyproxy;DROP TABLE amh.module_hyproxy_cache" ;
amh nginx reload; amh nginx restart;
echo '[OK] HYProxy Uninstall successful.'; echo '[OK] HYProxy Uninstall successful.';
else else
exit; exit;

View File

@ -1,24 +1,30 @@
<?php <?php
class hyproxy extends AmysqlController { class hyproxy extends AmysqlController
{
public $indexs = null; public $indexs = null;
public $hyproxys = null; public $hyproxys = null;
public $notice = null; public $notice = null;
// 载入数据模型(Model) // 载入数据模型(Model)
function AmysqlModelBase() { function AmysqlModelBase()
if($this -> indexs) return; {
if ($this -> indexs) {
return;
}
$this -> _class('Functions'); $this -> _class('Functions');
$this -> indexs = $this -> _model('indexs'); $this -> indexs = $this -> _model('indexs');
$this -> hyproxys = $this -> _model('hyproxys'); $this -> hyproxys = $this -> _model('hyproxys');
} }
function IndexAction(){ function IndexAction()
{
$this -> hyproxy_list(); $this -> hyproxy_list();
} }
// 反代列表 // 反代列表
function hyproxy_list(){ function hyproxy_list()
{
$this -> title = '反代列表 - HYProxy'; // 面板模块标题 $this -> title = '反代列表 - HYProxy'; // 面板模块标题
$this -> AmysqlModelBase(); $this -> AmysqlModelBase();
Functions::CheckLogin(); // 面板登录检查函数 Functions::CheckLogin(); // 面板登录检查函数
@ -92,37 +98,32 @@ class hyproxy extends AmysqlController {
// ********************************************************* // *********************************************************
// 缓存设置 // 缓存设置
function hyproxy_cache(){ function hyproxy_cache()
{
$this -> title = '缓存设置 - HYProxy - AMH'; $this -> title = '缓存设置 - HYProxy - AMH';
$this -> AmysqlModelBase(); $this -> AmysqlModelBase();
Functions::CheckLogin(); Functions::CheckLogin();
// 保存 // 保存
if (isset($_POST['save'])) if (isset($_POST['save'])) {
{
$this -> status = 'error'; $this -> status = 'error';
$field = array('levels', 'keys_zone', 'max_size', 'valid', 'inactive'); $field = array('levels', 'keys_zone', 'max_size', 'valid', 'inactive');
foreach ($field as $key=>$val) foreach ($field as $key => $val) {
{ if (!isset($_POST[$val]) || empty($_POST[$val])) {
if (!isset($_POST[$val]) || empty($_POST[$val]))
{
$error = true; $error = true;
break; break;
} }
} }
if (isset($error)) if (isset($error)) {
$this -> notice = '请填写完整数据。'; $this -> notice = '请填写完整数据。';
else } else {
{ if ($this -> hyproxys -> save_hyproxy_cache()) {
if ($this -> hyproxys -> save_hyproxy_cache())
{
$this -> status = 'success'; $this -> status = 'success';
$this -> notice = 'HYProxy 缓存设置成功。'; $this -> notice = 'HYProxy 缓存设置成功。';
} } else {
else
$this -> notice = 'HYProxy 缓存设置失败。'; $this -> notice = 'HYProxy 缓存设置失败。';
} }
}
} }
$this -> indexs -> log_insert($this -> notice); $this -> indexs -> log_insert($this -> notice);
@ -133,20 +134,17 @@ class hyproxy extends AmysqlController {
// ********************************************************* // *********************************************************
// 缓存索引 // 缓存索引
function hyproxy_cache_index(){ function hyproxy_cache_index()
{
$this -> title = '缓存索引 - HYProxy - AMH'; $this -> title = '缓存索引 - HYProxy - AMH';
$this -> AmysqlModelBase(); $this -> AmysqlModelBase();
Functions::CheckLogin(); Functions::CheckLogin();
if (isset($_POST['post_submit'])) if (isset($_POST['post_submit'])) {
{ if ($this -> hyproxys -> create_hyproxy_cache_index()) {
if ($this -> hyproxys -> create_hyproxy_cache_index())
{
$this -> status = 'success'; $this -> status = 'success';
$this -> notice = 'HYProxy 创建缓存索引成功。'; $this -> notice = 'HYProxy 创建缓存索引成功。';
} } else {
else
{
$this -> status = 'error'; $this -> status = 'error';
$this -> notice = 'HYProxy 创建缓存索引失败。'; $this -> notice = 'HYProxy 创建缓存索引失败。';
} }
@ -159,21 +157,18 @@ class hyproxy extends AmysqlController {
// ********************************************************* // *********************************************************
// 缓存删除 // 缓存删除
function hyproxy_cache_del(){ function hyproxy_cache_del()
{
$this -> title = '缓存删除 - HYProxy - AMH'; $this -> title = '缓存删除 - HYProxy - AMH';
$this -> AmysqlModelBase(); $this -> AmysqlModelBase();
Functions::CheckLogin(); Functions::CheckLogin();
// 删除缓存 // 删除缓存
if (isset($_POST['post_delete'])) if (isset($_POST['post_delete'])) {
{ if ($this -> hyproxys -> hyproxy_cache_delete()) {
if ($this -> hyproxys -> hyproxy_cache_delete())
{
$this -> status = 'success'; $this -> status = 'success';
$this -> notice = 'HYProxy 缓存删除成功。'; $this -> notice = 'HYProxy 缓存删除成功。';
} } else {
else
{
$this -> status = 'error'; $this -> status = 'error';
$this -> notice = 'HYProxy 缓存删除失败。'; $this -> notice = 'HYProxy 缓存删除失败。';
} }
@ -181,12 +176,12 @@ class hyproxy extends AmysqlController {
// 取得域名列表与缓存文件类型 // 取得域名列表与缓存文件类型
$hyproxy_cache_index = $this -> hyproxys -> get_hyproxy_cache_index(); $hyproxy_cache_index = $this -> hyproxys -> get_hyproxy_cache_index();
foreach ($hyproxy_cache_index as $key=>$val) foreach ($hyproxy_cache_index as $key => $val) {
{
$cache_host[] = $key; $cache_host[] = $key;
foreach ($val as $k=>$v) foreach ($val as $k => $v) {
$cache_type[$k] = $k; $cache_type[$k] = $k;
} }
}
// 缓存列表 // 缓存列表
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
@ -205,5 +200,4 @@ class hyproxy extends AmysqlController {
$this -> indexs -> log_insert($this -> notice); $this -> indexs -> log_insert($this -> notice);
$this -> _view('hyproxy_cache_del'); $this -> _view('hyproxy_cache_del');
} }
} }

View File

@ -1,7 +1,9 @@
<?php <?php
class hyproxys extends AmysqlModel{ class hyproxys extends AmysqlModel
function hyclear_result($str){ {
function hyclear_result($str)
{
$str = Functions::trim_result($str); $str = Functions::trim_result($str);
$str = str_replace(array('[OK] HYProxy is already installed.', '[OK] Nginx reload','[HYProxy-1.0 admin]'), '', $str); $str = str_replace(array('[OK] HYProxy is already installed.', '[OK] Nginx reload','[HYProxy-1.0 admin]'), '', $str);
$arr = explode("\n", $str); //分割为数组,每行为一个数组元素 $arr = explode("\n", $str); //分割为数组,每行为一个数组元素
@ -10,7 +12,9 @@ class hyproxys extends AmysqlModel{
} }
// 反代列表 // 反代列表
function hyproxy_list($args=''){//OK function hyproxy_list($args = '')
{
//OK
//TODO //TODO
//$server_name = $args['server_name'];域名搜索 //$server_name = $args['server_name'];域名搜索
//$status;//状态搜索start/stop //$status;//状态搜索start/stop
@ -19,12 +23,16 @@ class hyproxys extends AmysqlModel{
return $this -> _all($sql); return $this -> _all($sql);
} }
// 取得反代网站 // 取得反代网站
function hyproxy_get($name){//OK function hyproxy_get($name)
{
//OK
$sql = "SELECT * FROM module_hyproxy WHERE `server_name` = '$name'"; $sql = "SELECT * FROM module_hyproxy WHERE `server_name` = '$name'";
return $this -> _row($sql); return $this -> _row($sql);
} }
// 增加反代网站 // 增加反代网站
function hyproxy_add($server_name, $proxy_pass){//OK function hyproxy_add($server_name, $proxy_pass)
{
//OK
$server_name = trim($server_name); $server_name = trim($server_name);
$proxy_pass = trim($proxy_pass); $proxy_pass = trim($proxy_pass);
$proxy = $this->hyproxy_get($server_name); $proxy = $this->hyproxy_get($server_name);
@ -56,7 +64,9 @@ class hyproxys extends AmysqlModel{
} }
} }
// 编辑反代网站 // 编辑反代网站
function hyproxy_edit(){//OK function hyproxy_edit()
{
//OK
$server_name = trim($_POST['server_name']); $server_name = trim($_POST['server_name']);
if ($_POST['index']!=$_POST['index_hidden']) {//默认首页 if ($_POST['index']!=$_POST['index_hidden']) {//默认首页
$data['index'] = empty($_POST['index']) ? 'index.php index.html index.htm' : trim($_POST['index']); $data['index'] = empty($_POST['index']) ? 'index.php index.html index.htm' : trim($_POST['index']);
@ -85,7 +95,9 @@ class hyproxys extends AmysqlModel{
} }
} }
// 删除反代网站 // 删除反代网站
function hyproxy_del($del_name){//OK function hyproxy_del($del_name)
{
//OK
$del_name = trim($del_name); $del_name = trim($del_name);
$cmd = "amh module HYProxy-1.0 admin del,$del_name"; $cmd = "amh module HYProxy-1.0 admin del,$del_name";
$cmd = Functions::trim_cmd($cmd); $cmd = Functions::trim_cmd($cmd);
@ -97,7 +109,9 @@ class hyproxys extends AmysqlModel{
} }
} }
// 状态 // 状态
function hyproxy_run($run_name, $g){//OK function hyproxy_run($run_name, $g)
{
//OK
$run_zh = array( $run_zh = array(
'start' => '启动', 'start' => '启动',
'stop' => '停止' 'stop' => '停止'
@ -117,7 +131,9 @@ class hyproxys extends AmysqlModel{
} }
} }
// 缓存状态 // 缓存状态
function hyproxy_run_cache($run_name, $g){//OK function hyproxy_run_cache($run_name, $g)
{
//OK
$run_zh = array( $run_zh = array(
'1' => '启动', '1' => '启动',
'0' => '停止' '0' => '停止'
@ -139,7 +155,8 @@ class hyproxys extends AmysqlModel{
// ********************************************************* // *********************************************************
// 取得缓存 // 取得缓存
function get_hyproxy_cache(){ function get_hyproxy_cache()
{
$name = trim($name); $name = trim($name);
$cmd = "amh cat_nginx"; $cmd = "amh cat_nginx";
$cmd = Functions::trim_cmd($cmd); $cmd = Functions::trim_cmd($cmd);
@ -152,32 +169,33 @@ class hyproxys extends AmysqlModel{
$data['inactive'] = $arr[3]; $data['inactive'] = $arr[3];
$data['max_size'] = $arr[4]; $data['max_size'] = $arr[4];
$data['valid'] = $arr2[1]; $data['valid'] = $arr2[1];
foreach ($data as $key=>$val) foreach ($data as $key => $val) {
$data[$key] = trim($val); $data[$key] = trim($val);
}
return $data; return $data;
} }
// 保存缓存 // 保存缓存
function save_hyproxy_cache(){ function save_hyproxy_cache()
{
$run_name = trim($run_name); $run_name = trim($run_name);
$cmd = "amh module HYProxy-1.0 admin cache,{$_POST['levels']},{$_POST['keys_zone']},{$_POST['max_size']},{$_POST['valid']},{$_POST['inactive']}"; $cmd = "amh module HYProxy-1.0 admin cache,{$_POST['levels']},{$_POST['keys_zone']},{$_POST['max_size']},{$_POST['valid']},{$_POST['inactive']}";
$cmd = Functions::trim_cmd($cmd); $cmd = Functions::trim_cmd($cmd);
exec($cmd, $tmp, $status); exec($cmd, $tmp, $status);
Return !$status; return !$status;
} }
// ********************************************************* // *********************************************************
// 取得缓存索引 // 取得缓存索引
function get_hyproxy_cache_index(){ function get_hyproxy_cache_index()
{
// $sql = "SELECT count(*) FROM module_hyproxy GROUP BY hyproxy_type "; // $sql = "SELECT count(*) FROM module_hyproxy GROUP BY hyproxy_type ";
$sql = "SELECT * FROM module_hyproxy_cache"; $sql = "SELECT * FROM module_hyproxy_cache";
$result = $this -> _query($sql); $result = $this -> _query($sql);
while ($rs = mysql_fetch_assoc($result)) while ($rs = mysql_fetch_assoc($result)) {
{
$url_info = parse_url($rs['hyproxy_key']); $url_info = parse_url($rs['hyproxy_key']);
if (isset($url_info['host']) && !empty($url_info['host'])) if (isset($url_info['host']) && !empty($url_info['host'])) {
{
++$data[$url_info['host']]['all']['sum']; ++$data[$url_info['host']]['all']['sum'];
$data[$url_info['host']]['all']['size'] += $rs['hyproxy_size']; $data[$url_info['host']]['all']['size'] += $rs['hyproxy_size'];
@ -185,11 +203,12 @@ class hyproxys extends AmysqlModel{
$data[$url_info['host']][$rs['hyproxy_type']]['size'] += $rs['hyproxy_size']; $data[$url_info['host']][$rs['hyproxy_type']]['size'] += $rs['hyproxy_size'];
} }
} }
Return $data; return $data;
} }
// 创建缓存索引 // 创建缓存索引
function create_hyproxy_cache_index(){ function create_hyproxy_cache_index()
{
$val = (int)$_POST['cache_index_time_val']; $val = (int)$_POST['cache_index_time_val'];
$type = array('1' => 60*24, '2' => 60, '3' => 1); $type = array('1' => 60*24, '2' => 60, '3' => 1);
$area = array('1' => '-', '2' => '+'); $area = array('1' => '-', '2' => '+');
@ -200,29 +219,30 @@ class hyproxys extends AmysqlModel{
$cmd = "amh module HYProxy-1.0 admin cache-index,$val,$mode"; $cmd = "amh module HYProxy-1.0 admin cache-index,$val,$mode";
$cmd = Functions::trim_cmd($cmd); $cmd = Functions::trim_cmd($cmd);
exec($cmd, $tmp, $status); exec($cmd, $tmp, $status);
Return !$status; return !$status;
} }
// ********************************************************* // *********************************************************
// 取得缓存列表 // 取得缓存列表
function get_hyproxy_cache_list($page = 1, $page_sum = 20){ function get_hyproxy_cache_list($page = 1, $page_sum = 20)
{
$where = ''; $where = '';
if (isset($_GET['cache_key']) && !empty($_GET['cache_key'])) if (isset($_GET['cache_key']) && !empty($_GET['cache_key'])) {
{
$_GET['cache_key'] = trim($_GET['cache_key']); $_GET['cache_key'] = trim($_GET['cache_key']);
$where .= " AND hyproxy_key LIKE '{$_GET['cache_key']}'"; $where .= " AND hyproxy_key LIKE '{$_GET['cache_key']}'";
} }
if (isset($_GET['cache_type']) && $_GET['cache_type'] != 'all') if (isset($_GET['cache_type']) && $_GET['cache_type'] != 'all') {
$where .= " AND hyproxy_type LIKE '{$_GET['cache_type']}'"; $where .= " AND hyproxy_type LIKE '{$_GET['cache_type']}'";
}
$limit = ' LIMIT ' . ($page-1)*$page_sum . ' , ' . $page_sum; $limit = ' LIMIT ' . ($page-1)*$page_sum . ' , ' . $page_sum;
$sql = "SELECT * FROM module_hyproxy_cache WHERE 1 $where"; $sql = "SELECT * FROM module_hyproxy_cache WHERE 1 $where";
$sum = $this -> _sum($sql); $sum = $this -> _sum($sql);
$sql = "SELECT * FROM module_hyproxy_cache WHERE 1 $where $limit"; $sql = "SELECT * FROM module_hyproxy_cache WHERE 1 $where $limit";
Return array('data' => $this -> _all($sql), 'sum' => $sum); return array('data' => $this -> _all($sql), 'sum' => $sum);
} }
// 删除缓存 // 删除缓存
@ -233,6 +253,6 @@ class hyproxys extends AmysqlModel{
$cmd = "amh module HYProxy-1.0 admin cache-delete,$url_param,$file_type"; $cmd = "amh module HYProxy-1.0 admin cache-delete,$url_param,$file_type";
$cmd = Functions::trim_cmd($cmd); $cmd = Functions::trim_cmd($cmd);
exec($cmd, $tmp, $status); exec($cmd, $tmp, $status);
Return !$status; return !$status;
} }
} }

View File

@ -1,12 +1,20 @@
#!/bin/bash #!/bin/bash
Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`; Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`; nginx_configure='--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --with-ipv6';
[ -f /usr/local/nginx/sbin/nginx ] && nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`;
new_nginx_configure=$nginx_configure; new_nginx_configure=$nginx_configure;
if ! echo "$new_nginx_configure" | grep 'with-http_stub_status_module' > /dev/null; then
new_nginx_configure="${new_nginx_configure} --with-http_stub_status_module";
fi;
if ! echo "$new_nginx_configure" | grep 'with-stream' > /dev/null; then
new_nginx_configure="${new_nginx_configure} --with-stream";
fi;
if echo "$new_nginx_configure" | grep 'ngx_http_upstream_check_module' > /dev/null; then if echo "$new_nginx_configure" | grep 'ngx_http_upstream_check_module' > /dev/null; then
new_nginx_configure=`echo $new_nginx_configure | sed "s| --add-module=modules/ngx_http_upstream_check_module||"`; new_nginx_configure=`echo $new_nginx_configure | sed "s| --add-module=modules/ngx_http_upstream_check_module||"`;
fi; fi;
nginx_version=`/usr/local/nginx/sbin/nginx -v 2> /tmp/nginx_version && cat /tmp/nginx_version && rm -f /tmp/nginx_version`; nginx_version='';
[ -f /usr/local/nginx/sbin/nginx ] && nginx_version=`/usr/local/nginx/sbin/nginx -v 2> /tmp/nginx_version && cat /tmp/nginx_version && rm -f /tmp/nginx_version`;
echo $nginx_version; echo $nginx_version;
if [ "$nginx_version" != "nginx version: nginx/1.14.0" ]; then if [ "$nginx_version" != "nginx version: nginx/1.14.0" ]; then
@ -22,7 +30,7 @@ if [ "$nginx_version" != "nginx version: nginx/1.14.0" ]; then
cd /usr/local/; cd /usr/local/;
rm -rf nginx-1.14.0 nginx-1.14.0.tar.gz; rm -rf nginx-1.14.0 nginx-1.14.0.tar.gz;
if grep -q -e "Tengine.*<br />" /home/wwwroot/index/web/View/index.php; then if grep -q -e "Tengine.*<br />" /home/wwwroot/index/web/View/index.php; then
sed -i "s|Nginx.*<br />|Nginx 1.14.0 <br />|" /home/wwwroot/index/web/View/index.php; sed -i "s|Tengine.*<br />|Nginx 1.14.0 <br />|" /home/wwwroot/index/web/View/index.php;
fi fi
if grep -q -e "Nginx.*<br />" /home/wwwroot/index/web/View/index.php; then if grep -q -e "Nginx.*<br />" /home/wwwroot/index/web/View/index.php; then
sed -i "s|Nginx.*<br />|Nginx 1.14.0 <br />|" /home/wwwroot/index/web/View/index.php; sed -i "s|Nginx.*<br />|Nginx 1.14.0 <br />|" /home/wwwroot/index/web/View/index.php;
@ -30,4 +38,4 @@ if [ "$nginx_version" != "nginx version: nginx/1.14.0" ]; then
fi; fi;
nginx -v nginx -v
echo 'AMH Nginx Update Done' echo 'AMH Nginx Update successful'

View File

@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`; Cpunum=`cat /proc/cpuinfo |grep 'processor'|wc -l`;
nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`; nginx_configure='--prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --with-ipv6';
[ -f /usr/local/nginx/sbin/nginx ] && nginx_configure=`/usr/local/nginx/sbin/nginx -V 2> /tmp/nginx_configure && cat /tmp/nginx_configure | grep 'configure arguments' | cut -d: -f2 && rm -f /tmp/nginx_configure`;
new_nginx_configure=$nginx_configure; new_nginx_configure=$nginx_configure;
if ! echo "$new_nginx_configure" | grep 'with-http_stub_status_module' > /dev/null; then if ! echo "$new_nginx_configure" | grep 'with-http_stub_status_module' > /dev/null; then
new_nginx_configure="${new_nginx_configure} --with-http_stub_status_module"; new_nginx_configure="${new_nginx_configure} --with-http_stub_status_module";
@ -12,7 +13,8 @@ if ! echo "$new_nginx_configure" | grep 'ngx_http_upstream_check_module' > /dev/
new_nginx_configure="${new_nginx_configure} --add-module=modules/ngx_http_upstream_check_module"; new_nginx_configure="${new_nginx_configure} --add-module=modules/ngx_http_upstream_check_module";
fi; fi;
nginx_version=`/usr/local/nginx/sbin/nginx -v 2> /tmp/nginx_version && cat /tmp/nginx_version | grep 'Tengine' && rm -f /tmp/nginx_version`; nginx_version='';
[ -f /usr/local/nginx/sbin/nginx ] && nginx_version=`/usr/local/nginx/sbin/nginx -v 2> /tmp/nginx_version && cat /tmp/nginx_version && rm -f /tmp/nginx_version`;
echo $nginx_version; echo $nginx_version;
if [ "$nginx_version" != "Tengine version: Tengine/2.3.2" ]; then if [ "$nginx_version" != "Tengine version: Tengine/2.3.2" ]; then
@ -36,4 +38,4 @@ if [ "$nginx_version" != "Tengine version: Tengine/2.3.2" ]; then
fi; fi;
nginx -v nginx -v
echo 'AMH Tengine Update Done' echo 'AMH Tengine Update successful'

View File

@ -3,7 +3,9 @@
<?php include('hyproxy_category.php'); ?> <?php include('hyproxy_category.php'); ?>
<?php <?php
if (!empty($notice)) echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>'; if (!empty($notice)) {
echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>';
}
?> ?>
<p>反代缓存设置:</p> <p>反代缓存设置:</p>
<form action="./index.php?c=hyproxy&a=hyproxy_cache" method="POST" > <form action="./index.php?c=hyproxy&a=hyproxy_cache" method="POST" >

View File

@ -42,7 +42,9 @@ var cache_delete_submit = function ()
<?php include('hyproxy_category.php'); ?> <?php include('hyproxy_category.php'); ?>
<?php <?php
if (!empty($notice)) echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>'; if (!empty($notice)) {
echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>';
}
?> ?>
<div> <div>
@ -51,10 +53,8 @@ var cache_delete_submit = function ()
<input type="hidden" value="hyproxy_cache_del" name="a"/> <input type="hidden" value="hyproxy_cache_del" name="a"/>
<input type="hidden" value="hyproxy" name="c"/> <input type="hidden" value="hyproxy" name="c"/>
索引域名 <select name="cache_host" id="cache_host" style="width:180px;"> 索引域名 <select name="cache_host" id="cache_host" style="width:180px;">
<?php if (is_array($cache_host)) <?php if (is_array($cache_host)) {
{ foreach ($cache_host as $key => $val) {
foreach ($cache_host as $key=>$val)
{
?> ?>
<option value="<?php echo $val;?>"><?php echo $val;?></option> <option value="<?php echo $val;?>"><?php echo $val;?></option>
<?php <?php
@ -66,10 +66,8 @@ var cache_delete_submit = function ()
<script>G('cache_host').value = '<?php echo isset($_GET['cache_host']) ? $_GET['cache_host'] : '';?>';</script> <script>G('cache_host').value = '<?php echo isset($_GET['cache_host']) ? $_GET['cache_host'] : '';?>';</script>
网址匹配 (可使用%通配符) <input type="text" name="cache_key" id="cache_key" class="input_text" value="<?php echo isset($_GET['cache_key']) ? $_GET['cache_key'] : '';?>" style="width:250px;"/> &nbsp; 网址匹配 (可使用%通配符) <input type="text" name="cache_key" id="cache_key" class="input_text" value="<?php echo isset($_GET['cache_key']) ? $_GET['cache_key'] : '';?>" style="width:250px;"/> &nbsp;
文件类型 <select name="cache_type" id="cache_type" style="width:180px;"> 文件类型 <select name="cache_type" id="cache_type" style="width:180px;">
<?php if (is_array($cache_type)) <?php if (is_array($cache_type)) {
{ foreach ($cache_type as $key => $val) {
foreach ($cache_type as $key=>$val)
{
if ($val != 'all') { if ($val != 'all') {
?> ?>
<option value="<?php echo $val;?>"><?php echo $val;?></option> <option value="<?php echo $val;?>"><?php echo $val;?></option>
@ -103,17 +101,13 @@ var cache_delete_submit = function ()
<th>创建时间</th> <th>创建时间</th>
</tr> </tr>
<?php <?php
if(!is_array($hyproxy_cache_list['data']) || count($hyproxy_cache_list['data']) < 1) if (!is_array($hyproxy_cache_list['data']) || count($hyproxy_cache_list['data']) < 1) {
{
?> ?>
<tr><td colspan="7" style="padding:10px;text-align:center;">没找到缓存索引数据。</td></tr> <tr><td colspan="7" style="padding:10px;text-align:center;">没找到缓存索引数据。</td></tr>
<?php <?php
} } else {
else
{
$k = 0; $k = 0;
foreach ($hyproxy_cache_list['data'] as $key=>$val) foreach ($hyproxy_cache_list['data'] as $key => $val) {
{
?> ?>
<tr> <tr>
<th class="i"><?php echo $val['hyproxy_id'];?></th> <th class="i"><?php echo $val['hyproxy_id'];?></th>

View File

@ -38,7 +38,9 @@ var cache_index_submit = function ()
<?php include('hyproxy_category.php'); ?> <?php include('hyproxy_category.php'); ?>
<?php <?php
if (!empty($notice)) echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>'; if (!empty($notice)) {
echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>';
}
?> ?>
<p>反代缓存索引:</p> <p>反代缓存索引:</p>
<table border="0" cellspacing="1" id="STable" style="width:720px;"> <table border="0" cellspacing="1" id="STable" style="width:720px;">
@ -52,8 +54,7 @@ var cache_index_submit = function ()
$host_sum = 0; $host_sum = 0;
$file_sum = 0; $file_sum = 0;
$file_size = 0; $file_size = 0;
foreach ($hyproxy_cache_index as $key=>$val) foreach ($hyproxy_cache_index as $key => $val) {
{
$file_sum += $val['all']['sum']; $file_sum += $val['all']['sum'];
$file_size += $val['all']['size']; $file_size += $val['all']['size'];
++$host_sum; ++$host_sum;
@ -72,10 +73,8 @@ foreach ($hyproxy_cache_index as $key=>$val)
<th>使用空间</th> <th>使用空间</th>
</tr> </tr>
<?php <?php
foreach ($val as $k=>$v) foreach ($val as $k => $v) {
{ if ($k != 'all') {
if($k != 'all')
{
?> ?>
<tr> <tr>
<td class="object_name"><?php echo $k;?></td> <td class="object_name"><?php echo $k;?></td>

View File

@ -4,7 +4,9 @@
<div id="body"> <div id="body">
<?php include('hyproxy_category.php'); ?> <?php include('hyproxy_category.php'); ?>
<?php <?php
if (!empty($notice)) echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>'; if (!empty($notice)) {
echo '<div style="margin:5px 2px;width:500px;"><p id="' . $status . '">' . $notice . '</p></div>';
}
?> ?>
<p>HYProxy反向代理:</p> <p>HYProxy反向代理:</p>
<div id="HYProxy_list"> <div id="HYProxy_list">
@ -16,18 +18,14 @@
<th>管理</th> <th>管理</th>
</tr> </tr>
<?php <?php
if(!is_array($hyproxy_list) || count($hyproxy_list) < 1) if (!is_array($hyproxy_list) || count($hyproxy_list) < 1) {
{
?> ?>
<tr> <tr>
<td colspan="4" style="padding:10px;">暂无绑定域名。</td> <td colspan="4" style="padding:10px;">暂无绑定域名。</td>
</tr> </tr>
<?php <?php
} } else {
else foreach ($hyproxy_list as $key => $val) {
{
foreach ($hyproxy_list as $key=>$val)
{
?> ?>
<tr> <tr>
<th class="i"><?php echo $val['hyproxy_id'];?></th> <th class="i"><?php echo $val['hyproxy_id'];?></th>

View File

@ -0,0 +1,18 @@
#禁用反向代理缓存模板
server {
listen 80;
listen [::]:80;
server_name {$server_name};
index {$index};
location / {
default_type text/html;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host {$header_host};
proxy_pass {$proxy_pass};
}
access_log {$access_log_path} combined; #access_log end
error_log {$error_log_path} crit; #error_log end
}

View File

@ -0,0 +1,26 @@
#启用反向代理缓存模板
server {
listen 80;
listen [::]:80;
server_name {$server_name};
index {$index};
location / {
default_type text/html;
proxy_cache hyproxy;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host {$header_host};
proxy_pass {$proxy_pass};
if ($http_Cache_Control = "no-cache") {
rewrite ^(.*)$ /purge$1 last;
}
}
location ~ /purge(/.*){
proxy_cache_purge hyproxy $scheme://$host$1$is_args$args;
}
access_log {$access_log_path} combined; #access_log end
error_log {$error_log_path} crit; #error_log end
}

View File

@ -1,5 +1,5 @@
<?php <?php
/* /**
* HYPorxy CLI 文件 * HYPorxy CLI 文件
* 依赖PDO 、InnoDB * 依赖PDO 、InnoDB
* 需要安装PDO_MYSQL-1.0.2插件 * 需要安装PDO_MYSQL-1.0.2插件
@ -10,13 +10,17 @@ if (PHP_SAPI !== "cli") {
header("status: 404 Not Found"); header("status: 404 Not Found");
die(); die();
} }
define ('AMH_ROOT', '/home/wwwroot/index/web' . DIRECTORY_SEPARATOR); // AHM根目录
define ('AMH_Amysql', AMH_ROOT . 'Amysql' . DIRECTORY_SEPARATOR); // 系统目录
define ('_HYProxy', dirname(__FILE__) . DIRECTORY_SEPARATOR); // HYProxy插件根目录
define ('_HYCONF', '/usr/local/nginx/conf/hyproxy' . DIRECTORY_SEPARATOR); // HY Nginx conf目录
require_once(AMH_Amysql . 'Config.php'); //加载配置参数 define('DS', DIRECTORY_SEPARATOR); // 目录分隔符
require_once(_HYProxy . 'Class/medoo.php'); //加载medoo数据库类库 define('AMH_ROOT', '/home/wwwroot/index/web' . DS); // AHM根目录
define('AMH_AMYSQL', AMH_ROOT . 'Amysql' . DS); // 系统目录
define('_HYPROXY', dirname(__FILE__) . DS); // HYProxy插件根目录
define('_HYCONF', '/usr/local/nginx/conf/hyproxy' . DS); // HYProxy Nginx conf目录
define('_HYLOGS', '/home/hyproxy_logs' . DS); // HYProxy Nginx logs目录
define('_HYBAKS', '/home/hyproxy_backup' . DS); // HYProxy Backup目录
require_once(AMH_AMYSQL . 'Config.php'); //加载配置参数
require_once(_HYPROXY . 'Class/medoo.php'); //加载medoo数据库类库
$HYConfig = array( $HYConfig = array(
'database_type' => 'mysql', 'database_type' => 'mysql',
@ -35,6 +39,7 @@ $opt = array(
'proxy_pass::',//后端 'proxy_pass::',//后端
'field_name::',//字段名 'field_name::',//字段名
'value::',//字段内容 'value::',//字段内容
'bak_name::',//备份名称
); );
$param = getopt('', $opt); $param = getopt('', $opt);
//print_r($param); //print_r($param);
@ -54,26 +59,51 @@ switch ($param['action']) {
case 'del': case 'del':
echo hyproxy_del($param['server_name']); echo hyproxy_del($param['server_name']);
break; break;
case 'uninstall':
echo '';
// echo hyproxy_uninstall();
break;
case 'backup':
echo hyproxy_backup();
break;
case 'restore':
echo hyproxy_restore($param['bak_name']);
break;
case 'amproxy_bak' :
echo hyproxy_amproxy_bak();
break;
default: default:
echo 'error action'; echo 'error action';
} }
function hyproxy_list($proxy_status){//done 列出站点列表ID,域名,状态 /**
* 列出站点列表ID,域名,状态
* @param $proxy_status
* @return string
*/
function hyproxy_list($proxy_status)
{
global $medoo; global $medoo;
empty($proxy_status) && $proxy_status = 'start'; empty($proxy_status) && $proxy_status = 'start';
$table = 'module_hyproxy'; $table = 'module_hyproxy';
$columns = array('hyproxy_id','server_name','status'); $columns = array('hyproxy_id','server_name','status');
$where = array('status'=>$proxy_status); $where = array('status'=>$proxy_status);
$data = $medoo->select($table,$columns,$where); $list = $medoo->select($table, $columns, $where);
$res = ''; $res = '';
foreach ($data as &$value) { foreach ($list as $row) {
$res.=sprintf("%d,%s,%s\n",$value['hyproxy_id'],$value['server_name'],$value['status']); $res.=sprintf("%d,%s,%s\n", $row['hyproxy_id'], $row['server_name'], $row['status']);
} }
return $res; return $res;
} }
function hyproxy_make($server_name){//done /**
* 生成nginx配置文件
* @param $server_name
* @return string
*/
function hyproxy_make($server_name = null)
{
global $medoo; global $medoo;
$table = 'module_hyproxy'; $table = 'module_hyproxy';
$columns = '*'; $columns = '*';
@ -81,71 +111,61 @@ function hyproxy_make($server_name){//done
if (!empty($server_name)) {//指定域名 if (!empty($server_name)) {//指定域名
$where = array('server_name'=>trim($server_name)); $where = array('server_name'=>trim($server_name));
} }
$data = $medoo->select($table,$columns,$where); $list = $medoo->select($table, $columns, $where);
$res = ''; $res = '';
if (empty($data)) { if (empty($list)) {
$res = "[ERROR] The server_name does not exist.\n"; $res = "[ERROR] The server_name does not exist.\n";
} else { } else {
if (empty($server_name)) { if (empty($server_name)) {
hy_deldir(_HYCONF);//删除所有 hy_deldir(_HYCONF);//删除所有
} }
foreach ($data as &$value) { foreach ($list as $row) {
if (!empty($value['proxy_cache'])) { //定义日志完整路径
$nginx = sprintf('server { $row['access_log_path'] = _HYLOGS . $row['server_name'] . '.access.log';
listen 80; $row['error_log_path'] = _HYLOGS . $row['server_name'] . '.error.log';
listen [::]:80; //构建conf内容
server_name %s; $conf = hy_build_nginx_conf($row);
index %s;
location / { //文件操作
default_type text/html; $filebytes = false;
proxy_cache hyproxy; if ($row['status']!='start') {
proxy_set_header X-Real-IP $remote_addr; $filebytes = unlink(_HYCONF.$row['server_name'].'.conf');//删除
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } elseif (!empty($conf)) {
proxy_set_header Host %s; if (!is_dir(_HYCONF)) {
proxy_pass %s; mkdir(_HYCONF, 0755);
if ($http_Cache_Control = "no-cache") {
rewrite ^(.*)$ /purge$1 last;
} }
if (!is_dir(_HYLOGS)) {
mkdir(_HYLOGS, 0755);
} }
$filebytes = file_put_contents(_HYCONF.$row['server_name'].'.conf', $conf); //写入内容
location ~ /purge(/.*){ //access_log_path不存在的话创建
proxy_cache_purge hyproxy $scheme://$host$1$is_args$args; if (!file_exists($row['access_log_path'])) {
file_put_contents($row['access_log_path'], '');
} }
}',$value['server_name'],$value['index'],$value['header_host'],$value['proxy_pass']); //error_log_path不存在的话创建
} else { if (!file_exists($row['error_log_path'])) {
$nginx = sprintf('server { file_put_contents($row['error_log_path'], '');
listen 80;
listen [::]:80;
server_name %s;
index %s;
location / {
default_type text/html;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host %s;
proxy_pass %s;
} }
}',$value['server_name'],$value['index'],$value['header_host'],$value['proxy_pass']);
}
if ($value['status']=='start') {
$filebytes = file_put_contents(_HYCONF.$value['server_name'].'.conf', $nginx); //写入内容
} else {
$filebytes = unlink(_HYCONF.$value['server_name'].'.conf');//删除
} }
if ($filebytes) { if ($filebytes) {
$res.=sprintf("[OK] %s conf make is done.\n",$value['server_name']); $res.=sprintf("[OK] %s conf make is done.\n", $row['server_name']);
} else { } else {
$res.=sprintf("[ERROR] %s conf make is error.\n",$value['server_name']); $res.=sprintf("[ERROR] %s conf make is error.\n", $row['server_name']);
} }
} }
} }
return $res; return $res;
} }
function hyproxy_add($server_name,$proxy_pass=''){//done /**
* 新建反向代理
* @param $server_name
* @param string $proxy_pass
* @return string
*/
function hyproxy_add($server_name, $proxy_pass = '')
{
if (empty($server_name)) { if (empty($server_name)) {
return "[ERROR] The server_name is empty.\n";//不能为空 return "[ERROR] The server_name is empty.\n";//不能为空
} }
@ -184,7 +204,15 @@ function hyproxy_add($server_name,$proxy_pass=''){//done
return $res; return $res;
} }
function hyproxy_edit($server_name,$field_name='',$value=''){//done /**
* 修改反向代理参数
* @param $server_name
* @param string $field_name
* @param string $value
* @return string
*/
function hyproxy_edit($server_name, $field_name = '', $value = '')
{
if (empty($server_name)) { if (empty($server_name)) {
return "[ERROR] The server_name is empty.\n";//不能为空 return "[ERROR] The server_name is empty.\n";//不能为空
} }
@ -226,19 +254,24 @@ function hyproxy_edit($server_name,$field_name='',$value=''){//done
return $res; return $res;
} }
function hyproxy_del($server_name){ /**
if (empty($server_name)) { * 删除反向代理
return "[ERROR] The server_name is empty.\n";//不能为空 * @param $server_name
} * @return string
*/
function hyproxy_del($server_name = null)
{
$res = ''; $res = '';
if (empty($server_name)) {
$res = "[ERROR] The server_name is empty.\n";//不能为空
} else {
global $medoo; global $medoo;
$table = 'module_hyproxy'; $table = 'module_hyproxy';
$columns = '*'; $columns = '*';
$where = array('server_name'=>trim($server_name)); $where = array('server_name'=>trim($server_name));
$proxy = $medoo->get($table, $columns, $where); $row = $medoo->get($table, $columns, $where);
if (empty($proxy)) { if (empty($row)) {
return "[ERROR] The server_name is not exist.\n";//不存在 $res = "[ERROR] The server_name is not exist.\n";//不存在
} else { } else {
$mysqlres = $medoo->delete($table, $where); $mysqlres = $medoo->delete($table, $where);
if ($mysqlres) { if ($mysqlres) {
@ -247,10 +280,143 @@ function hyproxy_del($server_name){
$res = "[ERROR] Proxy del error.\n"; $res = "[ERROR] Proxy del error.\n";
} }
} }
}
unlink(_HYCONF . $server_name . '.conf');//删除配置文件
unlink(_HYLOGS . $server_name . '.access.log');//删除日志文件
unlink(_HYLOGS . $server_name . '.error.log');//删除日志文件
return $res; return $res;
} }
function hy_deldir($path) { /**
* 已作废
* 卸载插件时做的一些处理
* 谨慎使用
*/
function hyproxy_uninstall()
{
//先备份
hyproxy_backup();
//循环删除所有站点
global $medoo;
$table = 'module_hyproxy';
$columns = array('hyproxy_id','server_name','status');
$list = $medoo->select($table, $columns);
if (!empty($list)) {
$medoo->delete($table, null);
foreach ($list as $row) {
unlink(_HYCONF . $row['server_name'] . '.conf');//删除配置文件
unlink(_HYLOGS . $row['server_name'] . '.access.log');//删除日志文件
unlink(_HYLOGS . $row['server_name'] . '.error.log');//删除日志文件
}
}
return "[OK] All proxy del success.\n";
}
/**
* 备份所有反向代理
*/
function hyproxy_backup()
{
if (!is_dir(_HYBAKS)) {
mkdir(_HYBAKS, 0755);
}
global $medoo;
$table = 'module_hyproxy';
$list = $medoo->select($table, '*');
$filebytes = false;
$filename = _HYBAKS . date("Y-m-d-H:i:s") . '.bak';
$filebytes = file_put_contents($filename, json_encode($list));//写入内容
if ($filebytes) {
$res="[OK] All proxy backup success.\n";
} else {
$res="[ERROR] All proxy backup error.\n";
}
return $res;
}
/**
* 还原所有反向代理
* @param null $bak_name
* @return string
*/
function hyproxy_restore($bak_name = null)
{
$backup_path = _HYBAKS. DS . $bak_name;
if (file_exists($backup_path)) {
$backup = file_get_contents($backup_path);
$list = json_decode($backup, true);
//修复mysql保留字段index问题导致无法写入
foreach ($list as &$row) {
$row['`index`'] = $row['index'];
unset($row['index']);
}
global $medoo;
$table = 'module_hyproxy';
$medoo->delete($table, null);
$medoo->query("alter table $table auto_increment=1");
$medoo->insert($table, $list);
hyproxy_make();
return "[OK] Proxy restore success\n";
} else {
return "[ERROR] Backup file not exist!\n";
}
}
/**
* 备份AMProxy的反向代理
* @return string
*/
function hyproxy_amproxy_bak()
{
$dir = '/usr/local/nginx/conf/proxy/';
if (is_dir($dir)) {
$list = scandir($dir);
foreach ($list as $row) {
unset($conf, $data);
//排除目录中的.和..
if ($row != "." && $row != ".." && is_file($dir.$row)) {
$conf = file_get_contents($dir.$row);
$data = array(
'server_name' => trim(hy_get_body($conf, 'server_name', ';', 1)),
'index' => 'index.php index.html index.htm',//index是保留字段
'header_host' => '$host',
'proxy_pass' => trim(hy_get_body($conf, 'proxy_pass', ';', 1)),
'proxy_cache' => 1,
'status' => 'start',
'time' => time()
);
if (trim(hy_get_body($conf, 'proxy_set_header Host ', ';', 1)) != $data['server_name']) {
$data['header_host'] = trim(hy_get_body($conf, 'proxy_set_header Host ', ';', 1));
}
$proxy[] = $data;
}
}
}
if (!empty($proxy)) {
if (!is_dir(_HYBAKS)) {
mkdir(_HYBAKS, 0755);
}
$filebytes = false;
$filename = _HYBAKS . 'AMProxy' .date("Y-m-d-H:i:s") . '.bak';
$filebytes = file_put_contents($filename, json_encode($proxy));//写入内容
if ($filebytes) {
$res="[OK] AMProxy proxy backup success.\n";
} else {
$res="[ERROR] AMProxy proxy backup error.\n";
}
} else {
$res = '[ERROR] No AMProxy.';
}
return $res;
}
/**
* 删除目录和子目录
* @param $path
*/
function hy_deldir($path)
{
//如果是目录则继续 //如果是目录则继续
if (is_dir($path)) { if (is_dir($path)) {
//扫描一个文件夹内的所有文件夹和文件并返回数组 //扫描一个文件夹内的所有文件夹和文件并返回数组
@ -272,3 +438,73 @@ function hy_deldir($path) {
} }
} }
} }
/**
* 根据传入数据构建nginx配置内容
* @param $row
*/
function hy_build_nginx_conf($row)
{
//开启反向代理缓存模板
$proxy_cache_template_path = _HYPROXY . 'conf' . DS . 'proxy_cache_template.conf';
$proxy_cache_template = '';
if (file_exists($proxy_cache_template_path)) {
$proxy_cache_template = file_get_contents($proxy_cache_template_path);
}
//禁用反向代理缓存模板
$no_cache_template_path = _HYPROXY . 'conf' . DS . 'no_cache_template.conf';
$no_cache_template = '';
if (file_exists($no_cache_template_path)) {
$no_cache_template = file_get_contents($no_cache_template_path);
}
$conf = false;
if (!empty($row['server_name'])) {
//判断使用哪个模板
$template = false;
if (!empty($row['proxy_cache']) && !empty($proxy_cache_template)) {
$template = $proxy_cache_template;
} elseif (!empty($no_cache_template)) {
$template = $no_cache_template;
}
//模板有效的话,进行遍历数组替换模板变量
if ($template) {
foreach ($row as $k => $v) {
$template=str_replace('{$'.$k.'}', $v, $template);
}
$conf = $template;
}
}
return $conf;
}
/**
* 输入始末关键词提取主要内容
* @param $str string 提取前内容
* @param $start string 开始关键词
* @param $end string 结束关键词
* @param $option int 选项 1:不包含前后关键词 2:包含前关键词 3:包含后关键词 其他(默认):包含前后关键词
* @return string
*/
function hy_get_body($str, $start, $end, $option)
{
$str_array=explode($start, $str);
$tem=$str_array[1];
if (empty($end)) {
return $tem;
} else {
$str_array=explode($end, $tem);
if ($option==1) {
return $str_array[0];
}
if ($option==2) {
return $start.$str_array[0];
}
if ($option==3) {
return $str_array[0].$end;
} else {
return $start.$str_array[0].$end;
}
}
}