259 lines
10 KiB
PHP
259 lines
10 KiB
PHP
<?php
|
|
|
|
class hyproxys extends AmysqlModel
|
|
{
|
|
function hyclear_result($str)
|
|
{
|
|
$str = Functions::trim_result($str);
|
|
$str = str_replace(array('[OK] HYProxy is already installed.', '[OK] Nginx reload','[HYProxy-1.0 admin]'), '', $str);
|
|
$arr = explode("\n", $str); //分割为数组,每行为一个数组元素
|
|
$arr = array_filter(array_map('trim', $arr)); //去除数组中的空元素
|
|
return implode(PHP_EOL, $arr); //用换行符连结数组为字符串
|
|
}
|
|
|
|
// 反代列表
|
|
function hyproxy_list($args = '')
|
|
{
|
|
//OK
|
|
//TODO
|
|
//$server_name = $args['server_name'];域名搜索
|
|
//$status;//状态搜索start/stop
|
|
//分页
|
|
$sql = "SELECT * FROM module_hyproxy ORDER BY hyproxy_id ASC";
|
|
return $this -> _all($sql);
|
|
}
|
|
// 取得反代网站
|
|
function hyproxy_get($name)
|
|
{
|
|
//OK
|
|
$sql = "SELECT * FROM module_hyproxy WHERE `server_name` = '$name'";
|
|
return $this -> _row($sql);
|
|
}
|
|
// 增加反代网站
|
|
function hyproxy_add($server_name, $proxy_pass)
|
|
{
|
|
//OK
|
|
$server_name = trim($server_name);
|
|
$proxy_pass = trim($proxy_pass);
|
|
$proxy = $this->hyproxy_get($server_name);
|
|
if ($proxy) {
|
|
return array( 'status' => 'error' , 'notice' => '绑定域名已存在,请删除旧记录或更换域名。' );
|
|
} 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',
|
|
'header_host' => '$host',
|
|
'proxy_pass' => $proxy_pass,
|
|
'proxy_cache' => 1,
|
|
'status' => 'start',
|
|
'time' => time()
|
|
);
|
|
$res = $this -> _insert('module_hyproxy', $data);
|
|
if ($res) {
|
|
$cmd = "amh module HYProxy-1.0 admin make,$server_name";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
shell_exec($cmd);
|
|
return array( 'status' => 'success' , 'notice' => $server_name.' : HYProxy域名增加成功。' );
|
|
} else {
|
|
return array( 'status' => 'error' , 'notice' => $server_name.' : HYProxy域名增加失败。' );
|
|
}
|
|
}
|
|
}
|
|
// 编辑反代网站
|
|
function hyproxy_edit()
|
|
{
|
|
//OK
|
|
$server_name = trim($_POST['server_name']);
|
|
if ($_POST['index']!=$_POST['index_hidden']) {//默认首页
|
|
$data['index'] = empty($_POST['index']) ? 'index.php index.html index.htm' : trim($_POST['index']);
|
|
}
|
|
if ($_POST['header_host']!=$_POST['header_host_hidden']) {//Host头
|
|
$data['header_host'] = empty($_POST['header_host']) ? '$host' : trim($_POST['header_host']);
|
|
}
|
|
if ($_POST['proxy_pass']!=$_POST['proxy_pass_hidden']) {//后端地址
|
|
$data['proxy_pass'] = trim($_POST['proxy_pass']);
|
|
empty($data['proxy_pass']) && $data['proxy_pass'] = 'http://'.$server_name;
|
|
if (!preg_match('/^http(s)?:\\/\\/.+/', $data['proxy_pass'])) {
|
|
$data['proxy_pass'] = 'http://'.$data['proxy_pass'];
|
|
}
|
|
}
|
|
$data['time'] = time();//时间戳
|
|
$where = sprintf("WHERE `server_name` = '%s'", $server_name);//查询条件
|
|
$res = $this -> _update('module_hyproxy', $data, $where);
|
|
|
|
if ($res) {
|
|
$cmd = "amh module HYProxy-1.0 admin make,$server_name";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
shell_exec($cmd);
|
|
return array( 'status' => 'success' , 'notice' => $server_name.' : HYProxy域名修改成功。' );
|
|
} else {
|
|
return array( 'status' => 'error' , 'notice' => $server_name.' : HYProxy域名修改失败。' );
|
|
}
|
|
}
|
|
// 删除反代网站
|
|
function hyproxy_del($del_name)
|
|
{
|
|
//OK
|
|
$del_name = trim($del_name);
|
|
$cmd = "amh module HYProxy-1.0 admin del,$del_name";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
$result = $this->hyclear_result(shell_exec($cmd));
|
|
if (strpos($result, '[ERROR]') === false) {
|
|
return array( 'status' => 'success' , 'notice' => $del_name.' : HYProxy域名删除成功。' );
|
|
} else {
|
|
return array( 'status' => 'error' , 'notice' => $del_name.' : HYProxy域名删除失败。' );
|
|
}
|
|
}
|
|
// 状态
|
|
function hyproxy_run($run_name, $g)
|
|
{
|
|
//OK
|
|
$run_zh = array(
|
|
'start' => '启动',
|
|
'stop' => '停止'
|
|
);
|
|
$data['status'] = $g;
|
|
$data['time'] = time();//时间戳
|
|
$where = sprintf("WHERE `server_name` = '%s'", $run_name);//查询条件
|
|
$res = $this -> _update('module_hyproxy', $data, $where);
|
|
|
|
if ($res) {
|
|
$cmd = "amh module HYProxy-1.0 admin make,$run_name";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
shell_exec($cmd);
|
|
return array( 'status' => 'success' , 'notice' => $run_name . ' : HYProxy域名' . $run_zh[$g] . '成功。' );
|
|
} else {
|
|
return array( 'status' => 'error' , 'notice' => $run_name . ' : HYProxy域名' . $run_zh[$g] . '失败。' );
|
|
}
|
|
}
|
|
// 缓存状态
|
|
function hyproxy_run_cache($run_name, $g)
|
|
{
|
|
//OK
|
|
$run_zh = array(
|
|
'1' => '启动',
|
|
'0' => '停止'
|
|
);
|
|
$data['proxy_cache'] = $g;
|
|
$data['time'] = time();//时间戳
|
|
$where = sprintf("WHERE `server_name` = '%s'", $run_name);//查询条件
|
|
$res = $this -> _update('module_hyproxy', $data, $where);
|
|
|
|
if ($res) {
|
|
$cmd = "amh module HYProxy-1.0 admin make,$run_name";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
shell_exec($cmd);
|
|
return array( 'status' => 'success' , 'notice' => $run_name . ' : HYProxy域名缓存' . $run_zh[$g] . '成功。' );
|
|
} else {
|
|
return array( 'status' => 'error' , 'notice' => $run_name . ' : HYProxy域名缓存' . $run_zh[$g] . '失败。' );
|
|
}
|
|
}
|
|
|
|
// *********************************************************
|
|
// 取得缓存
|
|
function get_hyproxy_cache()
|
|
{
|
|
$name = trim($name);
|
|
$cmd = "amh cat_nginx";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
$result = trim(shell_exec($cmd), "\n");
|
|
$result = Functions::trim_result($result);
|
|
preg_match("/hyproxy_cache levels=(.*)keys_zone=hyproxy:(.*)inactive=(.*)max_size=(.*);/", $result, $arr);
|
|
preg_match("/proxy_cache_valid 200 304(.*);/", $result, $arr2);
|
|
$data['levels'] = $arr[1];
|
|
$data['keys_zone'] = $arr[2];
|
|
$data['inactive'] = $arr[3];
|
|
$data['max_size'] = $arr[4];
|
|
$data['valid'] = $arr2[1];
|
|
foreach ($data as $key => $val) {
|
|
$data[$key] = trim($val);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
// 保存缓存
|
|
function save_hyproxy_cache()
|
|
{
|
|
$run_name = trim($run_name);
|
|
$cmd = "amh module HYProxy-1.0 admin cache,{$_POST['levels']},{$_POST['keys_zone']},{$_POST['max_size']},{$_POST['valid']},{$_POST['inactive']}";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
exec($cmd, $tmp, $status);
|
|
return !$status;
|
|
}
|
|
|
|
|
|
// *********************************************************
|
|
// 取得缓存索引
|
|
function get_hyproxy_cache_index()
|
|
{
|
|
// $sql = "SELECT count(*) FROM module_hyproxy GROUP BY hyproxy_type ";
|
|
$sql = "SELECT * FROM module_hyproxy_cache";
|
|
$result = $this -> _query($sql);
|
|
while ($rs = mysql_fetch_assoc($result)) {
|
|
$url_info = parse_url($rs['hyproxy_key']);
|
|
if (isset($url_info['host']) && !empty($url_info['host'])) {
|
|
++$data[$url_info['host']]['all']['sum'];
|
|
$data[$url_info['host']]['all']['size'] += $rs['hyproxy_size'];
|
|
|
|
++$data[$url_info['host']][$rs['hyproxy_type']]['sum'];
|
|
$data[$url_info['host']][$rs['hyproxy_type']]['size'] += $rs['hyproxy_size'];
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
// 创建缓存索引
|
|
function create_hyproxy_cache_index()
|
|
{
|
|
$val = (int)$_POST['cache_index_time_val'];
|
|
$type = array('1' => 60*24, '2' => 60, '3' => 1);
|
|
$area = array('1' => '-', '2' => '+');
|
|
$val = isset($type[$_POST['cache_index_time_type']]) ? $val * $type[$_POST['cache_index_time_type']] : $val * $type[1]; // 时间类型
|
|
$val = isset($area[$_POST['cache_index_time_type']]) ? $area[$_POST['cache_index_time_area']] . $val : '-' . $val; // 时间范围
|
|
$mode = $_POST['cache_index_time_mode'] == '2' ? 'truncate' : '';
|
|
|
|
$cmd = "amh module HYProxy-1.0 admin cache-index,$val,$mode";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
exec($cmd, $tmp, $status);
|
|
return !$status;
|
|
}
|
|
|
|
// *********************************************************
|
|
// 取得缓存列表
|
|
function get_hyproxy_cache_list($page = 1, $page_sum = 20)
|
|
{
|
|
$where = '';
|
|
|
|
if (isset($_GET['cache_key']) && !empty($_GET['cache_key'])) {
|
|
$_GET['cache_key'] = trim($_GET['cache_key']);
|
|
$where .= " AND hyproxy_key LIKE '{$_GET['cache_key']}'";
|
|
}
|
|
|
|
if (isset($_GET['cache_type']) && $_GET['cache_type'] != 'all') {
|
|
$where .= " AND hyproxy_type LIKE '{$_GET['cache_type']}'";
|
|
}
|
|
|
|
$limit = ' LIMIT ' . ($page-1)*$page_sum . ' , ' . $page_sum;
|
|
$sql = "SELECT * FROM module_hyproxy_cache WHERE 1 $where";
|
|
$sum = $this -> _sum($sql);
|
|
|
|
$sql = "SELECT * FROM module_hyproxy_cache WHERE 1 $where $limit";
|
|
return array('data' => $this -> _all($sql), 'sum' => $sum);
|
|
}
|
|
|
|
// 删除缓存
|
|
function hyproxy_cache_delete()
|
|
{
|
|
$url_param = (isset($_POST['cache_key']) && !empty($_POST['cache_key'])) ? $_POST['cache_key'] : '-all';
|
|
$file_type = (isset($_POST['cache_type']) && !empty($_POST['cache_type']) && $_POST['cache_type'] != 'all') ? $_POST['cache_type'] : '-all';
|
|
$cmd = "amh module HYProxy-1.0 admin cache-delete,$url_param,$file_type";
|
|
$cmd = Functions::trim_cmd($cmd);
|
|
exec($cmd, $tmp, $status);
|
|
return !$status;
|
|
}
|
|
}
|