@@ -14,7 +14,7 @@ class SocketServer:
14
14
kick_message = None
15
15
samples = None
16
16
server_icon = None
17
- s = None
17
+ sock = None
18
18
19
19
def __init__ (self , ip , port , motd , version_text , kick_message , samples , server_icon , logger ):
20
20
self .ip = ip
@@ -28,67 +28,73 @@ def __init__(self, ip, port, motd, version_text, kick_message, samples, server_i
28
28
29
29
def on_new_client (self , client_socket , addr ):
30
30
data = client_socket .recv (1024 )
31
- (length , i ) = byte_utils .read_varint (data , 0 )
32
- (packetID , i ) = byte_utils .read_varint (data , i )
33
-
34
- if packetID == 0 :
35
- (version , i ) = byte_utils .read_varint (data , i )
36
- (ip , i ) = byte_utils .read_utf (data , i )
37
-
38
- ip = ip .replace ('\x00 ' , '' )
39
- is_using_fml = False
40
-
41
- if ip .endswith ("FML" ):
42
- is_using_fml = True
43
- ip = ip [:- 3 ]
44
-
45
- (port_tuple , i ) = byte_utils .read_ushort (data , i )
46
- (state , i ) = byte_utils .read_varint (data , i )
47
- if state == 1 :
48
- self .logger .info (("[%s:%s] Received client " + ("(using ForgeModLoader) " if is_using_fml else "" ) +
49
- "ping packet (%s:%s)." ) % (addr [0 ], addr [1 ], ip , port_tuple [0 ]))
50
- motd = {}
51
- motd ["version" ] = {}
52
- motd ["version" ]["name" ] = self .version_text
53
- motd ["version" ]["protocol" ] = 2
54
- motd ["players" ] = {}
55
- motd ["players" ]["max" ] = 0
56
- motd ["players" ]["online" ] = 0
57
- motd ["players" ]["sample" ] = []
58
-
59
- for sample in self .samples :
60
- motd ["players" ]["sample" ].append ({"name" : sample , "id" : str (uuid .uuid4 ())})
61
-
62
- motd ["description" ] = {"text" : self .motd }
63
-
64
- if self .server_icon and len (self .server_icon ) > 0 :
65
- motd ["favicon" ] = self .server_icon
66
-
67
- self .write_response (client_socket , json .dumps (motd ))
68
- elif state == 2 :
69
- name = ""
70
- if len (data ) != i :
71
- (some_int , i ) = byte_utils .read_varint (data , i )
72
- (some_int , i ) = byte_utils .read_varint (data , i )
73
- (name , i ) = byte_utils .read_utf (data , i )
74
- self .logger .info (
75
- ("[%s:%s] " + (name + " t" if len (name ) > 0 else "T" ) + "ries to connect to the server " +
76
- ("(using ForgeModLoader) " if is_using_fml else "" ) + "(%s:%s)." )
77
- % (addr [0 ], addr [1 ], ip , port_tuple [0 ]))
78
- self .write_response (client_socket , json .dumps ({"text" : self .kick_message }))
31
+
32
+ try :
33
+ (length , i ) = byte_utils .read_varint (data , 0 )
34
+ (packetID , i ) = byte_utils .read_varint (data , i )
35
+
36
+ if packetID == 0 :
37
+ (version , i ) = byte_utils .read_varint (data , i )
38
+ (ip , i ) = byte_utils .read_utf (data , i )
39
+
40
+ ip = ip .replace ('\x00 ' , '' )
41
+ is_using_fml = False
42
+
43
+ if ip .endswith ("FML" ):
44
+ is_using_fml = True
45
+ ip = ip [:- 3 ]
46
+
47
+ (port , i ) = byte_utils .read_ushort (data , i )
48
+ (state , i ) = byte_utils .read_varint (data , i )
49
+ if state == 1 :
50
+ self .logger .info (("[%s:%s] Received client " + ("(using ForgeModLoader) " if is_using_fml else "" ) +
51
+ "ping packet (%s:%s)." ) % (addr [0 ], addr [1 ], ip , port ))
52
+ motd = {}
53
+ motd ["version" ] = {}
54
+ motd ["version" ]["name" ] = self .version_text
55
+ motd ["version" ]["protocol" ] = 2
56
+ motd ["players" ] = {}
57
+ motd ["players" ]["max" ] = 0
58
+ motd ["players" ]["online" ] = 0
59
+ motd ["players" ]["sample" ] = []
60
+
61
+ for sample in self .samples :
62
+ motd ["players" ]["sample" ].append ({"name" : sample , "id" : str (uuid .uuid4 ())})
63
+
64
+ motd ["description" ] = {"text" : self .motd }
65
+
66
+ if self .server_icon and len (self .server_icon ) > 0 :
67
+ motd ["favicon" ] = self .server_icon
68
+
69
+ self .write_response (client_socket , json .dumps (motd ))
70
+ elif state == 2 :
71
+ name = ""
72
+ if len (data ) != i :
73
+ (some_int , i ) = byte_utils .read_varint (data , i )
74
+ (some_int , i ) = byte_utils .read_varint (data , i )
75
+ (name , i ) = byte_utils .read_utf (data , i )
76
+ self .logger .info (
77
+ ("[%s:%s] " + (name + " t" if len (name ) > 0 else "T" ) + "ries to connect to the server " +
78
+ ("(using ForgeModLoader) " if is_using_fml else "" ) + "(%s:%s)." )
79
+ % (addr [0 ], addr [1 ], ip , port ))
80
+ self .write_response (client_socket , json .dumps ({"text" : self .kick_message }))
81
+ else :
82
+ self .logger .info (
83
+ "[%s:%d] Tried to request a login/ping with an unknown state: %d" % (addr [0 ], addr [1 ], state ))
84
+ elif packetID == 1 :
85
+ (long , i ) = byte_utils .read_long (data , i )
86
+ response = bytearray ()
87
+ byte_utils .write_varint (response , 9 )
88
+ byte_utils .write_varint (response , 1 )
89
+ bytearray .append (long )
90
+ client_socket .sendall (bytearray )
91
+ self .logger .info ("[%s:%d] Responded with pong packet." % (addr [0 ], addr [1 ]))
79
92
else :
80
- self .logger .info (
81
- "[%s:%d] Tried to request a login/ping with an unknown state: %d" % (addr [0 ], addr [1 ], state ))
82
- elif packetID == 1 :
83
- (long , i ) = byte_utils .read_long (data , i )
84
- response = bytearray ()
85
- byte_utils .write_varint (response , 9 )
86
- byte_utils .write_varint (response , 1 )
87
- bytearray .append (long )
88
- client_socket .sendall (bytearray )
89
- self .logger .info ("[%s:%d] Responded with pong packet." % (addr [0 ], addr [1 ]))
90
- else :
91
- self .logger .warning ("[%s:%d] Sent an unexpected packet: %d" % (addr [0 ], addr [1 ], packetID ))
93
+ self .logger .warning ("[%s:%d] Sent an unexpected packet: %d" % (addr [0 ], addr [1 ], packetID ))
94
+ except TypeError :
95
+ self .logger .warning ("An invalid data was sent (%s)" % data )
96
+ return
97
+
92
98
93
99
def write_response (self , client_socket , response ):
94
100
response_array = bytearray ()
@@ -100,14 +106,15 @@ def write_response(self, client_socket, response):
100
106
client_socket .sendall (response_array )
101
107
102
108
def start (self ):
103
- self .s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
104
- self .s .bind ((self .ip , self .port ))
105
- self .s .settimeout (5000 )
106
- self .s .listen (30 )
109
+ self .sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
110
+ self .sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
111
+ self .sock .bind ((self .ip , self .port ))
112
+ self .sock .settimeout (5000 )
113
+ self .sock .listen (30 )
107
114
self .logger .info ("Server started on %s:%s! Waiting for incoming connections..." % (self .ip , self .port ))
108
115
while 1 :
109
- (client , address ) = self .s .accept ()
116
+ (client , address ) = self .sock .accept ()
110
117
_thread .start_new_thread (self .on_new_client , (client , address ,))
111
118
112
119
def close (self ):
113
- self .s .close ()
120
+ self .sock .close ()
0 commit comments