216 lines
7.2 KiB
PHP
216 lines
7.2 KiB
PHP
<?php
|
|
/**
|
|
* AMPorxy CLI 文件
|
|
* 依赖HYProxy-1.0插件
|
|
*/
|
|
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__). '/../HYProxy-1.0' . 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);
|
|
|
|
switch ($param['action']) {
|
|
case 'list':
|
|
echo amproxy_list($param['proxy_status']);
|
|
break;
|
|
case 'add':
|
|
echo amproxy_add($param['server_name'], $param['proxy_pass']);
|
|
break;
|
|
case 'edit':
|
|
echo amproxy_edit($param['server_name'], $param['field_name'], $param['value']);
|
|
break;
|
|
case 'del':
|
|
echo amproxy_del($param['server_name']);
|
|
break;
|
|
default:
|
|
echo '无效操作';
|
|
}
|
|
|
|
/**
|
|
* 【完成】
|
|
* 反向代理列表
|
|
* @param $proxy_status = start,stop
|
|
* @return string
|
|
*/
|
|
function amproxy_list($proxy_status = null)
|
|
{
|
|
global $medoo;
|
|
|
|
$table = 'module_hyproxy';
|
|
$columns = array('server_name','status');
|
|
$where = !empty($proxy_status) ? array('status'=>$proxy_status) : '';
|
|
$where['ORDER'] = "status";
|
|
$list = $medoo->select($table, $columns, $where);
|
|
$res = '';
|
|
foreach ($list as $row) {
|
|
$status = '[Stop]';
|
|
if ($row['status']=='start') {
|
|
$status = '[Running]';
|
|
}
|
|
$res.=sprintf("%s %s\n", $row['server_name'], $status);
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 【完成】
|
|
* 新建反向代理
|
|
* @param $server_name
|
|
* @param string $proxy_pass
|
|
* @return string
|
|
*/
|
|
function amproxy_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] $server_name AMProxy 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] successfully create $server_name AMProxy.\n";
|
|
shell_exec("php "._HYPROXY."hyproxy-cli.php --action='make' --server_name=$server_name");
|
|
} else {
|
|
$res = "[Error] error create $server_name AMProxy.\n";
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 修改反向代理参数
|
|
* @param $server_name
|
|
* @param string $field_name
|
|
* @param string $value
|
|
* @return string
|
|
*/
|
|
function amproxy_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] $server_name AMProxy 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] $field_name parameters set success.\n";
|
|
shell_exec("php "._HYPROXY."hyproxy-cli.php --action='make' --server_name=$server_name");
|
|
} else {
|
|
$res = "[Error] $field_name parameters set error.\n";
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 【完成】
|
|
* 删除反向代理
|
|
* @param $server_name
|
|
* @return string
|
|
*/
|
|
function amproxy_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] $server_name AMProxy not exist!\n";//不存在
|
|
} else {
|
|
$mysqlres = $medoo->delete($table, $where);
|
|
if ($mysqlres) {
|
|
$res = "[OK] successfully delete $server_name AMProxy.\n";
|
|
} else {
|
|
$res = "[Error] error delete $server_name AMProxy.\n";
|
|
}
|
|
}
|
|
}
|
|
unlink(_HYCONF . $server_name . '.conf');//删除配置文件
|
|
unlink(_HYLOGS . $server_name . '.access.log');//删除日志文件
|
|
unlink(_HYLOGS . $server_name . '.error.log');//删除日志文件
|
|
return $res;
|
|
} |