Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 3db4970

Browse files
committed
✨ 支持获取证书/初步添加对socket.io的支持
1 parent b078f8b commit 3db4970

File tree

2 files changed

+131
-4
lines changed

2 files changed

+131
-4
lines changed

inc/socketio.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
use Swoole\Coroutine;
3+
use Swoole\Coroutine\Http\Client;
4+
class socketio {
5+
private $url;
6+
private $token;
7+
private $client;
8+
private $data;
9+
private $certdata;
10+
private $Connected = false;
11+
public function __construct($url,$token) {
12+
$this->url = $url;
13+
$this->token = $token;
14+
}
15+
public function connect() {
16+
$this->client = $client = new Client($this->url, 443, true);
17+
$ret = $client->upgrade('/socket.io/?EIO=4&transport=websocket');
18+
$auth = [
19+
'token' => $this->token
20+
];
21+
if ($ret) {
22+
$client->push('40'.json_encode($auth));
23+
}
24+
25+
while(true) {
26+
$alldata = $client->recv(1.5);
27+
if (!is_bool($alldata)){
28+
$this->data = $data = $alldata->data;
29+
preg_match('/^\d+/', $data, $code);
30+
if ($code[0] == '0'){
31+
$jsondata = json_decode(substr($data, strlen($code[0])),true);
32+
mlog("[socket.io]Connected sid:{$jsondata['sid']} pingInterval:{$jsondata['pingInterval']} pingTimeout:{$jsondata['pingTimeout']}",1);
33+
}
34+
if ($code[0] == '40'){
35+
$jsondata = json_decode(substr($data, strlen($code[0])),true);
36+
mlog("[socket.io]Connected sid:{$jsondata['sid']}",1);
37+
mlog("已连接主控");
38+
$this->Connected = true;
39+
}
40+
if ($code[0] == '42'){
41+
$data = substr($data, strlen($code[0]));
42+
mlog("[socket.io]Got data {$data}");
43+
}
44+
if ($code[0] == '2'){
45+
$client->push('3');
46+
mlog("[socket.io]Sending PONG",1);
47+
}
48+
if ($code[0] == '41'){
49+
mlog("[socket.io]Close Connection");
50+
$client->close();
51+
return;
52+
}
53+
if ($code[0] == '430'){
54+
$jsondata = json_decode(substr($data, strlen($code[0])),true);
55+
$this->certdata = $jsondata;
56+
//mlog("[socket.io]Got MESSAGE {$data}",1);
57+
}
58+
//var_dump($data);
59+
Coroutine::sleep(0.1);
60+
}
61+
global $shouldExit;
62+
if ($shouldExit) {
63+
mlog("[socket.io]Close Connection");
64+
$client->close();
65+
return;
66+
}
67+
}
68+
}
69+
public function Getcert() {
70+
$time = 0;
71+
while ($time !== 30){
72+
if(isset($this->certdata)){
73+
return $this->certdata;
74+
}
75+
}
76+
if ($time = 30){
77+
mlog("Getcert Connected Overtime",2);
78+
return(false);
79+
}
80+
81+
}
82+
public function ack($event,$data = "") {
83+
$time = 0;
84+
while ($time !== 30){
85+
Coroutine::sleep(1);
86+
$time++;
87+
if ($this->Connected){
88+
$senddata = json_encode([$event,$data]);
89+
return($this->client->push('420'.$senddata));
90+
}
91+
}
92+
if ($time = 30){
93+
mlog("[socket.io]ACK Connected Overtime",2);
94+
return(false);
95+
}
96+
}
97+
public function Getclient() {
98+
return $this->client;
99+
}
100+
}

main.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
const OPENBMCLAPIURL = 'openbmclapi.bangbang93.com';
1313
global $tokendata;
1414
$list = glob('./inc/*.php');
15-
foreach ($list as $file) {
16-
$file = explode('/', $file)['2'];
17-
require './inc/' . $file;
18-
}
15+
foreach ($list as $file) {
16+
$file = explode('/', $file)['2'];
17+
require './inc/' . $file;
18+
}
1919
echo"OpenBmclApionPHP v0.0.1-dev". PHP_EOL;
2020
run(function()use ($config){
2121
//注册信号处理器
@@ -77,4 +77,31 @@ function registerSigintHandler() {
7777
mlog("检查文件完毕,没有缺失/损坏");
7878
}
7979
}
80+
global $shouldExit;
81+
if (!is_array($Missfile) && !$shouldExit){//判断Missfile是否为空和是否是主动退出
82+
$socketio = new socketio(OPENBMCLAPIURL,$tokendata['token']);
83+
mlog("正在连接主控");
84+
Coroutine::create(function () use ($socketio){
85+
$socketio->connect();
86+
});
87+
Coroutine::sleep(1);
88+
if (file_exists('./cert/'.$config['CLUSTER_ID'].'.cert') && file_exists('./cert/'.$config['CLUSTER_ID'].'.key')) {
89+
90+
} else {
91+
mlog("正在获取证书");
92+
if (!file_exists("./cert")) {
93+
mkdir("./cert",0777,true);
94+
}
95+
$socketio->ack("request-cert");
96+
Coroutine::sleep(1);
97+
$allcert = $socketio->Getcert();
98+
mlog("已获取证书,到期时间{$allcert['0']['1']['expires']}");
99+
$cert = fopen('./cert/'.$config['CLUSTER_ID'].'.cert', 'w');
100+
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
101+
fclose($cert);
102+
$cert = fopen('./cert/'.$config['CLUSTER_ID'].'.key', 'w');
103+
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
104+
fclose($cert);
105+
}
106+
}
80107
});

0 commit comments

Comments
 (0)