This repository was archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomm.php
More file actions
205 lines (193 loc) · 4.83 KB
/
Copy pathcomm.php
File metadata and controls
205 lines (193 loc) · 4.83 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* Created by Harry Liu.
* Date: 2019/3/5
* Time: 16:55
* Email: L3478830@163.com
*/
//加在配置文件
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
//定义加密iv
define ("IV",getenv('iv'));
define ('CIPHER',getenv('cipher'));
function isGet(){
return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
}
/**
* Return the length of the given string.
*
* @param string $value
* @return int
*/
function length($value)
{
return mb_strlen($value);
}
/**
* Returns the portion of string specified by the start and length parameters.
*
* @param string $string
* @param int $start
* @param int|null $length
* @return string
*/
function substr_new($string, $start, $length = null)
{
return mb_substr($string, $start, $length, 'UTF-8');
}
/**
* Determine if a given string starts with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function startsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && mb_strpos($haystack, $needle) === 0) {
return true;
}
}
return false;
}
/**
* Determine if a given string ends with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function endsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ((string) $needle === substr_new($haystack, -length($needle))) {
return true;
}
}
return false;
}
if (! function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
if (! function_exists('env')) {
/**
* Gets the value of an environment variable. Supports boolean, empty and null.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return value($default);
}
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return;
}
if (startsWith($value, '"') && endsWith($value, '"')) {
return substr($value, 1, -1);
}
return $value;
}
}
/*
* 加密函数
*/
function ssl_encry($plaintext,$key='5ae1b8a17bad4da4fdac796f64c16ecd'){
if (in_array(CIPHER, openssl_get_cipher_methods()))
{
$ciphertext = openssl_encrypt($plaintext, CIPHER, $key, $options=OPENSSL_RAW_DATA,IV);
return base64_encode($ciphertext);
}
}
/*
* 解密函数
*/
function ssl_decri($encstr,$key='5ae1b8a17bad4da4fdac796f64c16ecd'){
$de64_str=base64_decode($encstr);
if (in_array(CIPHER, openssl_get_cipher_methods())) {
$output = openssl_decrypt($de64_str, CIPHER, $key, $options=OPENSSL_RAW_DATA,$iv=IV);
return $output;
}
}
/*
* 发送数据到bootServer
*/
function send_json($server,$port,$json_str){
$client = new swoole_client(SWOOLE_SOCK_TCP);
$env_key=env('aes_key','5ae1b8a17bad4da4fdac796f64c16ecd');
$secret_key=env('secret_key','1522');
if (!$client->connect($server, $port, -1))
{
exit("connect failed. Error: {$client->errCode}\n");
}
$return_str=$client->recv();
if (ssl_decri($return_str)==$secret_key){
$client->send(ssl_encry($json_str,$env_key));
$client->close();
return true;
}else{
$client->close();
return false;
}
}
/*
* 获取clans 服务器的配置
*/
function clans_get_list($count){
$clan_list=[];
for ($clan=0; $clan<= (int)$count - 1; $clan++){
$repo = env('clans_repo_name_'.strval($clan));
$server_name=env('clans_server_'.strval($clan),'127.0.0.1');
$port =env('clans_server_port_'.strval($clan),5750);
$clans = [
"index" => $clan,
"repo" => $repo,
"server" => $server_name,
"port" => $port
];
array_push($clan_list,$clans);
}
return $clan_list;
}
/*
* 渲染模版
*/
function render($tplpath,$tplfile,$val){
$loader = new \Twig\Loader\FilesystemLoader($tplpath);
$twig = new \Twig\Environment($loader);
return $twig->render($tplfile, ["github"=>$val]);
}
/*
* 写入文件,并设置权限
*/
function write_cmd($filename,$val){
$cmdfilefd = fopen($filename, "w") or die($filename." Cannot write to file!");
fwrite($cmdfilefd,$val);
fclose($cmdfilefd);
chmod($filename,0755);
}