dongtong0796 2014-10-01 02:35
浏览 204
已采纳

如何使用PHP访问PritunlVPN API?

Did anyone managed to access pritunl's api using php ?

the guide provided from the website only has python examples. http://pritunl.com/api.html

here's my attempt as it seems that api uses HTTP Authenticate Digest

but this will return failed to open stream: HTTP request failed! HTTP/1.1 401 UNAUTHORIZED

class pritunl {

public $BASE_URL   = "https://localhost:9700";
public $API_TOKEN  = "KDwGfVp5YfBDfozZMr6GOMHxIQard8Ej";
public $API_SECRET = "0VHyY0sI2bwvUHtewDSa64Q8n1Vqj8io";

public function __construct() {
    $this->auth_timestamp = time();
    $this->auth_nonce     = $this->gen_uuid();
    $this->auth_string    = join("&",array($this->API_TOKEN,$this->auth_timestamp,$this->auth_nonce));
    $this->auth_signature = base64_encode(hash_hmac('sha256',$this->API_SECRET,$this->auth_string));

}

public function connect($dir="") {
    $options = array(
        'http' => array(
            'header'  => ['Authorization: Digest','Content-Type: application/json','Auth-Token: '.$this->API_TOKEN,'Auth-Timestamp: '.$this->auth_timestamp,'Auth-Nonce: '.$this->auth_nonce,'Auth-Signature: '.$this->auth_signature],
            'method'  => 'GET',
            'content' => '',
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($this->BASE_URL.$dir, false, $context);

    return $result;

}

private function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
    // 32 bits for "time_low"
    mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

    // 16 bits for "time_mid"
    mt_rand( 0, 0xffff ),

    // 16 bits for "time_hi_and_version",
    // four most significant bits holds version number 4
    mt_rand( 0, 0x0fff ) | 0x4000,

    // 16 bits, 8 bits for "clk_seq_hi_res",
    // 8 bits for "clk_seq_low",
    // two most significant bits holds zero and one for variant DCE1.1
    mt_rand( 0, 0x3fff ) | 0x8000,

    // 48 bits for "node"
    mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
  • 写回答

1条回答 默认 最新

  • 啊啊啊小孔 2014-10-02 18:51
    关注
    <?php
    
    $BASE_URL   = "";
    $API_TOKEN  = "";
    $API_SECRET = "";
    
    function auth_request($method, $path, $data="") {
        global $BASE_URL, $API_TOKEN, $API_SECRET;
    
        $auth_timestamp = strval(time());
        $auth_nonce = uniqid();
        $auth_array = array(
            $API_TOKEN,
            $auth_timestamp,
            $auth_nonce,
            strtoupper($method),
            $path,
        );
        if ($data) {
            array_push($auth_array, $data);
        }
    
        $auth_string = join("&", $auth_array);
        $auth_signature = base64_encode(hash_hmac(
            "sha256", $auth_string, $API_SECRET, true));
        $options = array(
            "http" => array(
                "header"  => array(
                    'Content-Type: application/json',
                    'Auth-Token: '.$API_TOKEN,
                    'Auth-Timestamp: '.$auth_timestamp,
                    'Auth-Nonce: '.$auth_nonce,
                    'Auth-Signature: '.$auth_signature
                ),
                "method"  => $method,
                "content" => $data,
            ),
        );
    
        $context = stream_context_create($options);
        return file_get_contents($BASE_URL.$path, false, $context);
    };
    
    printf(auth_request("GET", "/status")."
    ");
    printf(auth_request("PUT", "/organization/6d18ab1855804e5e86121e24cd3ec0e5",
        json_encode(array(
            "name" => "test",
        ))
    )."
    ");
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog