一键登录WHM

One click login to WHM

我正在尝试为 WHM 生成一键登录 URL。

下面的代码是基于 cPanel 登录(我打算为 WHM 更改),但它根本不起作用。访问被拒绝错误。

这是我尝试过的:

$whmusername = 'root';
$whmpassword = 'password';
$hostname = 'server.example.com';
$cpanel_user = 'cpaneluser';
$query = "https://$hostname/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";
   
$curl = curl_init();                                     // Create Curl Object.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);       // Allow self-signed certificates...
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);       // and certificates that don't match the hostname.
curl_setopt($curl, CURLOPT_HEADER, false);               // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        // Return contents of transfer on curl_exec.
$header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);         // Set the username and password.
curl_setopt($curl, CURLOPT_URL, $query);                 // Execute the query.
$result = curl_exec($curl);
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
}

$decoded_response = json_decode( $result, true );
  
$session_url = $decoded_response['data']['url'];
$cookie_jar = 'cookie.txt';
  
curl_setopt($curl, CURLOPT_HTTPHEADER, null);             // Unset the authentication header.
curl_setopt($curl, CURLOPT_COOKIESESSION, true);          // Initiate a new cookie session.
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);       // Set the cookie jar.
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);      // Set the cookie file.
curl_setopt($curl, CURLOPT_URL, $session_url);            // Set the query url to the session login url.
  
$result = curl_exec($curl);                               // Execute the session login call.
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                    // Log an error if curl_exec fails.
}
  
$session_url = preg_replace( '{/login(?:/)??.*}', '', $session_url );
  
$query = "$session_url/execute/Ftp/list_ftp";
  
curl_setopt($curl, CURLOPT_URL, $query);  // Change the query url to use the UAPI call.
$result = curl_exec($curl);               // Execute the UAPI call.
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                    // log error if curl exec fails
}
  
curl_close($curl);
print $result;
   

我能够通过使用以下代码来做到这一点。此代码最初用于 WHM API 示例,但我为此重新应用了它。

$user = "root";
$token = "API Token";

$query = "https://server.example.com:2087/json-api/create_user_session?api.version=1&user=root&service=whostmgrd";

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

$header[0] = "Authorization: whm $user:$token";
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_URL, $query);

$result = curl_exec($curl);

$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($http_status != 200) {
    echo "[!] Error: " . $http_status . " returned\n";
} else {
    $json = json_decode($result, true);
    $login_url = $json['data']['url'];
    echo $login_url;
}
curl_close($curl);

不使用 WHM user/password,只使用用户并使用可以在 WHM -> 管理 API 令牌中创建的 API 令牌登录。

然后获取 json 数据并仅输出 URL。