-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.php
150 lines (124 loc) · 4.16 KB
/
common.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
function get_config($path = null)
{
if(is_null($path)){
$path = __DIR__."/config/config.json";
}
if(file_exists($path)){
$content = file_get_contents($path);
$config = json_decode($content,true);
$config = check_config($config);
$config = check_config_port($config);
return $config;
}else{
throw new \Exception('配置文件不存在');
}
}
/**
* check_config_port
* 检查端口是否重复
* @param mixed $config
* @return mixed
*/
function check_config_port($config)
{
$config = check_config($config);
if(isset($config['nat_list'])){
$local_used_host_list = [];
$server_used_host_list = [];
foreach ($config['nat_list'] as $c_key => $c_value) {
$local_host = "{$c_value['local_port']}:{$c_value['local_port']}";
if(!in_array($local_host,$local_used_host_list)){
$local_used_host_list[] = $local_host;
}else{
throw new \Exception("客户端转发地址配置项重复:{$local_host}");
}
$server_host = "{$c_value['server_port']}:{$c_value['server_port']}";
if(!in_array($server_host,$server_used_host_list)){
$server_used_host_list[] = $server_host;
}else{
throw new \Exception("服务端转发地址配置项重复:{$local_host}");
}
}
}
return $config;
}
/**
* check_config
* 检查配置完整性
* @param mixed $config
* @return mixed
*/
function check_config($config){
//必选配置
$tartget_key_array = [
'local_ip'=>'',
'local_port'=>'',
'server_ip'=>'',
'server_port'=>'',
];
// 是否设置多个服务
if(isset($config['nat_list'])){
foreach ($config['nat_list'] as $c_key => $c_value) {
// nat_list里没有设置但是应当设置的key
$not_set_key_list = array_diff_key($tartget_key_array,$c_value);
foreach ($not_set_key_list as $n_key => $n_value) {
if(!isset($config[$n_key])){
// nat_list没有,一级配置也没有的设置,错误!
throw new \Exception("一级和二级配置缺少配置项:{$n_key}.只有一级配置完整,二级配置项才可以省略.");
}else{
//把总设置赋给nat_list的设置项里
$config['nat_list'][$c_key][$n_key] = $config[$n_key];
}
if(!isset($config['nat_list'][$c_key]['name'])){
$config['nat_list'][$c_key]['name'] = $config['nat_list'][$c_key]['local_port']."<->".$config['nat_list'][$c_key]['server_port'];
}
if(!isset($config['nat_list'][$c_key]['password'])){
$config['nat_list'][$c_key]['password'] = "phpnb";
}
if(!isset($config['nat_list'][$c_key]['channel_port'])){
$config['nat_list'][$c_key]['channel_port'] = 2206;
}
}
}
}else{
// 如果只是单个服务,那么一级配置一定要齐全
foreach ($tartget_key_array as $k_key => $k_value) {
if(!isset($config[$k_key])){
throw new \Exception('未配置选项:'.$k_key.".windows下必须配齐所有一级配置.");
}
}
if(!isset($config['name'])){
$config['name'] = $config['local_port']."<->".$config['server_port'];
}
if(!isset($config['password'])){
$config['password'] = "phpnb";
}
if(!isset($config['channel_port'])){
$config['channel_port'] = "2206";
}
}
return $config;
}
/**
* 检查端口是否可以被绑定
* @author flynetcn
*/
function check_port_bindable($host, $port, &$errno=null, &$errstr=null)
{
$socket = stream_socket_server("tcp://$host:$port", $errno, $errstr);
if (!$socket) {
return false;
}
fclose($socket);
unset($socket);
return true;
}
function is_win()
{
if (DIRECTORY_SEPARATOR === '\\') {
return true;
}else{
return false;
}
}