kinnay / NintendoClients

Python package to communicate with Switch, Wii U and 3DS servers
MIT License
537 stars 63 forks source link

Update games + BAAS (presence) + FIVE (group invite) #85

Closed EpicUsername12 closed 1 year ago

EpicUsername12 commented 1 year ago

I added the Nintendo Switch presence endpoints

"/1.0.0/users/%016x/device_accounts/%016x" % (user_id, baas_id)

The code supports everything the Nintendo Switch 'friends' service can do to the "friend presence"

You can use the code this way:

    dauth = DAuthClient(keys)
    dauth.set_certificate(cert, pkey)
    dauth.set_system_version(SYSTEM_VERSION)
    response = await dauth.device_token(dauth.BAAS)
    device_token = response["device_auth_token"]

    aauth = AAuthClient()
    aauth.set_system_version(SYSTEM_VERSION)
    response = await aauth.auth_digital(
        0x0100ABD01785C000, 0x20000,
        device_token, ticket
    )
    app_token = response["application_auth_token"]

    baas = BAASClient()
    baas.set_system_version(SYSTEM_VERSION)

    response = await baas.authenticate(device_token)
    access_token = response["accessToken"]

    response = await baas.login(
        BAAS_USER_ID, BAAS_PASSWORD, access_token, app_token
    )
    user_id = int(response["user"]["id"], 16)
    id_token = response["idToken"]

    # 0x0100abd01785c000 = Portal 2 title ID
    # You would appear online playing 'Portal 2' on your friends friend list
    #
    # PresenceState.ONLINE, PresenceState.PLAYING, PresenceState.INACTIVE (offline)
    await baas.set_presence(BAAS_USER_ID, user_id, response["accessToken"], PresenceState.ONLINE, 0x0100abd01785c000)
EpicUsername12 commented 1 year ago

I added "Five" endpoint basic functionality

The only function supported is this one:

app.lp1.five.nintendo.net/v1/invitation_groups

It can be used like this:

from nintendo.five import FiveClient

# Copied from Portal 2 (24 bytes)
invite_data = streams.StreamOut(s)
invite_data.u64(MY_OWN_PID)
invite_data.u64(MY_OWN_PID)
invite_data.u32(msess.id) # Gathering ID
invite_data.pad(4)
invite_data.u8(0x04) # Unk
invite_data.pad(3)
invite_data.u8(16) # Unk
invite_data.pad(3)
invite_data.write(b"none") # lol

fc = FiveClient()
fc.set_system_version(SYSTEM_VERSION) # Not mandatory, defaults to the latest version
await fc.send_invitation_to_group(response["accessToken"], 0x0100abd01785c000, "Invite text that will appear in the invite list!", invite_data.data, [TARGET_PID])

Proof it works: 0262CDD1-F700-41C5-9AD9-6C6C0FF924CD

kinnay commented 1 year ago

Hi, there are a few problems with this PR:

  1. 22306D00 is definitely not the game server id of Portal 2. This is the game server id of SMM2. I'm also quite sure that the access key is not hNWPC8B1. An access key always contains 8 hex digits. The only exception is AC:NH, but even AC:NH does not use uppercase.
  2. You say "Respect header ordering", but your code places the Content-Type header first. In my Charles dump it appears between the Accept and Authorization headers instead. I also think that the user agent for five should contain nnFriends?
  3. The coding style differs a bit. I do not use type hints anywhere, and for strings I use double quotes as much as possible. It's best to be consistent. Also, the data in send_invitation_to_group is indented too much.

Personally, I've been writing tests for most Switch endpoints, which validate the raw HTTP request. These requests are copied directly from a Charles dump and only modified to remove my personal data. This should catch errors such as wrong user agents / header ordering. See test_dragons.py for example.

I will probably try to implement these features myself, but I do appreciate the effort.

Also sorry that I didn't reply earlier. I wasn't sure if I wanted to implement this functionality in NintendoClients at all because it's not really related to NEX, but by now I've decided that I want to implement other services as well.

EpicUsername12 commented 1 year ago

The game server ID is in fact 2EDB3700, but the access key is hNWPC8B1 (i must be stupid)

But i agree on the rest, will be waiting for your implementation

kinnay commented 1 year ago

Here: https://github.com/kinnay/NintendoClients/blob/master/nintendo/five.py

I hope that this is good.