shupershuff / Diablo2RLoader

D2rLoader - Diablo 2: Resurrected launcher for multi-boxing
MIT License
62 stars 11 forks source link

Couldn't get D2Emu connection Token. #51

Closed hebjor closed 2 weeks ago

hebjor commented 2 months ago

Hi & thanks for a great tool,

I'm using version 1.13.2

image

Recently I started to get this message when I start

image

I tried all 3 different sites (d2emu.com, D2runewizard.com and diablo2.io) but get same error message with all of them.

When I do a "r" to refresh I keep getting "Unable to check for DClone status via d2emu.com." When I do a "d" to check DClone status I get these error messages

image

Connecting to d2emu.com website works fine in a browser. I'm connecting from Thailand/Asia.

shupershuff commented 2 months ago

Can you reopen script, try again and let me know if it still happens?

hebjor commented 2 months ago

Yes, same issue


`Friday, September 13, 2024 9:30:45 PM

PS D:\games\D2RLoader> .\D2Loader.ps1 Couldn't get D2Emu connection Token. Press any key to continue...`

shupershuff commented 2 months ago

Are you able to get to the d2emu website? https://d2emu.com/

If so, can you run these two commands in PowerShell and screenshot what you get:

(invoke-webrequest https://d2emu.com/api/v1/shupertoken/aes).content | convertfrom-json ((invoke-webrequest https://d2emu.com/api/v1/shupertoken/token).content | convertfrom-json).token

hebjor commented 2 months ago

Yes, website works fine. I use it for checking TZ and never had any issues with it.

The first command returns a blank line, the 2nd seem to return nothing. image

shupershuff commented 2 months ago

That's really odd. What do you get when you simply run the following:

invoke-webrequest https://d2emu.com/api/v1/shupertoken/aes invoke-webrequest https://d2emu.com/api/v1/shupertoken/token

Are you using your own internet connection? Wondering if the connection is being blocked via a proxy or firewall.

hebjor commented 2 months ago

image

Yes, it is my own internet. It is 1000/1000 fiber and as far as I know there are no restrictions (firewall or proxy). As far as I can tell this is just normal https traffic. Could it be d2emu.com is using some kind of Cloudflare caching that causes this?

hebjor commented 2 months ago

I tried to remote desktop into my computer back in Europe and get different results that seem to make more sense. I will install a VPN and do some more testing from Thailand. It seems the Thailand internet connection is the issue.

image

shupershuff commented 2 months ago

It's definitely an internet connection / firewall issue as there aren't any issues with D2Emu's API. That said, I'll reach out to them to see if there's any geo blocking going on.

hebjor commented 2 months ago

Yeah, this is very strange. I installed NordVPN and tried from both Singapore and US but get the same empty results. It makes me wonder if it could be something local on my computer. It's kinda weird since it is fresh Windows 11 install. I have disabled Windows Defender and Windows Firewall. I will do some more testing in my end, and see if I can install it on an old Windows laptop.

shupershuff commented 2 months ago

What about another URL, does this return no content?

Invoke-webrequest -uri https://v2.jokeapi.dev/joke/miscellaneous

shupershuff commented 2 months ago

Also, try this command Invoke-WebRequest -Uri "https://d2emu.com/api/v1/shupertoken/aes" -Proxy $null

hebjor commented 2 months ago

image

It worked! So there must be some sort of proxy somewhere. I also tried the previous commands on a Windows 10 laptop I have and it also works. So this must be some local problem on my new pc.

hebjor commented 2 months ago

The jokeapi works without the proxy parameter

image

hebjor commented 2 months ago

Clearly a local problem on this Windows 11 pc, but I have no idea where that proxy comes from. When I run some commands to check it does not report any proxy.

image

shupershuff commented 2 months ago

You can also use this to check proxy Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'

Or open "internet options" from start menu (the old menu you might recognise from Internet Explorer back in the day): image

hebjor commented 2 months ago

This is very weird, now it actually works without the -Proxy too. image Also D2Rloader starts and works just fine.
image

I have no idea what is going on here, but I'm very happy it works! Thanks so much for the help

shupershuff commented 2 months ago

Glad to hear :)

hebjor commented 2 months ago

Not understanding what was wrong drove me crazy so I did some more testing, and it seems to be the version of PS that caused the issue.

Powershell 7.4.5 fails image

Powershell 5.1.22621.4111 works image

image image

also the -Proxy parameter does not seem to make any difference, it was just a random thing I started PS via CMD I got the older version. I normally start it via Windows Terminal which starts the latest version.

shupershuff commented 2 months ago

Not understanding what was wrong drove me crazy so I did some more testing, and it seems to be the version of PS that caused the issue.

Powershell 7.4.5 fails image

Powershell 5.1.22621.4111 works image

image image

also the -Proxy parameter does not seem to make any difference, it was just a random thing I started PS via CMD I got the older version. I normally start it via Windows Terminal which starts the latest version.

Thanks for the details mate. I will have a think about how I can prevent this happening going forward.

shupershuff commented 2 months ago

I've not been able to figure out the connection problems. It also happens with curl and invoke-restmethod when connecting to that site. It's likely an issue with cloudflare or the webserver. As a workaround, try replacing the GetEmuToken Function with the below and let me know if it works. This will force PS 7.x to run those commands in PS 5.1:

Function GetEmuToken { #For connecting to D2Emu for tz and/or dclone data
    Try {
        if ($PSVersionTable.psversion.major -ge 7){# Run a PowerShell 5.1 script from within PowerShell 7.x, for whatever reason no content is returned on PS 7.x. Identified in https://github.com/shupershuff/Diablo2RLoader/issues/51
            $AES = & "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -Command {(invoke-webrequest https://d2emu.com/api/v1/shupertoken/aes).content | convertfrom-json}
            $EncryptedToken = & "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -Command {((invoke-webrequest https://d2emu.com/api/v1/shupertoken/token).content | convertfrom-json).token}
        }
        Else {
            $AES = (invoke-webrequest https://d2emu.com/api/v1/shupertoken/aes).content | convertfrom-json
            $EncryptedToken = ((invoke-webrequest https://d2emu.com/api/v1/shupertoken/token).content | convertfrom-json).token
        }
        $Key = $AES.key
        $IV = $AES.iv
        $bytes = [System.Convert]::FromBase64String($EncryptedToken);
        $aes = [System.Security.Cryptography.Aes]::Create();
        $utf8 = [System.Text.Encoding]::Utf8;
        $aes.Key = $utf8.GetBytes($key);
        $aes.IV  = $utf8.GetBytes($iv);
        $decryptor = $aes.CreateDecryptor();
        $unencryptedData = $decryptor.TransformFinalBlock($bytes, 0, $bytes.Length);
        $aes.Dispose();
        $Script:EmuToken = [System.Text.Encoding]::UTF8.GetString($unencryptedData); #Decrypted token
    }
    Catch {
        write-output " Couldn't get D2Emu connection Token." | red
        PressTheAnyKey
    }
}
hebjor commented 2 months ago

I can confirm the GetEmuToken function above works on latest Powershell version.