Skip to content

Instantly share code, notes, and snippets.

@ezklap
Last active April 19, 2024 14:51
Show Gist options
  • Save ezklap/1925522a0970690679467e2d01458f96 to your computer and use it in GitHub Desktop.
Save ezklap/1925522a0970690679467e2d01458f96 to your computer and use it in GitHub Desktop.
Apex Legends IP logger
from scapy.all import *
import time
from pynput.keyboard import Key, KeyCode, Listener
import socket
'''
A python 3 script that captures the game servers IP address written for Apex Legends
you can use this for any game just look at the UDP port via wireshark and change it
on the script.
Matchmaking issue : I wrote a python script that helped me log the game servers IP addresses i was connecting to, as I had noticed 2 out of 3 matches in ranked mode I was connecting to LAGY servers , turns out Apex Matchmaking connected me to servers on the other side of the planet (400+ ms pings) and not close to the region I had selected in the game's settings.
With the lagy server IP addresses in hand I was able to control which servers not to connect to, I use PeerBlock (an open source firewall) to block out the bad servers IP range. i.e 34.56.0.0 - 34.56.255.255
Hope this helps others out there that face the same issue.
The script is a python 3 console application you run it in windows command line or python 3 IDLE it also logs the IP address in a text file where the script resides.
Things you will need python dependencies
scapy module
pynput module
Also you will need winpcap to capture the network traffic http://www.winpcap.org/ do not get confused with winpcapy python module
To capture the IP address run the script, and once your in a match use hotkey (Shift + L )
you can trace the geo location of the ip using any site,a good site I use https://ipstack.com/
The script does the following
1- Gets your PC's IP address (to compare it later)
2- captures a UDP network packet on port 37005
(The network packet holds 2 IP addresses source and destination your machine and the servers)
4- the script compares which of the two IP addresses to log
5- after comparing which IP from the packet belongs to the servers it prints it out and logs the IP address to a text file. the log file will be in the same folder where you have the script saved.
Usage:
Run the console script and use it when your in a match hold SHIFT and press L
'''
# getting out local IP address
def get_ip():
# get the local machines IP address
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
global local_ip
local_ip = get_ip()
def pc(packet):
if packet.proto == 17:
udp = packet.payload
# Taken from source https://nitratine.net/blog/post/how-to-make-hotkeys-in-python/
def function_1():
# looking for UDP packets sent to port 37005 Apex Legends servers
x = sniff(filter="udp and portrange 37005-37005", prn=pc, store=1, count=1)
y = x[0][IP].src
z = x[0][IP].dst
if local_ip in y:
dest_ip = z
else:
dest_ip = y
print('%r Game server IP: %r' % (strftime("%H:%M:%S", time.localtime()), dest_ip))
with open('ApexServerIPList.txt', 'a') as the_file:
the_file.write(dest_ip + '\n')
# Create a mapping of keys to function (use frozenset as sets are not hashable - so they can't be used as keys)
combination_to_function = {
frozenset([Key.shift, KeyCode(char='l')]): function_1, # No `()` after function_1 because we want to pass the function, not the value of the function
frozenset([Key.shift, KeyCode(char='L')]): function_1,
}
# Currently pressed keys
current_keys = set()
def on_press(key):
# When a key is pressed, add it to the set we are keeping track of and check if this set is in the dictionary
current_keys.add(key)
if frozenset(current_keys) in combination_to_function:
# If the current set of keys are in the mapping, execute the function
combination_to_function[frozenset(current_keys)]()
def on_release(key):
# When a key is released, remove it from the set of keys we are keeping track of
current_keys.remove(key)
with Listener(on_press=on_press, on_release=on_release) as listener:
print('Hold Shift and Press L to get the IP address')
listener.join()
@the-only-rafa
Copy link

I dont know anything about coding, was having problems with my apex and tried to get your script running, when I was in game nothing was happening while clicking shift L, then eventually this came up:

Microsoft Windows [Version 10.0.18363.657]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\Administrator>cd desktop/script

C:\Users\Administrator\Desktop\script>dir
Volume in drive C is BOOTCAMP
Volume Serial Number is DF5B-6C00

Directory of C:\Users\Administrator\Desktop\script

19/02/2020 16:43

.
19/02/2020 16:43 ..
19/02/2020 16:29 4,122 iplog.py
1 File(s) 4,122 bytes
2 Dir(s) 171,347,709,952 bytes free

C:\Users\Administrator\Desktop\script>python iplog.py
Hold Shift and Press L to get the IP address
Unhandled exception in listener callback
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput_util_init_.py", line 162, in inner
return f(self, *args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput\keyboard_win32.py", line 277, in process
self.on_release(key)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput_util_init
.py", line 78, in inner
if f(*args) is False:
File "iplog.py", line 97, in on_release
current_keys.remove(key)
KeyError: <Key.shift: <160>>
Traceback (most recent call last):
File "iplog.py", line 101, in
listener.join()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput_util_init_.py", line 210, in join
six.reraise(exc_type, exc_value, exc_traceback)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput_util_init_.py", line 162, in inner
return f(self, *args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput\keyboard_win32.py", line 277, in process
self.on_release(key)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput_util_init
.py", line 78, in inner
if f(*args) is False:
File "iplog.py", line 97, in on_release
current_keys.remove(key)
KeyError: <Key.shift: <160>>

any help to what I have done wrong would be great, wanna see if there is packet loss between me and this IP cause my connection is great anywhere else

@KhaliaKing
Copy link

Can you upload a step by step video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment