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

如何使用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条回答 默认 最新

相关推荐 更多相似问题