如何使用PHP访问PritunlVPN API?

时间:2014-10-01 02:35:27

标签: php api vpn

有没有人设法使用php访问pritunl的api?

网站提供的指南只有python示例。 http://pritunl.com/api.html

这是我的尝试,似乎api使用HTTP Authenticate Digest

但这会回来  无法打开流:HTTP请求失败! 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 个答案:

答案 0 :(得分:0)

<?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")."\n");
printf(auth_request("PUT", "/organization/6d18ab1855804e5e86121e24cd3ec0e5",
    json_encode(array(
        "name" => "test",
    ))
)."\n");

?>