4
4
use function Swoole \Timer ;
5
5
declare (ticks=1 )
6
6
date_default_timezone_set ('Asia/Shanghai ' );
7
- set_time_limit (0 );
8
- ini_set ('memory_limit ' , '-1 ' );
9
7
require './config.php ' ;
10
8
const PHPOBAVERSION = '1.5.0 ' ;
11
9
const VERSION = '1.10.5 ' ;
23
21
$ enable = false ;
24
22
echo "OpenBmclApionPHP v " . PHPOBAVERSION . "- " . VERSION . PHP_EOL ;
25
23
run (function ()use ($ config ){
26
- //注册信号处理器
24
+ //注册信号处理器、
25
+ function exits () {
26
+ global $ shouldExit ;
27
+ global $ tokentimerid ;
28
+ $ shouldExit = true ; // 设置退出标志
29
+ Swoole \Timer::clear ($ tokentimerid );
30
+ echo PHP_EOL ;
31
+ mlog ("正在退出... " );
32
+ }
27
33
function registerSigintHandler () {
28
- global $ tokentimerid ;
29
34
$ shouldExit = false ; // 初始化为false
30
- Swoole \Process::signal (SIGINT , function ($ signo ) use ($ tokentimerid ) {
31
- try {
32
- global $ shouldExit ;
33
- $ shouldExit = true ; // 设置退出标志
34
- Swoole \Timer::clear ($ tokentimerid );
35
- echo PHP_EOL ;
36
- mlog ("正在退出... " );
37
- exit ();
38
- } catch (\Swoole \ExitException $ e ) {
39
- //var_dump($e->getMessage());
40
- //var_dump($e->getStatus() === 1);
41
- //var_dump($e->getFlags() === SWOOLE_EXIT_IN_COROUTINE);
42
- }
35
+ Swoole \Process::signal (SIGINT , function ($ signo ){
36
+ exits ();
43
37
});
44
38
}
45
39
//获取初次Token
@@ -55,7 +49,49 @@ function registerSigintHandler() {
55
49
});
56
50
registerSigintHandler ();
57
51
mlog ("Timer start on ID {$ tokentimerid }" ,1 );
58
-
52
+ //建立socketio连接主控
53
+ $ socketio = new socketio (OPENBMCLAPIURL ,$ tokendata ['token ' ],$ config ['advanced ' ]['keepalive ' ]);
54
+ mlog ("正在连接主控 " );
55
+ Coroutine::create (function () use ($ socketio ){
56
+ $ socketio ->connect ();
57
+ });
58
+ Coroutine::sleep (1 );
59
+ //获取证书
60
+ $ socketio ->ack ("request-cert " );
61
+ Coroutine::sleep (1 );
62
+ $ allcert = $ socketio ->Getcert ();
63
+ //写入证书并且是否损坏
64
+ if (!file_exists ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' ) && !file_exists ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' )) {
65
+ mlog ("正在获取证书 " );
66
+ if (!file_exists ("./cert " )) {
67
+ mkdir ("./cert " ,0777 ,true );
68
+ }
69
+ mlog ("已获取证书,到期时间 {$ allcert ['0 ' ]['1 ' ]['expires ' ]}" );
70
+ $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' , 'w ' );
71
+ $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['cert ' ]);
72
+ fclose ($ cert );
73
+ $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' , 'w ' );
74
+ $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['key ' ]);
75
+ fclose ($ cert );
76
+ }
77
+ $ crt = file_get_contents ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' );
78
+ if ($ crt !== $ allcert ['0 ' ]['1 ' ]['cert ' ]) {
79
+ mlog ("证书损坏/过期 " );
80
+ mlog ("已获取新的证书,到期时间 {$ allcert ['0 ' ]['1 ' ]['expires ' ]}" );
81
+ $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' , 'w ' );
82
+ $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['cert ' ]);
83
+ fclose ($ cert );
84
+ $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' , 'w ' );
85
+ $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['key ' ]);
86
+ fclose ($ cert );
87
+ }
88
+ //启动http服务器
89
+ global $ httpserver ;
90
+ $ httpserver = new fileserver ($ config ['cluster ' ]['host ' ],$ config ['cluster ' ]['port ' ],$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' ,$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' ,$ config ['cluster ' ]['CLUSTER_SECRET ' ]);
91
+ Coroutine::create (function () use ($ config ,$ httpserver ){
92
+ $ httpserver ->startserver ();
93
+ });
94
+
59
95
//下载文件列表
60
96
$ cluster = new cluster ($ tokendata ['token ' ],VERSION );
61
97
$ files = $ cluster ->getFileList ();
@@ -69,6 +105,7 @@ function registerSigintHandler() {
69
105
elseif ($ config ['file ' ]['check ' ] == "exists " ){
70
106
$ Missfile = $ FilesCheck ->FilesCheckerexists ();
71
107
}
108
+ //循环到没有Missfile这个变量
72
109
if (is_array ($ Missfile )){
73
110
mlog ("缺失/损坏 " .count ($ Missfile )."个文件 " );
74
111
while (is_array ($ Missfile )){
@@ -100,44 +137,7 @@ function registerSigintHandler() {
100
137
}
101
138
global $ shouldExit ;
102
139
if (!is_array ($ Missfile ) && !$ shouldExit ){//判断Missfile是否为空和是否是主动退出
103
- $ socketio = new socketio (OPENBMCLAPIURL ,$ tokendata ['token ' ],$ config ['advanced ' ]['keepalive ' ]);
104
- mlog ("正在连接主控 " );
105
- Coroutine::create (function () use ($ socketio ){
106
- $ socketio ->connect ();
107
- });
108
- Coroutine::sleep (1 );
109
- $ socketio ->ack ("request-cert " );
110
- Coroutine::sleep (1 );
111
- $ allcert = $ socketio ->Getcert ();
112
- if (!file_exists ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' ) && !file_exists ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' )) {
113
- mlog ("正在获取证书 " );
114
- if (!file_exists ("./cert " )) {
115
- mkdir ("./cert " ,0777 ,true );
116
- }
117
- mlog ("已获取证书,到期时间 {$ allcert ['0 ' ]['1 ' ]['expires ' ]}" );
118
- $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' , 'w ' );
119
- $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['cert ' ]);
120
- fclose ($ cert );
121
- $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' , 'w ' );
122
- $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['key ' ]);
123
- fclose ($ cert );
124
- }
125
- $ crt = file_get_contents ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' );
126
- if ($ crt !== $ allcert ['0 ' ]['1 ' ]['cert ' ]) {
127
- mlog ("证书损坏/过期 " );
128
- mlog ("已获取新的证书,到期时间 {$ allcert ['0 ' ]['1 ' ]['expires ' ]}" );
129
- $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' , 'w ' );
130
- $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['cert ' ]);
131
- fclose ($ cert );
132
- $ cert = fopen ('./cert/ ' .$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' , 'w ' );
133
- $ Writtencert = fwrite ($ cert , $ allcert ['0 ' ]['1 ' ]['key ' ]);
134
- fclose ($ cert );
135
- }
136
- global $ httpserver ;
137
- $ httpserver = new fileserver ($ config ['cluster ' ]['host ' ],$ config ['cluster ' ]['port ' ],$ config ['cluster ' ]['CLUSTER_ID ' ].'.crt ' ,$ config ['cluster ' ]['CLUSTER_ID ' ].'.key ' ,$ config ['cluster ' ]['CLUSTER_SECRET ' ]);
138
- Coroutine::create (function () use ($ config ,$ httpserver ){
139
- $ httpserver ->startserver ();
140
- });
140
+ //enable节点
141
141
$ socketio ->enable ($ config ['cluster ' ]['public_host ' ],$ config ['cluster ' ]['public_port ' ],$ config ['cluster ' ]['byoc ' ]);
142
142
}
143
143
});
0 commit comments