'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; }