<?php

/**
 * 认证请求
 * @author:dc
 * @time 2025/3/31 11:20
 * Class Auth
 */
class Auth {


    private $data;


    public function __construct(string $data)
    {
        $this->data = $data;

        $this->check();

    }

    public $tag; // tag
    public $sign; // 签名
    public $rang; // 随机串
    public $out_ip; // 代理出口ip
    public $host; // 请求地址
    public $timeOut; // 超时


    private function check(){
        list($this->tag, $this->sign ,$this->rang, $this->out_ip ,$this->host, $this->timeOut) = explode(' ', $this->data.'            ');

        // 来源不对,直接关闭
        if ($this->sign !== md5('fob.'.$this->rang)){
            $this->error('签名验证失败');
        }
        // 没有出口ip
//        if(!$this->out_ip){
//            $this->error('出口ip不可空');
//        }
        // 没有代理地址
        if(!$this->host){
            $this->error('代理地址不可空');
        }
        $this->timeOut = intval($this->timeOut);

        $this->timeOut = $this->timeOut ? $this->timeOut : 5;

    }

    public function error($msg){
        throw new Exception("500 ".$msg."\r\n");
    }


}