Rewrite WU requests and cookie handling
This commit is contained in:
@ -25,19 +25,43 @@ function uupDevice() {
|
||||
return base64_encode(chunk_split($data, 1, "\0"));
|
||||
}
|
||||
|
||||
function uupSaveCookieFromResponse($out) {
|
||||
$outDecoded = html_entity_decode($out);
|
||||
preg_match('/<NewCookie>.*?<\/NewCookie>|<GetCookieResult>.*?<\/GetCookieResult>/', $outDecoded, $cookieData);
|
||||
|
||||
if(empty($cookieData))
|
||||
return false;
|
||||
|
||||
preg_match('/<Expiration>.*<\/Expiration>/', $cookieData[0], $expirationDate);
|
||||
preg_match('/<EncryptedData>.*<\/EncryptedData>/', $cookieData[0], $encryptedData);
|
||||
|
||||
$expirationDate = preg_replace('/<Expiration>|<\/Expiration>/', '', $expirationDate[0]);
|
||||
$encryptedData = preg_replace('/<EncryptedData>|<\/EncryptedData>/', '', $encryptedData[0]);
|
||||
|
||||
$cookieData = array(
|
||||
'expirationDate' => $expirationDate,
|
||||
'encryptedData' => $encryptedData,
|
||||
);
|
||||
|
||||
$cookieStorage = new UupDumpCache('WuRequestCookie', false);
|
||||
$cookieStorage->put($cookieData, false);
|
||||
|
||||
return $cookieData;
|
||||
}
|
||||
|
||||
function uupInvalidateCookie() {
|
||||
$cookieStorage = new UupDumpCache('WuRequestCookie', false);
|
||||
$cookieInfo = $cookieStorage->delete();
|
||||
}
|
||||
|
||||
function uupEncryptedData() {
|
||||
$cookieInfo = @file_get_contents(dirname(__FILE__).'/cookie.json');
|
||||
$cookieInfo = json_decode($cookieInfo, 1);
|
||||
$cookieStorage = new UupDumpCache('WuRequestCookie', false);
|
||||
$cookieInfo = $cookieStorage->get();
|
||||
|
||||
if(empty($cookieInfo)) {
|
||||
$postData = composeGetCookieRequest(uupDevice());
|
||||
sendWuPostRequest('https://fe3.delivery.mp.microsoft.com/ClientWebService/client.asmx', $postData);
|
||||
|
||||
$encData = uupEncryptedData();
|
||||
} else {
|
||||
$encData = $cookieInfo['encryptedData'];
|
||||
$data = sendWuPostRequestHelper('client', 'composeGetCookieRequest', [], false);
|
||||
$cookieInfo = uupSaveCookieFromResponse($data['out']);
|
||||
}
|
||||
|
||||
return $encData;
|
||||
return $cookieInfo['encryptedData'];
|
||||
}
|
||||
?>
|
||||
|
@ -16,7 +16,7 @@ limitations under the License.
|
||||
*/
|
||||
|
||||
function uupApiVersion() {
|
||||
return '1.40.3';
|
||||
return '1.41.0';
|
||||
}
|
||||
|
||||
require_once dirname(__FILE__).'/auths.php';
|
||||
|
@ -298,7 +298,8 @@ function branchFromBuild($build) {
|
||||
}
|
||||
|
||||
// Composes POST data for gathering list of urls for download
|
||||
function composeFileGetRequest($updateId, $device, $info, $rev = 1, $type = 'Production') {
|
||||
function composeFileGetRequest($updateId, $info, $rev = 1, $type = 'Production') {
|
||||
$device = uupDevice();
|
||||
$uuid = genUUID();
|
||||
|
||||
$createdTime = time();
|
||||
@ -359,7 +360,9 @@ XML;
|
||||
}
|
||||
|
||||
// Composes POST data for fetching the latest update information from Windows Update
|
||||
function composeFetchUpdRequest($device, $encData, $arch, $flight, $ring, $build, $sku = 48, $type = 'Production') {
|
||||
function composeFetchUpdRequest($arch, $flight, $ring, $build, $sku = 48, $type = 'Production') {
|
||||
$device = uupDevice();
|
||||
$encData = uupEncryptedData();
|
||||
$uuid = genUUID();
|
||||
|
||||
$createdTime = time();
|
||||
@ -585,7 +588,8 @@ XML;
|
||||
}
|
||||
|
||||
// Composes POST data for Get Cookie request
|
||||
function composeGetCookieRequest($device) {
|
||||
function composeGetCookieRequest() {
|
||||
$device = uupDevice();
|
||||
$uuid = genUUID();
|
||||
|
||||
$createdTime = time();
|
||||
|
@ -51,7 +51,7 @@ function genUUID() {
|
||||
);
|
||||
}
|
||||
|
||||
function sendWuPostRequest($url, $postData) {
|
||||
function sendWuPostRequestInternal($url, $postData, $saveCookie = true) {
|
||||
$req = curl_init($url);
|
||||
|
||||
$proxy = uupDumpApiGetConfig();
|
||||
@ -77,38 +77,40 @@ function sendWuPostRequest($url, $postData) {
|
||||
|
||||
curl_close($req);
|
||||
|
||||
/*
|
||||
Replace an expired cookie with a new one by replacing it in existing
|
||||
postData. This has to be done this way, because handling it properly would
|
||||
most likely require a rewrite of half of the project.
|
||||
*/
|
||||
if($error == 500 && preg_match('/<ErrorCode>(ConfigChanged|CookieExpired)<\/ErrorCode>/', $out)) {
|
||||
$oldCookie = uupEncryptedData();
|
||||
@unlink(dirname(__FILE__).'/cookie.json');
|
||||
$postData = str_replace($oldCookie, uupEncryptedData(), $postData);
|
||||
if($saveCookie === true)
|
||||
uupSaveCookieFromResponse($out);
|
||||
|
||||
return sendWuPostRequest($url, $postData);
|
||||
return [
|
||||
'error' => $error,
|
||||
'out' => $out
|
||||
];
|
||||
}
|
||||
|
||||
function sendWuPostRequest($url, $postData) {
|
||||
return sendWuPostRequestInternal($url, $postData)['out'];
|
||||
}
|
||||
|
||||
function sendWuPostRequestHelper(
|
||||
$endpoint,
|
||||
$postComposer,
|
||||
$postComposerArgs,
|
||||
$saveCookie = true
|
||||
) {
|
||||
$endpoints = [
|
||||
'client' => 'https://fe3.delivery.mp.microsoft.com/ClientWebService/client.asmx',
|
||||
'clientSecured' => 'https://fe3cr.delivery.mp.microsoft.com/ClientWebService/client.asmx/secured'
|
||||
];
|
||||
|
||||
$postData = call_user_func_array($postComposer, $postComposerArgs);
|
||||
$data = sendWuPostRequestInternal($endpoints[$endpoint], $postData, $saveCookie);
|
||||
|
||||
if($data['error'] == 500 && preg_match('/<ErrorCode>(ConfigChanged|CookieExpired|InvalidCookie)<\/ErrorCode>/', $data['out'])) {
|
||||
uupInvalidateCookie();
|
||||
$postData = call_user_func_array($postComposer, $postComposerArgs);
|
||||
return sendWuPostRequestInternal($endpoints[$endpoint], $postData, $saveCookie);
|
||||
}
|
||||
|
||||
$outDecoded = html_entity_decode($out);
|
||||
preg_match('/<NewCookie>.*?<\/NewCookie>|<GetCookieResult>.*?<\/GetCookieResult>/', $outDecoded, $cookieData);
|
||||
|
||||
if(!empty($cookieData)) {
|
||||
preg_match('/<Expiration>.*<\/Expiration>/', $cookieData[0], $expirationDate);
|
||||
preg_match('/<EncryptedData>.*<\/EncryptedData>/', $cookieData[0], $encryptedData);
|
||||
|
||||
$expirationDate = preg_replace('/<Expiration>|<\/Expiration>/', '', $expirationDate[0]);
|
||||
$encryptedData = preg_replace('/<EncryptedData>|<\/EncryptedData>/', '', $encryptedData[0]);
|
||||
|
||||
$fileData = array(
|
||||
'expirationDate' => $expirationDate,
|
||||
'encryptedData' => $encryptedData,
|
||||
);
|
||||
|
||||
@file_put_contents(dirname(__FILE__).'/cookie.json', json_encode($fileData));
|
||||
}
|
||||
|
||||
return $out;
|
||||
return $data;
|
||||
}
|
||||
|
||||
function consoleLogger($message, $showTime = 1) {
|
||||
|
Reference in New Issue
Block a user