Initial commit
This commit is contained in:
commit
8670eb9d56
9
config.php
Normal file
9
config.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
$pwd = "123456";
|
||||
|
||||
$msg = '这是 iVampireSP.com / Twilight 自用的 API 接口。如果别人分享给您,估计他对你图谋不轨了';
|
||||
|
||||
// redis
|
||||
$redis = new Redis();
|
||||
$redis->pconnect('127.0.0.1', 6379);
|
37
get.php
Normal file
37
get.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
|
||||
$device = $_REQUEST['device'] ?? null;
|
||||
|
||||
if (is_null($device)) {
|
||||
die(json_encode([
|
||||
'status' => 'error',
|
||||
'message' => $msg
|
||||
], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
// 不能超过 128
|
||||
if (strlen($device) > 128) {
|
||||
die(json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'device 不能超过 128 个字符'
|
||||
], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
// get from redis
|
||||
$data = $redis->get(md5($device));
|
||||
|
||||
if (empty($data)) {
|
||||
echo json_encode([
|
||||
'status' => 'empty',
|
||||
'message' => $msg
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
} else {
|
||||
echo $data;
|
||||
$redis->del(md5($device));
|
||||
}
|
||||
|
||||
// delete from redis
|
10
go/go.mod
Normal file
10
go/go.mod
Normal file
@ -0,0 +1,10 @@
|
||||
module ivampiresp.com/rCMD
|
||||
|
||||
go 1.21.0
|
||||
|
||||
require github.com/r3labs/sse/v2 v2.10.0
|
||||
|
||||
require (
|
||||
golang.org/x/net v0.0.0-20191116160921-f9c825593386 // indirect
|
||||
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
|
||||
)
|
15
go/go.sum
Normal file
15
go/go.sum
Normal file
@ -0,0 +1,15 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0=
|
||||
github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20191116160921-f9c825593386 h1:ktbWvQrW08Txdxno1PiDpSxPXG6ndGsfnJjRRtkM0LQ=
|
||||
golang.org/x/net v0.0.0-20191116160921-f9c825593386/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y=
|
||||
gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UDAkHu8BrjI=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
30
go/win.go
Normal file
30
go/win.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/r3labs/sse/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
println("Hello, World!")
|
||||
|
||||
device_id := os.Getenv("DEVICE_ID")
|
||||
if device_id == "" {
|
||||
println("DEVICE_ID env is not set")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for {
|
||||
client := sse.NewClient("https://ivampiresp.com/wp-content/rCMD/sse.php?device=" + device_id)
|
||||
|
||||
client.Subscribe("message", func(msg *sse.Event) {
|
||||
fmt.Println(msg.Data)
|
||||
|
||||
// json
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
31
send.php
Normal file
31
send.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
// get json
|
||||
$json = file_get_contents('php://input');
|
||||
|
||||
// decode json
|
||||
$data = json_decode($json);
|
||||
|
||||
// require json pwd, to, params
|
||||
if (!isset($data->p) || !isset($data->to) || !isset($data->cmd)) {
|
||||
die();
|
||||
}
|
||||
|
||||
if ($data->p !== $pwd) {
|
||||
die();
|
||||
}
|
||||
|
||||
// save to redis
|
||||
$redis->set(md5($data->to), json_encode([
|
||||
'cmd' => $data->cmd,
|
||||
'params' => $data->params ?? []
|
||||
]));
|
||||
|
||||
// publish redis
|
||||
$redis->publish('rCMD_' . md5($data->to), json_encode([
|
||||
'to' => $data->to,
|
||||
'cmd' => $data->cmd,
|
||||
'params' => $data->params ?? []
|
||||
]));
|
87
sse.php
Normal file
87
sse.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
$device = $_REQUEST['device'] ?? null;
|
||||
|
||||
if (is_null($device)) {
|
||||
die(json_encode([
|
||||
'status' => 'error',
|
||||
'message' => $msg
|
||||
], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
if (strlen($device) > 128) {
|
||||
die(json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'device 不能超过 128 个字符'
|
||||
], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
header('X-Accel-Buffering: no');
|
||||
header('Content-Type: text/event-stream');
|
||||
header('Cache-Control: no-cache');
|
||||
ob_end_clean();
|
||||
ob_implicit_flush(1);
|
||||
|
||||
$data = [
|
||||
"time" => time(),
|
||||
"status" => 'success',
|
||||
"cmd" => null,
|
||||
"params" => []
|
||||
];
|
||||
returnEventData($data);
|
||||
|
||||
|
||||
subscribeRedis($device);
|
||||
|
||||
while (1) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
function subscribeRedis($to)
|
||||
{
|
||||
$channel = 'rCMD_' . md5($to);
|
||||
|
||||
global $redis;
|
||||
$redis->subscribe([$channel], function ($redis, $channel, $msg) {
|
||||
|
||||
$msg = json_decode($msg, true);
|
||||
if (!$msg) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($msg["cmd"] == 'close') {
|
||||
$data = [
|
||||
"status" => "close",
|
||||
"message" => '连接已关闭'
|
||||
];
|
||||
returnEventData($data);
|
||||
die();
|
||||
}
|
||||
|
||||
$msg["status"] = "success";
|
||||
returnEventData($msg);
|
||||
});
|
||||
}
|
||||
|
||||
function returnEventData($returnData, $event = 'message', $id = 0, $retry = 0)
|
||||
{
|
||||
|
||||
$str = '';
|
||||
if ($id > 0) {
|
||||
$str .= "id: {$id}" . PHP_EOL;
|
||||
}
|
||||
if ($event) {
|
||||
$str .= "event: {$event}" . PHP_EOL;
|
||||
}
|
||||
if ($retry > 0) {
|
||||
$str .= "retry: {$retry}" . PHP_EOL;
|
||||
}
|
||||
if (is_array($returnData)) {
|
||||
$returnData = json_encode($returnData);
|
||||
}
|
||||
$str .= "data: {$returnData}" . PHP_EOL;
|
||||
$str .= PHP_EOL;
|
||||
echo $str;
|
||||
}
|
Loading…
Reference in New Issue
Block a user