'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 hyproxy_uninstall(); break; case 'backup': echo hyproxy_backup(); break; case 'restore': echo hyproxy_restore($param['bak_name']); 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) { $filebytes = false; $conf = hy_build_nginx_conf($row); if (!$row['status']=='start') { $filebytes = unlink(_HYCONF.$row['server_name'].'.conf');//删除 } elseif (!empty($conf)) { $filebytes = file_put_contents(_HYCONF.$row['server_name'].'.conf', $conf); //写入内容 } 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) { 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"; } } 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'; $medoo->delete($table, null); hyproxy_make(); return "[OK] All proxy del success.\n"; } /** * 备份所有反向代理 */ function hyproxy_backup() { global $medoo; $table = 'module_hyproxy'; $list = $medoo->select($table, '*'); $filebytes = false; $filename = _HYPROXY . 'baks' . DS . 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 = _HYPROXY . 'baks' . 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"; } } /** * 删除目录和子目录 * @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)) { $row['logs_path'] = _HYLOGS . $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; }