44use function Swoole \Timer ;
55declare (ticks=1 )
66date_default_timezone_set ('Asia/Shanghai ' );
7- set_time_limit (0 );
8- ini_set ('memory_limit ' , '-1 ' );
97require './config.php ' ;
108const PHPOBAVERSION = '1.5.0 ' ;
119const VERSION = '1.10.5 ' ;
2321$ enable = false ;
2422echo "OpenBmclApionPHP v " . PHPOBAVERSION . "- " . VERSION . PHP_EOL ;
2523run (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+ }
2733 function registerSigintHandler () {
28- global $ tokentimerid ;
2934 $ 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 ();
4337 });
4438 }
4539 //获取初次Token
@@ -55,7 +49,49 @@ function registerSigintHandler() {
5549 });
5650 registerSigintHandler ();
5751 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+
5995 //下载文件列表
6096 $ cluster = new cluster ($ tokendata ['token ' ],VERSION );
6197 $ files = $ cluster ->getFileList ();
@@ -69,6 +105,7 @@ function registerSigintHandler() {
69105 elseif ($ config ['file ' ]['check ' ] == "exists " ){
70106 $ Missfile = $ FilesCheck ->FilesCheckerexists ();
71107 }
108+ //循环到没有Missfile这个变量
72109 if (is_array ($ Missfile )){
73110 mlog ("缺失/损坏 " .count ($ Missfile )."个文件 " );
74111 while (is_array ($ Missfile )){
@@ -100,44 +137,7 @@ function registerSigintHandler() {
100137 }
101138 global $ shouldExit ;
102139 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节点
141141 $ socketio ->enable ($ config ['cluster ' ]['public_host ' ],$ config ['cluster ' ]['public_port ' ],$ config ['cluster ' ]['byoc ' ]);
142142 }
143143});
0 commit comments