diff --git a/AMHScript b/AMHScript index 2709076..dbd3db2 100644 --- a/AMHScript +++ b/AMHScript @@ -68,7 +68,9 @@ function amh_module_admin() echo "stop something"; ;; add) - echo "add something"; + proxy_pass=$3; + php ${module_dir}cli.php --action='add' --server_name=${domain} --proxy_pass=${proxy_pass}; + amh nginx reload; ;; edit) echo "edit something"; diff --git a/cli.php b/cli.php index 84e6d0d..0d4d4d8 100644 --- a/cli.php +++ b/cli.php @@ -46,7 +46,7 @@ echo amproxy_list($param['proxy_status']); break; case 'add': - //echo amproxy_add($param['server_name'], $param['proxy_pass']); + echo amproxy_add($param['server_name'], $param['proxy_pass']); break; case 'edit': //echo amproxy_edit($param['server_name'], $param['field_name'], $param['value']); @@ -82,4 +82,50 @@ $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; } \ No newline at end of file