511 lines
17 KiB
PHP
511 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* HYPorxy CLI 文件
|
|
* 依赖PDO 、InnoDB
|
|
* 需要安装PDO_MYSQL-1.0.2插件
|
|
* 需要打开InnoDB支持
|
|
*/
|
|
if (PHP_SAPI !== "cli") {
|
|
header('HTTP/1.1 404 Not Found');
|
|
header("status: 404 Not Found");
|
|
die();
|
|
}
|
|
|
|
define('DS', DIRECTORY_SEPARATOR); // 目录分隔符
|
|
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(
|
|
'database_type' => 'mysql',
|
|
'database_name' => $Config['DBname'],
|
|
'server' => 'localhost',
|
|
'username' => $Config['User'],
|
|
'password' => $Config['Password'],
|
|
'charset' => 'utf8'
|
|
);
|
|
$medoo = new medoo($HYConfig);
|
|
|
|
$opt = array(
|
|
'action:',
|
|
'proxy_status::',//状态
|
|
'server_name::',//域名
|
|
'proxy_pass::',//后端
|
|
'field_name::',//字段名
|
|
'value::',//字段内容
|
|
'bak_name::',//备份名称
|
|
);
|
|
$param = getopt('', $opt);
|
|
//print_r($param);
|
|
switch ($param['action']) {
|
|
case 'list':
|
|
echo hyproxy_list($param['proxy_status']);
|
|
break;
|
|
case 'make':
|
|
echo hyproxy_make($param['server_name']);
|
|
break;
|
|
case 'add':
|
|
echo hyproxy_add($param['server_name'], $param['proxy_pass']);
|
|
break;
|
|
case 'edit':
|
|
echo hyproxy_edit($param['server_name'], $param['field_name'], $param['value']);
|
|
break;
|
|
case 'del':
|
|
echo hyproxy_del($param['server_name']);
|
|
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:
|
|
echo 'error action';
|
|
}
|
|
|
|
/**
|
|
* 列出站点列表ID,域名,状态
|
|
* @param $proxy_status
|
|
* @return string
|
|
*/
|
|
function hyproxy_list($proxy_status)
|
|
{
|
|
global $medoo;
|
|
empty($proxy_status) && $proxy_status = 'start';
|
|
|
|
$table = 'module_hyproxy';
|
|
$columns = array('hyproxy_id','server_name','status');
|
|
$where = array('status'=>$proxy_status);
|
|
$list = $medoo->select($table, $columns, $where);
|
|
$res = '';
|
|
foreach ($list as $row) {
|
|
$res.=sprintf("%d,%s,%s\n", $row['hyproxy_id'], $row['server_name'], $row['status']);
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 生成nginx配置文件
|
|
* @param $server_name
|
|
* @return string
|
|
*/
|
|
function hyproxy_make($server_name = null)
|
|
{
|
|
global $medoo;
|
|
$table = 'module_hyproxy';
|
|
$columns = '*';
|
|
$where = '';
|
|
if (!empty($server_name)) {//指定域名
|
|
$where = array('server_name'=>trim($server_name));
|
|
}
|
|
$list = $medoo->select($table, $columns, $where);
|
|
$res = '';
|
|
if (empty($list)) {
|
|
$res = "[ERROR] The server_name does not exist.\n";
|
|
} else {
|
|
if (empty($server_name)) {
|
|
hy_deldir(_HYCONF);//删除所有
|
|
}
|
|
foreach ($list as $row) {
|
|
//定义日志完整路径
|
|
$row['access_log_path'] = _HYLOGS . $row['server_name'] . '.access.log';
|
|
$row['error_log_path'] = _HYLOGS . $row['server_name'] . '.error.log';
|
|
//构建conf内容
|
|
$conf = hy_build_nginx_conf($row);
|
|
|
|
//文件操作
|
|
$filebytes = false;
|
|
if ($row['status']!='start') {
|
|
$filebytes = unlink(_HYCONF.$row['server_name'].'.conf');//删除
|
|
} elseif (!empty($conf)) {
|
|
if (!is_dir(_HYCONF)) {
|
|
mkdir(_HYCONF, 0755);
|
|
}
|
|
if (!is_dir(_HYLOGS)) {
|
|
mkdir(_HYLOGS, 0755);
|
|
}
|
|
$filebytes = file_put_contents(_HYCONF.$row['server_name'].'.conf', $conf); //写入内容
|
|
//access_log_path不存在的话创建
|
|
if (!file_exists($row['access_log_path'])) {
|
|
file_put_contents($row['access_log_path'], '');
|
|
}
|
|
//error_log_path不存在的话创建
|
|
if (!file_exists($row['error_log_path'])) {
|
|
file_put_contents($row['error_log_path'], '');
|
|
}
|
|
}
|
|
|
|
if ($filebytes) {
|
|
$res.=sprintf("[OK] %s conf make is done.\n", $row['server_name']);
|
|
} else {
|
|
$res.=sprintf("[ERROR] %s conf make is error.\n", $row['server_name']);
|
|
}
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 新建反向代理
|
|
* @param $server_name
|
|
* @param string $proxy_pass
|
|
* @return string
|
|
*/
|
|
function hyproxy_add($server_name, $proxy_pass = '')
|
|
{
|
|
if (empty($server_name)) {
|
|
return "[ERROR] The server_name is empty.\n";//不能为空
|
|
}
|
|
|
|
$res = '';
|
|
global $medoo;
|
|
$table = 'module_hyproxy';
|
|
$columns = '*';
|
|
$where = array('server_name'=>trim($server_name));
|
|
$proxy = $medoo->get($table, $columns, $where);
|
|
|
|
if ($proxy) {
|
|
return "[ERROR] The server_name is exist.\n";//已存在
|
|
} else {
|
|
empty($proxy_pass) && $proxy_pass = 'http://'.$server_name;
|
|
if (!preg_match('/^http(s)?:\\/\\/.+/', $proxy_pass)) {
|
|
$proxy_pass = 'http://'.$proxy_pass;
|
|
}
|
|
$data = array(
|
|
'server_name' => $server_name,
|
|
'`index`' => 'index.php index.html index.htm',//index是保留字段
|
|
'header_host' => '$host',
|
|
'proxy_pass' => $proxy_pass,
|
|
'proxy_cache' => 1,
|
|
'status' => 'start',
|
|
'time' => time()
|
|
);
|
|
$mysqlres = $medoo->insert($table, $data);
|
|
if ($mysqlres) {
|
|
$res = "[OK] Proxy add success.\n";
|
|
$res.= hyproxy_make($server_name);
|
|
} else {
|
|
$res = "[ERROR] Proxy add error.\n";
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 修改反向代理参数
|
|
* @param $server_name
|
|
* @param string $field_name
|
|
* @param string $value
|
|
* @return string
|
|
*/
|
|
function hyproxy_edit($server_name, $field_name = '', $value = '')
|
|
{
|
|
if (empty($server_name)) {
|
|
return "[ERROR] The server_name is empty.\n";//不能为空
|
|
}
|
|
if (empty($field_name)) {
|
|
return "[ERROR] The field_name is empty.\n";//不能为空
|
|
}
|
|
$value = trim($value);
|
|
$res = '';
|
|
global $medoo;
|
|
$table = 'module_hyproxy';
|
|
$columns = '*';
|
|
$where = array('server_name'=>trim($server_name));
|
|
$proxy = $medoo->get($table, $columns, $where);
|
|
|
|
if (empty($proxy)) {
|
|
return "[ERROR] The server_name is not exist.\n";//不存在
|
|
} else {
|
|
if ($field_name=='proxy_pass') {
|
|
empty($value) && $value = 'http://'.$server_name;
|
|
if (!preg_match('/^http(s)?:\\/\\/.+/', $value)) {
|
|
$value = 'http://'.$value;
|
|
}
|
|
}
|
|
if ($field_name=='header_host') {
|
|
empty($value) && $value = '$host';
|
|
}
|
|
$data = array(
|
|
$field_name => $value,
|
|
'time' => time()
|
|
);
|
|
$mysqlres = $medoo->update($table, $data, array('hyproxy_id'=>$proxy['hyproxy_id']));
|
|
if ($mysqlres) {
|
|
$res = "[OK] Proxy edit success.\n";
|
|
$res.= hyproxy_make($server_name);
|
|
} else {
|
|
$res = "[ERROR] Proxy edit error.\n";
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 删除反向代理
|
|
* @param $server_name
|
|
* @return string
|
|
*/
|
|
function hyproxy_del($server_name = null)
|
|
{
|
|
$res = '';
|
|
if (empty($server_name)) {
|
|
$res = "[ERROR] The server_name is empty.\n";//不能为空
|
|
} else {
|
|
global $medoo;
|
|
$table = 'module_hyproxy';
|
|
$columns = '*';
|
|
$where = array('server_name'=>trim($server_name));
|
|
$row = $medoo->get($table, $columns, $where);
|
|
if (empty($row)) {
|
|
$res = "[ERROR] The server_name is not exist.\n";//不存在
|
|
} else {
|
|
$mysqlres = $medoo->delete($table, $where);
|
|
if ($mysqlres) {
|
|
$res = "[OK] Proxy del success.\n";
|
|
} else {
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* 已作废
|
|
* 卸载插件时做的一些处理
|
|
* 谨慎使用
|
|
*/
|
|
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)) {
|
|
//扫描一个文件夹内的所有文件夹和文件并返回数组
|
|
$p = scandir($path);
|
|
foreach ($p as $val) {
|
|
//排除目录中的.和..
|
|
if ($val != "." && $val != "..") {
|
|
//如果是目录则递归子目录,继续操作
|
|
if (is_dir($path . $val)) {
|
|
//子目录中操作删除文件夹和文件
|
|
deldir($path . $val . '/');
|
|
//目录清空后删除空文件夹
|
|
@rmdir($path . $val . '/');
|
|
} else {
|
|
//如果是文件直接删除
|
|
unlink($path . $val);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据传入数据构建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;
|
|
}
|
|
}
|
|
}
|