-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
send.php
99 lines (92 loc) · 3.41 KB
/
send.php
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
<?php
/**
* -------------------------------------------------------------------
* default plaintext TCP protocol
* -------------------------------------------------------------------
*/
// email sending with send confirmation (plaintext TCP)
function sendWithResponse(string $ip): bool {
try {
$response = '';
$fp = fsockopen("tcp://127.0.0.1", 3000, $errno, $errstr, 10);
if (stream_set_timeout($fp, 10)) {
if (!$fp) {
error_log("open TCP sock failed ({$errno}: {$errstr})");
return false;
} else {
fwrite($fp, json_encode([
'ip' => $ip
]));
while (!feof($fp)) {
$buffer = fread($fp, 4096);
$response .= $buffer;
if (strlen($buffer) < 4096) {
break;
}
}
fclose($fp);
}
$result = json_decode($response, true);
if (isset($result['status']) && $result['status'] === 'success') {
return $result['data'];
} else {
return false;
}
} else {
error_log("stream_set_timeout failed");
return false;
}
} catch (\Throwable $e) {
error_log("send exception: {$e->getMessage()})");
return false;
}
}
/**
* -------------------------------------------------------------------
* using SSL protocol
* -------------------------------------------------------------------
*/
// email sending with send confirmation (SSL)
function sendWithResponseOverSSL(string $ip): bool {
try {
$response = '';
$context = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
$fp = stream_socket_client('ssl://127.0.0.1:3000', $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $context);
if (stream_set_timeout($fp, 10)) {
if (!$fp) {
error_log("open TCP sock failed ({$errno}: {$errstr})");
return false;
} else {
fwrite($fp, json_encode([
'ip' => $ip
]));
while (!feof($fp)) {
$buffer = fread($fp, 4096);
$response .= $buffer;
if (strlen($buffer) < 4096) {
break;
}
}
fclose($fp);
}
$result = json_decode($response, true);
if (isset($result['status']) && $result['status'] === 'success') {
return $result['data'];
} else {
return false;
}
} else {
error_log("stream_set_timeout failed");
return false;
}
} catch (\Throwable $e) {
error_log("send exception: {$e->getMessage()})");
return false;
}
}