Skip to content

Commit

Permalink
Now stores logs
Browse files Browse the repository at this point in the history
Can be disabled in settings
  • Loading branch information
Thorinwasher committed Mar 31, 2021
1 parent 5bb826b commit b0dd5ab
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 39 deletions.
5 changes: 3 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


# Version of this config. Please do not change.
configVersion: 4
configVersion: 5

# Where do you want phantom to run?
serverInfo:
Expand Down Expand Up @@ -73,7 +73,7 @@ Content:
imagePath: 'server-icon.png'
# Text shown above the server (Max 29 Chars.)

#NOT implemented yet

Logging:
# Users that view the server's MOTD.
pings:
Expand All @@ -86,3 +86,4 @@ Logging:
# Generates a list of unique users; joins.log
log: false

debug: true
19 changes: 9 additions & 10 deletions pha_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
@author: Thorin
"""
import struct
from pha_logging import info,debug

class connection_manager:

def __init__(self,conn,ajson_creator):
def __init__(self,conn,ajson_creator,logger):
self.json_creator = ajson_creator
self.conn = conn

self.logger = logger
#recieve handshake
packet_length = self.unpack_varint();
self.packet_id = self.unpack_varint() #TODO check if invalid
Expand All @@ -23,7 +22,7 @@ def __init__(self,conn,ajson_creator):
self.client_port = self.conn.recv(2)
self.state = self.unpack_varint()

info("Connected to",self.client_address,"at port",self.client_port)
self.logger.info("Connected to",self.client_address,"at port",struct.unpack("H", self.client_port))
def pack_varint(self,data):
""" Pack the var int """
ordinal = b''
Expand Down Expand Up @@ -81,22 +80,22 @@ def do_response(self):
def status_connection(self):
while True:
data = self.read_fully()
debug("Recieved",str(data))
self.logger.debug("Recieved",str(data))
if data == b'\x00':
self.conn.sendall(self.write_response())
debug("Sent JSON response")
self.logger.debug("Sent JSON response")
elif data == b'':
break
else:
self.conn.sendall(self.pack_varint(len(data)) + data)
debug("Responded to ping.")
self.logger.debug("Responded to ping.")
break

def interpret_login(self):
packet_length = self.unpack_varint()
self.packet_id = self.unpack_varint()
self.username = self.unpack_string()
info(self.username , "tried to establish a connection")
self.logger.info(self.username , "tried to establish a connection")

def compile_disconnect_data(self):
chat_data = self.pack_string(self.json_creator.get_disconnect_dictionary_string())
Expand All @@ -106,8 +105,8 @@ def compile_disconnect_data(self):

def login_connection(self):
recieved_data = self.interpret_login()
debug("Recieved",str(recieved_data))
self.logger.debug("Recieved",str(recieved_data))
final_data = self.compile_disconnect_data();
debug("Sent JSON disconnect message")
self.logger.debug("Sent JSON disconnect message")
self.conn.sendall(final_data)

4 changes: 1 addition & 3 deletions pha_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import re
import base64
import json
from pha_logging import info
class json_creator:

def __init__(self,config):
def __init__(self,config,logger):
self.Content = config["Content"]
self.Style = config["Style"]
info("Loaded style",self.Style)
self.load_base64()
self.create_Response_dictionary()
self.disconnect_dictionary = {"text":self.Content["kickMessage"]}
Expand Down
63 changes: 47 additions & 16 deletions pha_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
@author: Thorin
"""
from time import gmtime, strftime
import os
from os import path

is_debug = False

def list_to_string(alist):
output = ""
Expand All @@ -23,27 +24,57 @@ def list_to_string(alist):
output = output + " " + item

return output
def info(*msg):
end_msg = list_to_string(msg)
print("["+ strftime("%H:%M:%S", gmtime())+"]" + " INFO",end_msg)

def debug(*msg):
if is_debug:
print("["+ strftime("%H:%M:%S", gmtime())+"]" + " DEBUG",list_to_string(msg))



class logger:
def __init__(self,version):
print("----------------------")
print("| Phantom server |")
print("| Version " + version +" |")
print("----------------------")
def __init__(self,version,config):

if not os.path.exists("log"):
os.mkdir("log")

self.log_pings = config["Logging"]["pings"]["log"]
self.is_debug = config["debug"]
self.file_path = "log"
self.create_new_log()

msg = "----------------------\n" + "| Phantom server |\n"+"| Version " + version +" |\n"+"----------------------"
print (msg)
self.write_to_file(msg)
msg = "[debug = " + str(self.is_debug) + ", style = " + str(config["Style"])+"]\n"
print(msg)
self.write_to_file(msg)

def info(self,*msg):
end_msg = "["+ strftime("%H:%M:%S", gmtime())+" INFO ] " + list_to_string(msg)

print(end_msg)
self.write_to_file(end_msg)

def debug(self,*msg):
if self.is_debug:
end_msg = "["+ strftime("%H:%M:%S", gmtime())+" Debug]" + list_to_string(msg)
self.write_to_file(end_msg)
print(end_msg)

def register_ping(self,client_port,client_address):
code = True

self.debug("Connected to",client_address,"at port",client_port)



def create_new_log(self):
if path.exists(self.file_path+"/pings.log"):
self.rename_old_log()
def rename_old_log(self):
i = 1
while path.exists(self.file_path+"/pings"+ str(i) +".old"):
i += 1
os.rename(self.file_path+"/pings.log", self.file_path+"/pings"+ str(i) +".old")

def write_to_file(self,msg):
if not self.log_pings:
return

with open(self.file_path + "/pings.log","a") as file:
file.writelines(msg + "\n")


19 changes: 11 additions & 8 deletions phantom.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import os
from pha_connection import connection_manager
from pha_json import json_creator
from pha_logging import info,logger,debug
from pha_logging import logger
from os import path



Version = "0.5.5"

defaultConfig = {
"configVersion" : 4,
"configVersion" : 5,
"serverInfo" : {
"host" : "localhost",
"port" : 25565
Expand All @@ -37,16 +37,19 @@
"list" : True,
"log" : False
}
}
},
"debug" : False
}


class phantom:
def __init__(self):
self.logger = logger(Version)

while not self.get_config():
continue
self.json_creator = json_creator(self.config)

self.logger = logger(Version,self.config)
self.json_creator = json_creator(self.config,self.logger)
self.host = self.config["serverInfo"]["host"]
self.port = self.config["serverInfo"]["port"]
"""
Expand All @@ -59,7 +62,7 @@ def get_config(self):
if path.exists("config.yml"):
return self.load_config()
else:
info("No config was found, provididing a shittier one")
print("No config was found, provididing a shittier one")
self.write_config()
return False

Expand All @@ -76,7 +79,7 @@ def load_config(self):
return False

def rename_config(self):
info("Providing you with a newer config")
print("Providing you with a newer config")
if path.exists("config.old"):
os.remove("config.old")
os.rename("config.yml", "config.old")
Expand All @@ -95,7 +98,7 @@ def write_config(self):

def connection_actions(self,conn):
try:
conn_mngr = connection_manager(conn,self.json_creator)
conn_mngr = connection_manager(conn,self.json_creator,self.logger)
conn_mngr.do_response()
finally:
conn.close()
Expand Down

0 comments on commit b0dd5ab

Please sign in to comment.