-
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
Showing
6 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
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,112 @@ | ||
<?php | ||
namespace App\Workerman\{AppName}; | ||
|
||
use \Workerman\Worker; | ||
use \Workerman\Connection\AsyncTcpConnection; | ||
|
||
// 自动加载类 | ||
define('STAGE_INIT', 0); | ||
define('STAGE_ADDR', 1); | ||
define('STAGE_UDP_ASSOC', 2); | ||
define('STAGE_DNS', 3); | ||
define('STAGE_CONNECTING', 4); | ||
define('STAGE_STREAM', 5); | ||
define('STAGE_DESTROYED', -1); | ||
define('CMD_CONNECT', 1); | ||
define('CMD_BIND', 2); | ||
define('CMD_UDP_ASSOCIATE', 3); | ||
define('ADDRTYPE_IPV4', 1); | ||
define('ADDRTYPE_IPV6', 4); | ||
define('ADDRTYPE_HOST', 3); | ||
|
||
$worker = new Worker('tcp://0.0.0.0:1080'); | ||
$worker->onConnect = function($connection) | ||
{ | ||
$connection->stage = STAGE_INIT; | ||
}; | ||
|
||
$worker->onMessage = function($connection, $buffer) | ||
{ | ||
switch($connection->stage) | ||
{ | ||
case STAGE_INIT: | ||
$connection->send("\x05\x00"); | ||
$connection->stage = STAGE_ADDR; | ||
return; | ||
case STAGE_ADDR: | ||
$cmd = ord($buffer[1]); | ||
if($cmd != CMD_CONNECT) | ||
{ | ||
echo "bad cmd $cmd\n"; | ||
$connection->close(); | ||
return; | ||
} | ||
$header_data = parse_socket5_header($buffer); | ||
if(!$header_data) | ||
{ | ||
$connection->close(); | ||
return; | ||
} | ||
$connection->stage = STAGE_CONNECTING; | ||
$remote_connection = new AsyncTcpConnection('tcp://'.$header_data[1].':'.$header_data[2]); | ||
$remote_connection->onConnect = function($remote_connection)use($connection) | ||
{ | ||
$connection->state = STAGE_STREAM; | ||
$connection->send("\x05\x00\x00\x01\x00\x00\x00\x00\x10\x10"); | ||
$connection->pipe($remote_connection); | ||
$remote_connection->pipe($connection); | ||
}; | ||
$remote_connection->connect(); | ||
} | ||
}; | ||
|
||
function parse_socket5_header($buffer) | ||
{ | ||
$addr_type = ord($buffer[3]); | ||
switch($addr_type) | ||
{ | ||
case ADDRTYPE_IPV4: | ||
if(strlen($buffer) < 10) | ||
{ | ||
echo bin2hex($buffer)."\n"; | ||
echo "buffer too short\n"; | ||
return false; | ||
} | ||
$dest_addr = ord($buffer[4]).'.'.ord($buffer[5]).'.'.ord($buffer[6]).'.'.ord($buffer[7]); | ||
$port_data = unpack('n', substr($buffer, -2)); | ||
$dest_port = $port_data[1]; | ||
$header_length = 10; | ||
break; | ||
case ADDRTYPE_HOST: | ||
$addrlen = ord($buffer[4]); | ||
if(strlen($buffer) < $addrlen + 5) | ||
{ | ||
echo $buffer."\n"; | ||
echo bin2hex($buffer)."\n"; | ||
echo "buffer too short\n"; | ||
return false; | ||
} | ||
$dest_addr = substr($buffer, 5, $addrlen); | ||
$port_data = unpack('n', substr($buffer, -2)); | ||
$dest_port = $port_data[1]; | ||
$header_length = $addrlen + 7; | ||
break; | ||
case ADDRTYPE_IPV6: | ||
if(strlen($buffer) < 22) | ||
{ | ||
echo "buffer too short\n"; | ||
return false; | ||
} | ||
echo "todo ipv6\n"; | ||
return false; | ||
default: | ||
echo "unsupported addrtype $addr_type\n"; | ||
return false; | ||
} | ||
return array($addr_type, $dest_addr, $dest_port, $header_length); | ||
} | ||
|
||
if(!defined('GLOBAL_START')) | ||
{ | ||
Worker::runAll(); | ||
} |
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,21 @@ | ||
<?php | ||
namespace App\Workerman\{AppName}; | ||
|
||
use Workerman\Worker; | ||
|
||
// 创建一个Worker监听2345端口,使用http协议通讯 | ||
$http_worker = new Worker("http://0.0.0.0:2345"); | ||
|
||
// 启动4个进程对外提供服务 | ||
$http_worker->count = 4; | ||
|
||
// 接收到浏览器发送的数据时回复hello world给浏览器 | ||
$http_worker->onMessage = function($connection, $data) | ||
{ | ||
// 向浏览器发送hello world | ||
$connection->send('hello world'); | ||
}; | ||
|
||
if(!defined('GLOBAL_START')) { | ||
Worker::runAll(); | ||
} |
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,34 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
class CreateCommand extends BaseCommand | ||
{ | ||
protected $signature = 'worker:create {name} {--type=1}'; | ||
protected $description = 'create workerman app'; | ||
|
||
public function fire() | ||
{ | ||
$workerName = $this->argument('name'); | ||
$config = config('workerman'); | ||
$appPath = $config['app_path']; | ||
if (!is_dir($appPath) && !mkdir($appPath)) { | ||
throw new \Exception("create app directory failed: {$appPath}"); | ||
} | ||
|
||
$workspace = $appPath . DIRECTORY_SEPARATOR . ucfirst($workerName); | ||
if (is_dir($workspace)) { | ||
throw new \Exception("directory exists: {$workspace}"); | ||
} | ||
if (!mkdir($workspace)) { | ||
throw new \Exception("create app directory failed: {$workspace}"); | ||
} | ||
|
||
$type = $this->option('type') == 1 ? 'Workerman' : 'GatewayWorker'; | ||
$tpl = dirname(dirname(dirname(__FILE__))) .DIRECTORY_SEPARATOR.'examples' .DIRECTORY_SEPARATOR. $type .DIRECTORY_SEPARATOR. 'start.tpl'; | ||
$content = str_replace('{AppName}', ucfirst($workerName), file_get_contents($tpl)); | ||
file_put_contents($workspace.DIRECTORY_SEPARATOR.'start.php', $content); | ||
|
||
return true; | ||
} | ||
|
||
} |
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