Yepoleb / python-a2s

A2S Query API for Python
MIT License
150 stars 17 forks source link

Which ports need to be unblocked, for a2s to work? #54

Closed Cameronsplaze closed 4 months ago

Cameronsplaze commented 4 months ago

I'm trying to get the player count from a valheim container. I can get it to work when running the container locally, but not when I run it in the cloud. When it's in the cloud, I don't get a response from https://api.steampowered.com/ISteamApps/GetServersAtAddress/v0001?addr=* (Just {"response":{"success":true,"servers":[]}}). (IP changes every time I reset the server.).

The ports I have unblocked are UDP 2456-2457, and I'm using 2457 for info. Because it works locally, the only thing I can think of is it's a firewall issue? The error I get is the classic timeout:

>>> a2s.info(("valheim-test.theamazingcameron.com", 2457), timeout=60)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/victor/Documents/GameManagement/.venv/lib64/python3.12/site-packages/a2s/info.py", line 182, in info
    return request_sync(address, timeout, encoding, InfoProtocol)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/victor/Documents/GameManagement/.venv/lib64/python3.12/site-packages/a2s/a2s_sync.py", line 22, in request_sync
    response = request_sync_impl(conn, encoding, a2s_proto)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/victor/Documents/GameManagement/.venv/lib64/python3.12/site-packages/a2s/a2s_sync.py", line 28, in request_sync_impl
    resp_data = conn.request(a2s_proto.serialize_request(challenge))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/victor/Documents/GameManagement/.venv/lib64/python3.12/site-packages/a2s/a2s_sync.py", line 92, in request
    return self.recv()
           ^^^^^^^^^^^
  File "/home/victor/Documents/GameManagement/.venv/lib64/python3.12/site-packages/a2s/a2s_sync.py", line 67, in recv
    packet = self._socket.recv(65535)
             ^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: timed out

I can't find anything in the valve docs about any required port, and wireshark was way to noisy to get anything useful from. Do you have any idea what I might be missing?

Nereg commented 4 months ago

For many games, there should be a configuration for that port. That's how they can host multiple servers on one IP. If the server doesn't show up in the steam response on that url , clients can't see the server on the server select screen, afaik. You could launch an nmap or simmular tool on your local instance to figure out which port it uses. The protocol is based on UDP, and I have a wireshark plugin for it somewhere.

Cameronsplaze commented 4 months ago

For many games, there should be a configuration for that port. That's how they can host multiple servers on one IP

Do you mean the info 2457 port? If so it's the default, and I get a response from it locally. It (and game port 2456) are unblocked in the cloud too.

I forgot to mention, my friends and I can connect to the container when it's in the cloud too, it's just getting a2s.info back that's the problem.

I have a wireshark plugin for it somewhere.

Oh that's a really interesting idea! I found this guide, maybe I can point that to the container locally and see more info

clients can't see the server on the server select screen, afaik

Actually does it matter if the server is public vs private? I have a config in the cloud I launch the server with, and force it to be private. I bet when I tried it locally, I used all the default settings and it created a public server. Let me try that real fast

Cameronsplaze commented 4 months ago

Running it as a public server in the cloud is doing the same thing, timing out. Probably a good thing at least, it might mean I can still do private servers w/ friends

Cameronsplaze commented 4 months ago

Maybe it's not firewall related... When I run locally:

docker container run -p 127.0.0.1:2456-2457:2456-2457/udp lloesche/valheim-server

With ONLY 2456-2457/udp open inward locally, I'm able to connect:

>>> import a2s
>>> a2s.info(("127.0.0.1", 2457))
SourceInfo(protocol=17, server_name='My Server', map_name='My Server', folder='valheim', game='', app_id=0, player_count=0, max_players=64, bot_count=0, server_type='d', platform='l', password_protected=True, vac_enabled=False, version='1.0.0.0', edf=177, port=2456, steam_id=90198416891312159, stv_port=None, stv_name=None, keywords='g=0.218.15,n=27,m=', game_id=892970, ping=0.0459046190007939

Also confirmed with docker ps on both systems, PORTS are the same on both...

Cameronsplaze commented 4 months ago

Good news is I think I've eliminated this package being the problem. If I manually start a ec2 instance, and run docker on it, I can connect to that valheim server like expected. It's only when the ecs places the ec2 task on the instance that this happens. At least that means AWS's ip's aren't being blocked by valve or something.

Thanks!

Cameronsplaze commented 4 months ago

I must have did my test before wrong, I confirmed it's SERVER_PUBLIC: False blocking a2s requests for Valheim servers. Testing again confirmed it, and there's this block from the Valheim container code showing they have to do something special to get player count too.