1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| <?php
define('BASE_DIR',__DIR__);
class V2board {
//转发规则 const MODE_ZF_RULE = 'MODE_ZF_RULE';
protected $name ;
public function log($str){ echo $str . PHP_EOL; }
/** * 生成转发格式 */ public function jsonFileTozfRule($srcFile){
$this->name = $this->_getName($srcFile);
$srcFile = BASE_DIR . DIRECTORY_SEPARATOR .'json' . DIRECTORY_SEPARATOR . $srcFile; $rawContent = $this->_getNodeInfoFromJsonFile($srcFile); $ruleContent = $this->filterNodeInfo($rawContent,self::MODE_ZF_RULE);
$this->saveToFile( $ruleContent, $fileName = sprintf('%s/rules/%s2sc.csv',BASE_DIR,$this->name) );
$this->log( "file name is " . $fileName); }
public function _getName($srcFileName){ $_t = explode('.',$srcFileName); return current($_t); }
//get v2board node from json file public function _getNodeInfoFromJsonFile($srcFile){ $content = file_get_contents($srcFile); $obj = json_decode($content); return $obj->data; }
public function filterNodeInfo($nodeObj,$mode = 'default'){
switch($mode){
case self::MODE_ZF_RULE: return $this->_modeZfRule($nodeObj); break; default: return $this->_modeDefault($nodeObj); break; }
}
public function _modeDefault($nodeObj){ $tmp = ""; foreach($nodeObj as $item){ if($item -> show != 0 && $item->parent_id != null){ // if(preg_match("/01/",$item->name) || preg_match("/02/",$item->name)){ // $tmp .= sprintf("%s,%s,%s",$item->name,$item->host,$item->port).PHP_EOL; // } $tmp .= sprintf("%s,%s,%s",$item->name,$item->host,$item->port).PHP_EOL; } } return $tmp; }
public function _modeZfRule($nodeObj){ $tmp = ""; $_t_Arr = []; foreach($nodeObj as $item){ if($item->parent_id == null){ $_t_Arr[$item->id] = [ 'host' => $item->host, 'port' => $item->server_port ]; } }
foreach($nodeObj as $item){
if($item->show != 0 && $item->parent_id != null){ $tmp .= sprintf( "%s#%s#%s#%s", $this->_buildRuleName($item), $item->port, $_t_Arr[$item->parent_id]['host'], $_t_Arr[$item->parent_id]['port'] ).PHP_EOL;
} } return $tmp; }
public function _buildRuleName($item){ $_t = explode('.',$item->host); return sprintf("%s-%s",$this->name,current($_t)); }
public function saveToFile($content,$file){ file_put_contents($file,$content); }
}
//使用方法,首先保存V2的node信息 (new V2board()) -> jsonFileTozfRule('mk.json');
|