首页 > 代码库 > shell 带签名请求,yii 处理带签名的请求

shell 带签名请求,yii 处理带签名的请求

 

处理请求

class TestController extends Controller
{
    
    public function init()
    {
        if(!YII_ENV_DEV){
            throw  new ExitException(201,‘can not access not in dev mode‘);
        }
    }
    
    /**
     *
     */
    public function actionClientip()
    {
        if(YII_ENV_DEV){
            $request = Yii::$app->getRequest();
            $keyStr = ‘key_123‘;
            $timestamp = $request->get(‘timestamp‘);
            $sign = $request->get(‘sign‘);
            if(md5($timestamp.$keyStr) == $sign){
                $databaseIp = $request->getUserIP();
                // $databaseIp = ‘127.0.0.1‘;
                $conainer = new ContainerSaveForDynamicData();
                $dbComponetConfig = [
                    ‘class‘ => ‘yii\db\Connection‘,
                    ‘dsn‘ => ‘mysql:host=‘.$databaseIp.‘;port=3306;dbname=database1‘,
                    ‘username‘ => ‘username1‘,
                    ‘password‘ => ‘password1‘,
                    ‘charset‘ => ‘utf8‘,
                    ‘tablePrefix‘=>‘prefix_‘
                ];
                $conainer->setDataByKey(ContainerSaveForDynamicData::DB_COMPONENT_CONFIG,$dbComponetConfig);
                return ‘success‘;
            }
            return ‘fail‘;
        }
        else{
            return ‘fail,not in dev mode‘;
        }
    }
    
}

非标准路径缓存管理

use yii\helpers\FileHelper;
// $conainer = new \common\components\ContainerSaveForDynamicData();
// $conainer->getDataByKey(‘db4Dynamic‘);
// $conainer->setDataByKey(‘db4Dynamic‘,‘127.0.0.1‘);
class ContainerSaveForDynamicData
{
    
    const DB_COMPONENT_CONFIG = ‘dbComponetConfig‘;
    public $cacheFileSuffix = ‘.bin‘;
    public $cachePath = ‘./dynamic-data-cache‘;
    public $dirMode = 0775;
    public $keyPrefix = ‘‘;
    public $fileMode;
    
    public function __construct()
    {
        $this->cachePath = __DIR__.‘/../../dynamic-data-cache‘;
        if (!is_dir($this->cachePath)) {
            FileHelper::createDirectory($this->cachePath, $this->dirMode, true);
        }
    }
    
    /**
     * 指定键的值
     * @param unknown $key
     * @return boolean|unknown
     */
    public function getDataByKey($key)
    {
        $key = $this->buildKey($key);
        $cacheFile = $this->getCacheFile($key);
        if(!file_exists($cacheFile)){
            return false;
        }
        
        if (@filemtime($cacheFile) > time()) {
            $fp = @fopen($cacheFile, ‘r‘);
            if ($fp !== false) {
                @flock($fp, LOCK_SH);
                $cacheValue = @stream_get_contents($fp);
                @flock($fp, LOCK_UN);
                @fclose($fp);
                
                $cacheValue = unserialize($cacheValue);
                
                return $cacheValue;
            }
        }
        
        return false;
    }

    /**
     * 设置值
     */
    public function setDataByKey($key,$value,$duration=0)
    {
        $key = $this->buildKey($key);
        $cacheFile = $this->getCacheFile($key);
        
        $value = serialize($value);
        
        if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) {
            if ($this->fileMode !== null) {
                @chmod($cacheFile, $this->fileMode);
            }
            if ($duration <= 0) {
                $duration = 31536000; // 1 year
            }
            return @touch($cacheFile, $duration + time());
        } else {
            $error = error_get_last();
            return false;
        }
    }

    protected function buildKey($key)
    {
        $key = md5(json_encode($key));
        return $this->keyPrefix . $key;
    }
    
    /**
     * 缓存文件名称
     * @param unknown $key
     * @return string
     */
    protected function getCacheFile($key)
    {
        return $this->cachePath . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix;
    }
}


从缓存中读取数据库配置

<?php

if(YII_ENV_DEV){
    $conainer = new ContainerSaveForDynamicData();
    if(($data = $conainer->getDataByKey(ContainerSaveForDynamicData::DB_COMPONENT_CONFIG)) != false){
        return $data;
    }
    else{
        return [
            ‘class‘ => ‘yii\db\Connection‘,
            ‘dsn‘ => ‘mysql:host=127.0.0.1;port=3306;dbname=database1‘,
            ‘username‘ => ‘username1‘,
            ‘password‘ => ‘password1‘,
            ‘charset‘ => ‘utf8‘,
            ‘tablePrefix‘=>‘prefix_‘
        ];
    }
    
}
else if(YII_ENV_TEST){
    return [
        ‘class‘ => ‘yii\db\Connection‘,
        ‘dsn‘ => ‘mysql:host=127.0.0.1;port=3306;dbname=database1‘,
        ‘username‘ => ‘username1‘,
        ‘password‘ => ‘password1‘,
        ‘charset‘ => ‘utf8‘,
        ‘tablePrefix‘=>‘prefix_‘
    ];
}
else if(YII_ENV_PROD){
    
}


定时请求

#!/bin/sh
export LANG=en_US.UTF-8
timestamp=`date "+%s"`

keystr="key_123"
pre_str=$timestamp$keystr
sign_str=$(echo -n $pre_str|md5sum|awk -F ‘ ‘ ‘{print $1}‘)
req_url_str="http://www.xxx.com/index.php?r=test/clientip&sign="$sign_str"×tamp="$timestamp
result_str=`curl -s --url $req_url_str`

echo $result_str >> /a/b/c/www.xxx.com/mytasks/logs/log_update_ip.log

 

shell 带签名请求,yii 处理带签名的请求