278 lines
8.3 KiB
PHP
278 lines
8.3 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 ('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'); //加载配置参数
|
|
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::',//字段内容
|
|
);
|
|
$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;
|
|
default:
|
|
echo 'error action';
|
|
}
|
|
|
|
function hyproxy_list($proxy_status){//done 列出站点列表ID,域名,状态
|
|
global $medoo;
|
|
empty($proxy_status) && $proxy_status = 'start';
|
|
|
|
$table = 'module_hyproxy';
|
|
$columns = array('hyproxy_id','server_name','status');
|
|
$where = array('status'=>$proxy_status);
|
|
$data = $medoo->select($table,$columns,$where);
|
|
$res = '';
|
|
foreach ($data as &$value) {
|
|
$res.=sprintf("%d,%s,%s\n",$value['hyproxy_id'],$value['server_name'],$value['status']);
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
function hyproxy_make($server_name){//done
|
|
global $medoo;
|
|
$table = 'module_hyproxy';
|
|
$columns = '*';
|
|
$where = '';
|
|
if (!empty($server_name)) {//指定域名
|
|
$where = array('server_name'=>trim($server_name));
|
|
}
|
|
$data = $medoo->select($table,$columns,$where);
|
|
$res = '';
|
|
if (empty($data)) {
|
|
$res = "[ERROR] The server_name does not exist.\n";
|
|
} else {
|
|
if (empty($server_name)) {
|
|
hy_deldir(_HYCONF);//删除所有
|
|
}
|
|
foreach ($data as &$value) {
|
|
if (!empty($value['proxy_cache'])) {
|
|
$nginx = sprintf('server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name %s;
|
|
index %s;
|
|
|
|
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 %s;
|
|
proxy_pass %s;
|
|
if ($http_Cache_Control = "no-cache") {
|
|
rewrite ^(.*)$ /purge$1 last;
|
|
}
|
|
}
|
|
|
|
location ~ /purge(/.*){
|
|
proxy_cache_purge hyproxy $scheme://$host$1$is_args$args;
|
|
}
|
|
access_log /usr/local/nginx/logs/%s.access.log combined; #access_log end
|
|
error_log /usr/local/nginx/logs/%s.error.log crit; #error_log end
|
|
}',$value['server_name'],$value['index'],$value['header_host'],$value['proxy_pass'],$value['server_name'],$value['server_name']);
|
|
} else {
|
|
$nginx = sprintf('server {
|
|
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;
|
|
}
|
|
access_log /usr/local/nginx/logs/%s.access.log combined; #access_log end
|
|
error_log /usr/local/nginx/logs/%s.error.log crit; #error_log end
|
|
}',$value['server_name'],$value['index'],$value['header_host'],$value['proxy_pass'],$value['server_name'],$value['server_name']);
|
|
}
|
|
if ($value['status']=='start') {
|
|
$filebytes = file_put_contents(_HYCONF.$value['server_name'].'.conf', $nginx); //写入内容
|
|
} else {
|
|
$filebytes = unlink(_HYCONF.$value['server_name'].'.conf');//删除
|
|
}
|
|
|
|
if($filebytes){
|
|
$res.=sprintf("[OK] %s conf make is done.\n",$value['server_name']);
|
|
}else{
|
|
$res.=sprintf("[ERROR] %s conf make is error.\n",$value['server_name']);
|
|
}
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
function hyproxy_add($server_name,$proxy_pass=''){//done
|
|
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;
|
|
}
|
|
|
|
function hyproxy_edit($server_name,$field_name='',$value=''){//done
|
|
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;
|
|
}
|
|
|
|
function hyproxy_del($server_name){
|
|
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 (empty($proxy)) {
|
|
return "[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";
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |