forked from LinkedDestiny/swoole-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e5f320
commit 9acaa15
Showing
10 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ [email protected] (Lancelot)<br> | |
2. [Swoole的Task使用以及swoole_client](02-Swoole的Task使用以及swoole_client.md) | ||
3. [Timer定时器、心跳检测及Task进阶实例:mysql连接池](03.Timer定时器、心跳检测及Task进阶实例:mysql连接池.md) | ||
4. [Swoole多端口监听、热重启以及Timer进阶:简单crontab](#04.Swoole多端口监听、热重启以及Timer进阶:简单crontab.md) | ||
5. [Swoole的自定义协议功能的使用](#05.Swoole的自定义协议功能的使用.md) | ||
5. [Swoole的自定义协议功能的使用](#05.Swoole的自定义协议功能的使用.md) | ||
6. [Swoole的文件操作及进阶实例:简易FTP](#06.Swoole的文件操作及进阶实例:简易FTP.md) |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIICVDCCAb2gAwIBAgIJAPJObLqhaQiKMA0GCSqGSIb3DQEBCwUAMEMxCzAJBgNV | ||
BAYTAkNOMREwDwYDVQQIDAhaaGVKaWFuZzEhMB8GA1UECgwYSW50ZXJuZXQgV2lk | ||
Z2l0cyBQdHkgTHRkMB4XDTE0MTExMTAyNDQyOVoXDTE0MTIxMTAyNDQyOVowQzEL | ||
MAkGA1UEBhMCQ04xETAPBgNVBAgMCFpoZUppYW5nMSEwHwYDVQQKDBhJbnRlcm5l | ||
dCBXaWRnaXRzIFB0eSBMdGQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKdz | ||
S5fu/nZ8r9ihVc9basNPg26Ehk81mPHNGQ3Hh/P+aily3leVgXJgU6dCwNNYiSYN | ||
ieme0x6zo/WZ2dZmbQBOvKsZQ7URM/PaqnlMwXLJnJ85cTRz4DZwQ5yiDVyEwjO0 | ||
BHrw+XHG/dsmWLGCsQnLC+QJ4Y6b3mFKN+/I3UW/AgMBAAGjUDBOMB0GA1UdDgQW | ||
BBRZoV1LTTZJot1b+L0fXd4Lr1y8sjAfBgNVHSMEGDAWgBRZoV1LTTZJot1b+L0f | ||
Xd4Lr1y8sjAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4GBAENuaVpLoeR1 | ||
SWC+08jid82f8DAapyVNl0wG2cS0VSjCn26Wer2ci8qCAtoGcn9vxZcrPhPOZBjA | ||
pzMKKERvmC4qM6xJGapdNAJYRhSmyDh+cRtkJKAWWMd0XweEgw4EHOxnLkRR7BjN | ||
4oV9rtYNive56aZnblfq2s/Hx2vZuznY | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIICXQIBAAKBgQCnc0uX7v52fK/YoVXPW2rDT4NuhIZPNZjxzRkNx4fz/mopct5X | ||
lYFyYFOnQsDTWIkmDYnpntMes6P1mdnWZm0ATryrGUO1ETPz2qp5TMFyyZyfOXE0 | ||
c+A2cEOcog1chMIztAR68Plxxv3bJlixgrEJywvkCeGOm95hSjfvyN1FvwIDAQAB | ||
AoGAE077J4DafMUx7bPk1i0/aVgQ2ryTVCzk/vTcb7dTWPI8QZ1KQCUwMQUkgEP1 | ||
QimSXv3uqi6zP3YNcPOVp+BVrySZX61JzfT+loOl0Z9f0bsM9Oqu36vawEMt6dF6 | ||
uwBOW3AQWy6vg2UEk/157R1agZyHn7w49y6oPb7Rp+Zm6CkCQQDYc2iXs5Raggrd | ||
snrUYjdzkHn32Z0343QdbNxJk0gjccphVGEypDje3z2wBCVuvU/sXYL5eScUVJla | ||
3KHq+g1TAkEAxgveCq0eAqj7yAJrK92EeoIrkb/kG58fSAbJlJaE4XRUGjlKHeg0 | ||
/jUkbAA+dXWs04/ZF24mTBsG8Swf5S1sZQJBAMROtzeOySfk9VHoQ3pHsZ8AE350 | ||
QSClC3R8fLXF/BEZbMElNBon0uRfd6UhvYlX7RqkHNuBw62upH9BxyBDtL0CQBW6 | ||
O5QERiSYkXbx/PwFA4HFQi5tVQNKw83NINRyZwWtE4UFAAKj9RxmjCZlKHUOgfV7 | ||
9J9DcGQtGufepWkpQJkCQQC4+jBaeGHkxnk1bvTFhXGwnobnIbTA+1CV9iQHiaEw | ||
T9vr8Hk+2aUj99la32LmuFbznHQwMH0Ry449ex4T2Q2O | ||
-----END RSA PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#include <malloc.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <resolv.h> | ||
#include <netdb.h> | ||
#include <openssl/ssl.h> | ||
#include <openssl/err.h> | ||
|
||
#define FAIL -1 | ||
|
||
int OpenConnection(const char *hostname, int port) | ||
{ | ||
int sd; | ||
struct hostent *host; | ||
struct sockaddr_in addr; | ||
if ((host = gethostbyname(hostname)) == NULL) | ||
{ | ||
printf("Eroor: %s\n", hostname); | ||
perror(hostname); | ||
abort(); | ||
} | ||
sd = socket(PF_INET, SOCK_STREAM, 0); | ||
bzero(&addr, sizeof(addr)); | ||
addr.sin_family = AF_INET; | ||
addr.sin_port = htons(port); | ||
addr.sin_addr.s_addr = *(long*) (host->h_addr); | ||
if (connect(sd, (struct sockaddr*) &addr, sizeof(addr)) != 0) | ||
{ | ||
close(sd); | ||
perror(hostname); | ||
abort(); | ||
} | ||
return sd; | ||
} | ||
|
||
SSL_CTX* InitCTX(void) | ||
{ | ||
const SSL_METHOD *method; | ||
SSL_CTX *ctx; | ||
OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */ | ||
SSL_load_error_strings(); /* Bring in and register error messages */ | ||
method = SSLv3_client_method(); /* Create new client-method instance */ | ||
ctx = SSL_CTX_new(method); /* Create new context */ | ||
if (ctx == NULL) | ||
{ | ||
ERR_print_errors_fp(stderr); | ||
printf("Eroor: %s\n", stderr); | ||
abort(); | ||
} | ||
return ctx; | ||
} | ||
|
||
void ShowCerts(SSL* ssl) | ||
{ | ||
X509 *cert; | ||
char *line; | ||
|
||
cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */ | ||
if (cert != NULL) | ||
{ | ||
printf("Server certificates:\n"); | ||
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); | ||
printf("Subject: %s\n", line); | ||
free(line); | ||
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); | ||
printf("Issuer: %s\n", line); | ||
free(line); | ||
X509_free(cert); | ||
} | ||
else | ||
printf("No certificates.\n"); | ||
} | ||
|
||
int main(int count, char *strings[]) | ||
{ | ||
SSL_CTX *ctx; | ||
int server; | ||
SSL *ssl; | ||
char buf[1024]; | ||
int bytes; | ||
char *hostname, *portnum; | ||
if (count != 3) | ||
{ | ||
printf("usage: %s <hostname> <portnum>\n", strings[0]); | ||
exit(0); | ||
} | ||
SSL_library_init(); | ||
hostname = strings[1]; | ||
portnum = strings[2]; | ||
ctx = InitCTX(); | ||
server = OpenConnection(hostname, atoi(portnum)); | ||
ssl = SSL_new(ctx); /* create new SSL connection state */ | ||
SSL_set_fd(ssl, server); /* attach the socket descriptor */ | ||
if (SSL_connect(ssl) == FAIL) /* perform the connection */ | ||
{ | ||
printf("Eroor: %s\n", stderr); | ||
ERR_print_errors_fp(stderr); | ||
} | ||
else | ||
{ | ||
char *msg = "HelloWorld"; | ||
printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); | ||
ShowCerts(ssl); /* get any certs */ | ||
SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */ | ||
bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */ | ||
buf[bytes] = '\0'; | ||
printf("Received %d : %s \n", strlen(buf), buf); | ||
SSL_free(ssl); /* release connection state */ | ||
} | ||
close(server); /* close socket */ | ||
SSL_CTX_free(ctx); /* release context */ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501 ,SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL ); | ||
|
||
$dir = __DIR__ . "/cert/"; | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2, | ||
'open_ssl'=> true, | ||
'ssl_cert_file' => $dir . "test.crt", | ||
'ssl_key_file' => $dir . "test.key" | ||
)); | ||
|
||
$this->serv->on('Start', array($this, 'onStart')); | ||
$this->serv->on('Connect', array($this, 'onConnect')); | ||
$this->serv->on('Receive', array($this, 'onReceive')); | ||
$this->serv->on('Close', array($this, 'onClose')); | ||
|
||
$this->serv->start(); | ||
} | ||
|
||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
} | ||
|
||
public function onConnect( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} connect\n"; | ||
|
||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
echo "Get Message From Client {$fd}:{$data}\n"; | ||
$this->serv->send( $fd, "Hello client\n"); | ||
} | ||
|
||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
} | ||
|
||
new Server(); |