Commit 1ed38de1 by BANice

平台认证和入网请求完成,env文件设置ip和端口,启动文件

parent 332998ea
GATEWAY_IP=127.0.0.1
GATEWAY_PORT=8282
GATEWAY_START_PORT=4000
GATEWAY_COUNT=1
GATEWAY_NAME=cdz
REGISTER_IP=
BOOT_FILE=
\ No newline at end of file
REGISTER_PORT=1239
REGISTER_IP=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
START_FILES=start_businessworker
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/monolog/monolog" />
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/log" />
<excludeFolder url="file://$MODULE_DIR$/vendor/workerman/gateway-worker" />
<excludeFolder url="file://$MODULE_DIR$/vendor/workerman/mysql" />
<excludeFolder url="file://$MODULE_DIR$/vendor/workerman/workerman" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
......
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<PHPCodeStyleSettings>
<option name="ALIGN_KEY_VALUE_PAIRS" value="true" />
<option name="COMMA_AFTER_LAST_ARRAY_ELEMENT" value="true" />
<option name="ELSE_IF_STYLE" value="SEPARATE" />
<option name="BLANK_LINES_BEFORE_RETURN_STATEMENT" value="1" />
</PHPCodeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpIncludePathManager">
<include_path>
<path value="$PROJECT_DIR$/vendor/psr/log" />
<path value="$PROJECT_DIR$/vendor/monolog/monolog" />
<path value="$PROJECT_DIR$/vendor/workerman/gateway-worker" />
<path value="$PROJECT_DIR$/vendor/workerman/mysql" />
<path value="$PROJECT_DIR$/vendor/workerman/workerman" />
<path value="$PROJECT_DIR$/vendor/composer" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.2" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -18,10 +18,10 @@ use Monolog\Logger;
/**
*
* @method $this debug($info,$contenxt=[])
* @method $this warn($info,$context=[])
* @method $this alert($info,$context=[])
* @method $this error($info,$context=[])
* @method $this debug($info, $contenxt = [])
* @method $this warn($info, $context = [])
* @method $this alert($info, $context = [])
* @method $this error($info, $context = [])
*
* Class Controller
* @package Application
......@@ -101,6 +101,7 @@ class Controller
* @var Response
*/
protected $response;
/**
*
* Controller constructor.
......@@ -131,10 +132,11 @@ class Controller
public function __destruct()
{
if(count($this->logRecords) != 0 )
{
if (count($this->logRecords) != 0) {
}
}
/**
......@@ -150,10 +152,11 @@ class Controller
foreach ($map as $key => $length) {
$length *= 2;
$parameters[$key] = substr($data, $start, $length);
if(property_exists($this,$key))
{
if (property_exists($this, $key)) {
$this->$key = $parameters[$key];
}
......@@ -271,24 +274,31 @@ class Controller
public function __call($name, $arguments)
{
if(in_array($name,['debug','error','warn','alert']))
{
if(count($arguments) === 2)
{
$logRecord = new LogRecord($name,$arguments[0]);
if (in_array($name, ['debug', 'error', 'warn', 'alert'])) {
if (count($arguments) === 2) {
$logRecord = new LogRecord($name, $arguments[0]);
}else{
} else {
$logRecord = new LogRecord($name,$arguments[0],$arguments[1]);
$logRecord = new LogRecord($name, $arguments[0], $arguments[1]);
}
array_push($this->logRecords,$logRecord);
array_push($this->logRecords, $logRecord);
}
}
public function get($key)
{
if (!$this->request instanceof Request) {
$this->request = $this->getRequest();
}
return $this->request->get($key);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/20
* Time: 下午1:47
*/
namespace Application\Controller;
use Application\Controller;
use GatewayWorker\Lib\Gateway;
class AccessController extends Controller
{
protected $parameterMap = [
'02' => [
'mac' => 6
]
];
public function access()
{
if($_SESSION['certified'] === true)
{
$this->response->send('02');
}else{
Gateway::closeClient($this->clientId);
}
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ namespace Application\Controller;
use Application\Controller;
use Lib\Aes;
class CertifyController extends Controller
{
......@@ -17,13 +18,30 @@ class CertifyController extends Controller
protected $parameterMap = [
'01' => [
'mac' => 12
'mac' => 6
]
];
public function certify()
{
$nonce = $this->getNonce();
$mac =$this->getRequest()->get('mac');
//todo 验证mac地址是否存在,从数据库中获取producter_id生产sign
$_SESSION['gateway_mac'] = $mac;
$_SESSION['certified'] = true;
$productId = 'A22E4F4CDB05C460E450E46241DD6F89';
$sign = bin2hex(Aes::encrypt($nonce,$productId));
$sign = substr($sign,0,8);
$this->response->send('01',$sign);
}
......
......@@ -34,7 +34,7 @@ class Events
{
static::$redis = new Redis();
static::$redis = new \Redis();
static::$redis->pconnect(Env::get('REDIS_HOST'), Env::get('REDIS_PORT'), 0);
......@@ -49,7 +49,7 @@ class Events
static::$redis->select(4);
static::$db = new Connection(Env::get('DB_HOST'), Env::get('DB_PORT'), Env::get('DB_USERNAME'), Env::get('DB_PASSWORD'), Env::get('DB_DATABASE'));
// static::$db = new Connection(Env::get('DB_HOST'), Env::get('DB_PORT'), Env::get('DB_USERNAME'), Env::get('DB_PASSWORD'), Env::get('DB_DATABASE'));
}
......@@ -57,25 +57,34 @@ class Events
public static function onMessage($clientId,$message)
{
var_dump($message);
$crc = substr($message,-2);
if($crc == Crc::crc($message))
{
$makeCrc = sprintf('%08x',(Crc::crc(substr($message,0,-2))));
if($crc != substr($makeCrc,0,2))
{
Gateway::closeClient($clientId);
}else{
$command = substr($message,6,2);
var_dump($command);
$route = Api::getRoute($command);
if( $route instanceof Route)
{
$controllerName = $route->getController();
$method = $route->getMethod();
$controllerName = APP.$controllerName;
$controller = new $controllerName($clientId,$message,$command);
$controller->$method();
......
......@@ -21,7 +21,7 @@ $worker->name = 'senthink_cdz_business';
// bussinessWorker进程数量
$worker->count = 4;
// 服务注册地址
$worker->registerAddress = '127.0.0.1:1239';
$worker->registerAddress = \Lib\Env::get('REGISTER_IP').':'.\Lib\Env::get('REGISTER_PORT');
// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START')) {
......
......@@ -29,7 +29,7 @@ $gateway->lanIp = Env::get('GATEWAY_IP');
// 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口
$gateway->startPort = Env::get('GATEWAY_START_PORT');
// 服务注册地址
$gateway->registerAddress = '0.0.0.0:1239';
$gateway->registerAddress = Env::get('REGISTER_IP').':'.Env::get('REGISTER_PORT');
// 心跳间隔
$gateway->pingInterval = 60;
......
......@@ -16,7 +16,7 @@ use \GatewayWorker\Register;
use Lib\Env;
// register 服务必须是text协议
$register = new Register('text://127.0.0.1:'.Env::get('GATEWAY_PORT'));
$register = new Register('text://'.Env::get('REGISTER_IP').':'.Env::get('REGISTER_PORT'));
// 如果不是在根目录启动,则运行runAll方法
......
......@@ -11,18 +11,15 @@ namespace Lib;
class Env
{
/**
* @var Env
*/
private static $instance = null;
/**
/*
* @var array
*/
private $env = [];
private static $env = [];
private static $boot = false;
private function __construct()
private static function boot()
{
$file = file_get_contents(__DIR__.'/../.env');
......@@ -42,52 +39,59 @@ class Env
$env = explode('=',$raw);
$this->env[$env[0]] = $env[1];
static::$env[$env[0]] = $env[1];
}
unset($rawArray);
}
/**
* @param $key
* @return bool|mixed
* @param string $key
* @return string|null
*/
public function get(string $key)
public static function get(string $key) : ? string
{
return !isset($this->env[$key]) ?? $this->env[$key];
if(static::$boot === false)
{
static::boot();
}
return static::$env[$key] ?? null;
}
/**
* @return Env
* @param string $key
* @return bool
*/
public static function getInstance()
public static function has(string $key) : bool
{
if(is_null(static::$instance))
{
static::$instance = new Env();
}
return isset(static::$env[$key]);
return static::$instance;
}
/**
* @param $name
* @param $arguments
* @return mixed
* @return array|mixed
*/
public static function __callStatic($name, $arguments)
public static function getStartFiles()
{
if(static::$boot === false)
{
$instance = static::getInstance();
static::boot();
}
$files = static::$env['START_FILES'];
$files = explode('|',$files);
return $instance->$name($arguments);
return $files;
}
}
\ No newline at end of file
......@@ -33,7 +33,7 @@ class Response
/**
* @var string
*/
private $lenght = '';
private $length = '';
/**
* @var string
......@@ -50,6 +50,10 @@ class Response
*/
private $controller;
/**
* Response constructor.
* @param Controller $controller
*/
public function __construct(Controller $controller)
{
......@@ -57,6 +61,10 @@ class Response
}
/**
* @param string $command
* @param string $data
*/
public function send(string $command,string $data ='')
{
$this->data = $data;
......@@ -67,7 +75,7 @@ class Response
$this->headerFlag = $this->controller->isEncrypt() ? '05' : '04';
$this->lenght = sprintf('%02x',dechex(7+strlen($data)/2 + 1));
$this->length = sprintf('%04x',(7+strlen($data)/2 + 1));
Gateway::sendToClient($this->controller->getClientId(),$this->organize());
......@@ -84,7 +92,7 @@ class Response
//todo 加密
}
$exceptCrc = self::PREAMBLE.$this->headerFlag.$this->headerFlag.$this->lenght.$this->data;
$exceptCrc = self::PREAMBLE.$this->headerFlag.$this->headerCtrl.$this->length.$this->nonce.$this->data;
$crc = substr(sprintf('%08x',Crc::crc($exceptCrc)),0,2);
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/20
* Time: 下午2:00
*/
namespace Lib\redis;
class RedisKey
{
}
\ No newline at end of file
......@@ -21,6 +21,9 @@ class Api
static::add('01','CertifyController@certify');
static::add('02','AccessController@access');
}
public static function add(string $command, string $controllerAction,array $middleware =[])
......@@ -41,6 +44,7 @@ class Api
static::$route[$command] = $route;
unset($route);
}
......
[2018-05-18 16:30:48] error.ERROR: Using $this when not in object context [] []
......@@ -5,21 +5,20 @@
* Date: 2018/5/16
* Time: 下午4:55
*/
use Lib\Env;
use Workerman\Worker;
ini_set('display_errors', 'on');
date_default_timezone_set('Asia/Shanghai');
// 检查扩展
if(!extension_loaded('pcntl'))
{
if (!extension_loaded('pcntl')) {
exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
}
if(!extension_loaded('posix'))
{
if (!extension_loaded('posix')) {
exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
}
......@@ -30,16 +29,45 @@ require_once __DIR__ . '/vendor/autoload.php';
function exceptionHandle($exception)
{
$logger = new \Monolog\Logger('error',new \Monolog\Handler\RotatingFileHandler('error',30));
$logger = new \Monolog\Logger('error');
$logger->pushHandler(new \Monolog\Handler\RotatingFileHandler('./storage/error', 30));
try{
$logger->error($exception->getMessage(),[
$exception->getFile(),
$exception->getTraceAsString(),
$exception->getLine(),
$exception->getTrace(),
]);
}catch (Exception $exception)
{
error_log($exception->getMessage());
}
$logger->error($exception->getMessage());
}
set_exception_handler('exceptionHandle');
// 加载所有Applications/*/start.php,以便启动所有服务
foreach(glob(__DIR__.'/Application/start*.php') as $start_file)
{
foreach (glob(__DIR__ . '/Application/start*.php') as $start_file) {
foreach (Env::getStartFiles() as $file)
{
if(strstr($start_file,$file))
{
require_once $start_file;
}
}
require_once $start_file;
}
\Route\Api::boot();
Worker::runAll();
\ No newline at end of file
[2018-05-18 16:32:02] error.ERROR: Using $this when not in object context [] []
[2018-05-18 16:35:32] error.ERROR: Using $this when not in object context [] []
[2018-05-18 16:36:02] error.ERROR: hello [] []
[2018-05-18 16:59:33] error.ERROR: gett [] []
[2018-05-18 17:05:20] error.ERROR: gett [] []
[2018-05-18 17:05:44] error.ERROR: gett [] []
[2018-05-18 17:07:51] error.ERROR: gett [] []
[2018-05-18 17:08:08] error.ERROR: gett [] []
[2018-05-18 17:08:29] error.ERROR: Using $this when not in object context [] []
[2018-05-18 17:09:17] error.ERROR: Using $this when not in object context [] []
[2018-05-18 17:14:02] error.ERROR: Undefined class constant 'env' [] []
[2018-05-18 17:17:41] error.ERROR: Undefined class constant 'env' [] []
[2018-05-18 17:19:36] error.ERROR: Using $this when not in object context [] []
[2018-05-18 17:19:47] error.ERROR: Using $this when not in object context [] []
[2018-05-18 17:21:00] error.ERROR: Using $this when not in object context [] []
[2018-05-18 17:31:34] error.ERROR: Call to undefined method Lib\Env::getInstance() [] []
[2018-05-18 17:33:50] error.ERROR: Call to undefined method Lib\Env::getInstan() [48,[]] []
[2018-05-18 17:34:40] error.ERROR: Call to undefined method Lib\Env::getInstan() ["/var/www/senthink/cdz_gateway/start.php","#0 {main}",50,[]] []
[2018-05-20 11:14:36] error.ERROR: Address already in use ["/var/www/senthink/cdz_gateway/vendor/workerman/workerman/Worker.php","#0 /var/www/senthink/cdz_gateway/vendor/workerman/workerman/Worker.php(578): Workerman\\Worker->listen()\n#1 /var/www/senthink/cdz_gateway/vendor/workerman/workerman/Worker.php(462): Workerman\\Worker::initWorkers()\n#2 /var/www/senthink/cdz_gateway/start.php(63): Workerman\\Worker::runAll()\n#3 {main}",1994,[{"file":"/var/www/senthink/cdz_gateway/vendor/workerman/workerman/Worker.php","line":578,"function":"listen","class":"Workerman\\Worker","type":"->","args":[]},{"file":"/var/www/senthink/cdz_gateway/vendor/workerman/workerman/Worker.php","line":462,"function":"initWorkers","class":"Workerman\\Worker","type":"::","args":[]},{"file":"/var/www/senthink/cdz_gateway/start.php","line":63,"function":"runAll","class":"Workerman\\Worker","type":"::","args":[]}]] []
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment