Created
April 28, 2020 21:24
-
-
Save monester/b6a397d78ff8b34913e6f4783d0cbd7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import struct | |
import select | |
import sys | |
packet_id = 0 | |
SERVERDATA_AUTH = 3 | |
SERVERDATA_AUTH_RESPONSE = 2 | |
SERVERDATA_EXECCOMMAND = 2 | |
SERVERDATA_RESPONSE_VALUE = 0 | |
def make_message(msg, type): | |
global packet_id | |
length = len(msg) + 10 | |
data = struct.pack('iii', length, packet_id, type) + msg.encode() + b'\x00\x00' | |
packet_id += 1 | |
return data | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect(('127.0.0.1', 27015)) | |
password_data = make_message('test', SERVERDATA_AUTH) | |
s.send(password_data) | |
while True: | |
socket_list = [sys.stdin, s] | |
# Get the list sockets which are readable | |
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], []) | |
for sock in read_sockets: | |
#incoming message from remote server | |
if sock == s: | |
length = int.from_bytes(sock.recv(4), 'little') | |
data = sock.recv(length) | |
if not data: | |
print('\nDisconnected from server') | |
sys.exit() | |
else: | |
try: | |
print('Data: "%s"' % data[8:-2].decode().strip()) | |
except Exception as e: | |
print('Unable to decode: %r: %s' % (data[8:-2], e)) | |
#user entered a message | |
else: | |
msg = sys.stdin.readline().strip() | |
if msg: | |
if not msg.startswith('/'): | |
msg = '/silent-command ' + msg | |
print('Sending command: "%s"' % msg) | |
s.send(make_message(msg, SERVERDATA_EXECCOMMAND)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment