forked from StarryPy/StarryPy-Python2-Deprecated
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
902 lines (763 loc) · 27.3 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
# -*- coding: UTF-8 -*-
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
from _socket import SHUT_RDWR
import locale
import logging
import logging.handlers
from uuid import uuid4
import sys
import socket
import datetime
from twisted.internet import reactor
from twisted.internet.error import CannotListenError
from twisted.internet.protocol import (
ClientFactory,
ServerFactory,
Protocol,
connectionDone
)
from twisted.internet.task import LoopingCall
from construct import Container
import construct.core
from packets import Packets, Direction, chat_received
from config import ConfigurationManager
from packet_stream import PacketStream
import packets
from plugin_manager import PluginManager, route, FatalPluginError
from utility_functions import build_packet
VERSION = '1.7.2'
VDEBUG_LVL = 9
logging.addLevelName(VDEBUG_LVL, 'VDEBUG')
def vdebug(self, message, *args, **kws):
if self.isEnabledFor(VDEBUG_LVL):
self._log(VDEBUG_LVL, message, args, **kws)
logging.Logger.vdebug = vdebug
def port_check(upstream_hostname, upstream_port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((upstream_hostname, upstream_port))
if result != 0:
sock.close()
return False
else:
sock.shutdown(SHUT_RDWR)
sock.close()
return True
class StarryPyServerProtocol(Protocol):
"""
The main protocol class for handling connections from Starbound clients.
"""
def __init__(self):
self.id = str(uuid4().hex)
logger.vdebug('Creating protocol with ID %s.', self.id)
self.factory.protocols[self.id] = self
self.player = None
self.state = None
logger.debug('Trying to initialize configuration manager.')
self.config = ConfigurationManager()
self.parsing = False
self.buffering_packet = None
self.after_write_callback = None
self.plugin_manager = None
self.call_mapping = {
Packets.PROTOCOL_VERSION: self.protocol_version,
Packets.SERVER_DISCONNECT: self.server_disconnect, # 1
Packets.CONNECT_SUCCESS: self.connect_success, # 2
Packets.CONNECT_FAILURE: self.connect_failure, # 3
Packets.HANDSHAKE_CHALLENGE: self.handshake_challenge, # 4
Packets.CHAT_RECEIVED: self.chat_received, # 5
Packets.UNIVERSE_TIME_UPDATE: self.universe_time_update, # 6
Packets.CELESTIAL_RESPONSE: lambda x: True, # 7
Packets.PLAYER_WARP_RESULT: self.player_warp_result, # 8
Packets.CLIENT_CONNECT: self.client_connect, # 9
Packets.CLIENT_DISCONNECT_REQUEST: (
self.client_disconnect_request # 10
),
Packets.HANDSHAKE_RESPONSE: self.handshake_response, # 11
Packets.PLAYER_WARP: self.player_warp, # 12
Packets.FLY_SHIP: self.fly_ship, # 13
Packets.CHAT_SENT: self.chat_sent, # 14
Packets.CELESTIAL_REQUEST: self.celestial_request, # 15
Packets.CLIENT_CONTEXT_UPDATE: self.client_context_update, # 16
Packets.WORLD_START: self.world_start, # 17
Packets.WORLD_STOP: self.world_stop, # 18
Packets.CENTRAL_STRUCTURE_UPDATE: (
self.central_structure_update # 19
),
Packets.TILE_ARRAY_UPDATE: self.tile_array_update, # 20
Packets.TILE_UPDATE: self.tile_update, # 21
Packets.TILE_LIQUID_UPDATE: self.tile_liquid_update, # 22
Packets.TILE_DAMAGE_UPDATE: self.tile_damage_update, # 23
Packets.TILE_MODIFICATION_FAILURE: (
self.tile_modification_failure # 24
),
Packets.GIVE_ITEM: self.give_item, # 25
Packets.SWAP_IN_CONTAINER_RESULT: (
self.swap_in_container_result # 26
),
Packets.ENVIRONMENT_UPDATE: self.environment_update, # 27
Packets.ENTITY_INTERACT_RESULT: self.entity_interact_result, # 28
Packets.UPDATE_TILE_PROTECTION: self.update_tile_protection, # 29
Packets.MODIFY_TILE_LIST: self.modify_tile_list, # 30
Packets.DAMAGE_TILE_GROUP: self.damage_tile_group, # 31
Packets.COLLECT_LIQUID: self.collect_liquid, # 32
Packets.REQUEST_DROP: self.request_drop, # 33
Packets.SPAWN_ENTITY: self.spawn_entity, # 34
Packets.ENTITY_INTERACT: self.entity_interact, # 35
Packets.CONNECT_WIRE: self.connect_wire, # 36
Packets.DISCONNECT_ALL_WIRES: self.disconnect_all_wires, # 37
Packets.OPEN_CONTAINER: self.open_container, # 38
Packets.CLOSE_CONTAINER: self.close_container, # 39
Packets.SWAP_IN_CONTAINER: self.swap_in_container, # 40
Packets.ITEM_APPLY_IN_CONTAINER: (
self.item_apply_in_container # 41
),
Packets.START_CRAFTING_IN_CONTAINER: (
self.start_crafting_in_container # 42
),
Packets.STOP_CRAFTING_IN_CONTAINER: (
self.stop_crafting_in_container # 43
),
Packets.BURN_CONTAINER: self.burn_container, # 44
Packets.CLEAR_CONTAINER: self.clear_container, # 45
Packets.WORLD_CLIENT_STATE_UPDATE: (
self.world_client_state_update # 46
),
Packets.ENTITY_CREATE: self.entity_create, # 47
Packets.ENTITY_UPDATE: self.entity_update, # 48
Packets.ENTITY_DESTROY: self.entity_destroy, # 49
Packets.HIT_REQUEST: self.hit_request, # 50
Packets.DAMAGE_REQUEST: lambda x: True, # 51
Packets.DAMAGE_NOTIFICATION: self.damage_notification, # 52
Packets.ENTITY_MESSAGE: lambda x: True, # 53
Packets.ENTITY_MESSAGE_RESPONSE: lambda x: True, # 54
Packets.UPDATE_WORLD_PROPERTIES: (
self.update_world_properties # 55
),
Packets.STEP_UPDATE: self.step_update, # 56
}
self.client_protocol = None
self.packet_stream = PacketStream(self)
self.packet_stream.direction = Direction.CLIENT
self.plugin_manager = self.factory.plugin_manager
def connectionMade(self):
"""
Called when the connection to the requesting client is actually
established.
After the connection is established, it attempts to connect to the
actual starbound server using StarboundClientFactory()
:rtype : None
"""
logger.info(
'Connection established from IP: %s',
self.transport.getPeer().host
)
reactor.connectTCP(
self.config.upstream_hostname,
self.config.upstream_port,
StarboundClientFactory(self),
timeout=self.config.server_connect_timeout
)
def string_received(self, packet):
"""
This method is called whenever a completed packet is received from the
client going to the Starbound server.
This is the first and only time where these packets can be modified,
stopped, or allowed.
Processing of parsed data is handled in handle_starbound_packets()
:rtype : None
"""
if 56 >= packet.id:
if self.handle_starbound_packets(packet):
self.client_protocol.transport.write(
packet.original_data)
if self.after_write_callback is not None:
self.after_write_callback()
else:
# We received an unknown packet; send it along.
logger.warning(
'Received unknown message ID (%d) from client.', packet.id
)
self.client_protocol.transport.write(
packet.original_data)
def dataReceived(self, data):
"""
Called whenever a packet is received. Generally this should not be
tampered with directly, as it attempts to reconstruct the packet
that Starbound clients send out.
The actual handling of the reconstructed packet should be done in
string_received(), which is called when the packet is built.
:param data: Raw packet data from Twisted.
:rtype : None
"""
if self.config.passthrough:
self.client_protocol.transport.write(data)
else:
self.packet_stream += data
@route
def protocol_version(self, data):
return True
@route
def server_disconnect(self, data):
return True
@route
def handshake_challenge(self, data):
return True
@route
def chat_received(self, data):
return True
@route
def universe_time_update(self, data):
return True
@route
def handshake_response(self, data):
return True
@route
def client_context_update(self, data):
return True
@route
def world_start(self, data):
return True
@route
def world_stop(self, data):
return True
@route
def central_structure_update(self, data):
return True
@route
def tile_array_update(self, data):
return True
@route
def tile_update(self, data):
return True
@route
def tile_liquid_update(self, data):
return True
@route
def tile_damage_update(self, data):
return True
@route
def tile_modification_failure(self, data):
return True
@route
def give_item(self, data):
return True
@route
def swap_in_container_result(self, data):
return True
@route
def environment_update(self, data):
return True
@route
def entity_interact_result(self, data):
return True
@route
def update_tile_protection(self, data):
return True
@route
def modify_tile_list(self, data):
return True
@route
def damage_tile(self, data):
return True
@route
def damage_tile_group(self, data):
return True
@route
def collect_liquid(self, data):
return True
@route
def request_drop(self, data):
return True
@route
def spawn_entity(self, data):
return True
@route
def entity_interact(self, data):
return True
@route
def connect_wire(self, data):
return True
@route
def disconnect_all_wires(self, data):
return True
@route
def open_container(self, data):
return True
@route
def close_container(self, data):
return True
@route
def swap_in_container(self, data):
return True
@route
def item_apply_in_container(self, data):
return True
@route
def start_crafting_in_container(self, data):
return True
@route
def stop_crafting_in_container(self, data):
return True
@route
def burn_container(self, data):
return True
@route
def clear_container(self, data):
return True
@route
def world_client_state_update(self, data):
return True
@route
def entity_create(self, data):
return True
@route
def entity_update(self, data):
return True
@route
def entity_destroy(self, data):
return True
@route
def hit_request(self, data):
return True
@route
def status_effect_request(self, data):
return True
@route
def update_world_properties(self, data):
return True
@route
def step_update(self, data):
return True
@route
def connect_success(self, data):
"""
Called when the server successfully connects with the client.
:param data: Parsed packet.
:rtype : bool
"""
return True
@route
def connect_failure(self, data):
"""
Called when the server fails to connect with the client.
:param data: Parsed packet.
:rtype : bool
"""
return True
@route
def chat_sent(self, data):
"""
Called when the client attempts to send a chat message/command to the
server.
:param data: Parsed chat packet.
:rtype : bool
"""
return True
@route
def celestial_request(self, data):
"""
Called when the client requests celestial data...?
:param data: Parsed chat packet.
:rtype : bool
"""
return True
@route
def damage_notification(self, data):
return True
@route
def client_connect(self, data):
"""
Called when the client attempts to connect to the Starbound server.
:param data: Parsed client_connect packet.
:rtype : bool
"""
return True
@route
def client_disconnect_request(self, player):
"""
Called when the client signals that it is about to disconnect from
the Starbound server.
:param player: The Player.
:rtype : bool
"""
return True
@route
def player_warp(self, data):
"""
Called when the players issues a warp.
:param data: The player_warp data.
:rtype : bool
"""
return True
@route
def player_warp_result(self, data):
"""
Called when the players begins to warp.
:param data: The player_warp data.
:rtype : bool
"""
return True
@route
def fly_ship(self, data):
"""
Called when the players moves their ship.
:param data: The fly_ship data.
:rtype : bool
"""
return True
def handle_starbound_packets(self, p):
"""
This function is the meat of it all. Every time a full packet with
a derived ID <= 56, it is passed through here.
"""
return self.call_mapping[p.id](p)
def send_chat_message(self, text, mode='BROADCAST', channel='', name=''):
"""
Convenience function to send chat messages to the client. Note that
this does *not* send messages to the server at large; broadcast should
be used for messages to all clients, or manually constructed chat
messages otherwise.
:param text: Message text, may contain multiple lines.
:param channel: The chat channel/context.
:param name: The name to display before the message. Blank leaves no
brackets, otherwise it will be displayed as `<name>`.
:return: None
"""
if '\n' in text:
lines = text.split('\n')
for line in lines:
self.send_chat_message(line)
return
if self.player is not None:
logger.vdebug(
'Calling send_chat_message from player %s on channel'
' %s with mode %s with reported username of %s with'
' message: %s',
self.player.name,
channel,
mode,
name,
text
)
chat_data = chat_received().build(
Container(
mode=mode,
channel=channel,
client_id=0,
name=name,
message=text.encode('utf-8')
)
)
logger.vdebug('Built chat payload. Data: %s', chat_data.encode('hex'))
chat_packet = build_packet(
packets.Packets.CHAT_RECEIVED, chat_data
)
logger.vdebug('Built chat packet. Data: %s', chat_packet.encode('hex'))
self.transport.write(chat_packet)
logger.vdebug('Sent chat message with text: %s', text)
def write(self, data):
"""
Convenience method to send data to the client.
:param data: Data to send.
:return: None
"""
self.transport.write(data)
def connectionLost(self, reason=connectionDone):
"""
Called as a pseudo-destructor when the connection is lost.
:param reason: The reason for the disconnection.
:return: None
"""
try:
logger.vdebug('Trying to disconnect protocol from factory')
if self.client_protocol is not None:
logger.vdebug('The client_protocol is not None')
x = build_packet(
packets.Packets.CLIENT_DISCONNECT_REQUEST,
packets.client_disconnect_request().build(
Container(data=0)
)
)
logger.vdebug('Disconnect packet has been built')
try:
if self.player is not None and self.player.logged_in:
logger.vdebug('Player not none and is still logged in')
self.client_disconnect_request(x)
logger.vdebug('Client disconnect requested')
except:
logger.error('Couldn\'t complete disconnect request.')
finally:
self.client_protocol.transport.write(x)
logger.vdebug('Kill packet written to transport protocol')
self.client_protocol.transport.abortConnection()
logger.vdebug('connection aborted')
self.player.logged_in = 0
logger.vdebug('Player status forced to logged_in=0')
except:
logger.error('Couldn\'t disconnect protocol.')
finally:
try:
self.factory.protocols.pop(self.id)
except:
logger.warning(
'Protocol was not in factory list. This should not happen.'
)
logger.info('protocol id: %s' % self.id)
finally:
logger.info(
'Lost connection from IP: %s',
self.transport.getPeer().host
)
logger.vdebug('Connection aborted')
self.transport.abortConnection()
def die(self):
self.connectionLost()
class ClientProtocol(Protocol):
"""
The protocol class which handles the connection to the Starbound server.
"""
def __init__(self):
self.packet_stream = PacketStream(self)
self.packet_stream.direction = packets.Direction.SERVER
logger.debug('Client protocol instantiated.')
def connectionMade(self):
"""
Called when the connection to the Starbound server is initially
established. Inserts a self-reference in the server_protocol to allow
two-way communication.
:return: None
"""
self.server_protocol.client_protocol = self
def string_received(self, packet):
"""
This method is called whenever a completed packet is received from the
Starbound server.
This is the first and only time where these packets can be modified,
stopped, or allowed.
Processing of parsed data is handled in handle_starbound_packets()
:return: None
"""
try:
if self.server_protocol.handle_starbound_packets(
packet):
self.server_protocol.write(packet.original_data)
except construct.core.FieldError:
logger.exception('Construct field error in string_received.')
self.server_protocol.write(
packet.original_data)
def dataReceived(self, data):
"""
Called whenever a packet is received. Generally this should not be
tampered with directly, as it attempts to reconstruct the packet
that the Starbound server sent out.
The actual handling of the reconstructed packet should be done in
string_received(), which is called when the packet is built.
:param data: Raw packet data from the Starbound server.
:return: None
"""
if self.server_protocol.config.passthrough:
self.server_protocol.write(data)
else:
self.packet_stream += data
def disconnect(self):
logger.vdebug('Client protocol disconnect called.')
x = build_packet(
packets.Packets.CLIENT_DISCONNECT_REQUEST,
packets.client_disconnect_request().build(Container(data=0))
)
self.transport.write(x)
self.transport.abortConnection()
logger.vdebug('Client protocol disconnected.')
class StarryPyServerFactory(ServerFactory):
"""
Factory which creates `StarryPyServerProtocol` instances.
"""
protocol = StarryPyServerProtocol
def __init__(self):
"""
Initializes persistent objects and prepares a list of connected
protocols.
"""
self.config = ConfigurationManager()
self.protocol.factory = self
self.protocols = {}
self.plugin_manager = PluginManager(factory=self)
try:
self.plugin_manager.prepare()
except FatalPluginError:
logger.critical('Shutting Down.')
sys.exit()
self.reaper = LoopingCall(self.reap_dead_protocols)
self.reaper.start(self.config.reap_time)
logger.debug(
'Factory created, endpoint of port %d', self.config.bind_port
)
def stopFactory(self):
"""
Called when the factory is stopped. Saves the configuration.
:return: None
"""
self.config.save()
self.plugin_manager.die()
def broadcast(self, text, name=''):
"""
Convenience method to send a broadcasted message to all clients on the
server.
:param text: Message text
:param name: The name to prepend before the message, format is <name>
:return: None
"""
for p in self.protocols.itervalues():
try:
p.send_chat_message(text)
except:
logger.exception('Exception in broadcast.')
def broadcast_planet(self, text, planet, name=''):
"""
Convenience method to send a broadcasted message to all clients on the
current planet (and ships orbiting it).
:param text: Message text
:param planet: The planet to send the message to
:param name: The name to prepend before the message, format is <name>,
not prepended when empty
:return: None
"""
for p in self.protocols.itervalues():
if p.player.planet == planet:
try:
p.send_chat_message(text)
except:
logger.exception('Exception in broadcast.')
def buildProtocol(self, address):
"""
Builds the protocol to a given address.
:rtype : Protocol
"""
logger.vdebug('Building protocol to address %s', address)
p = ServerFactory.buildProtocol(self, address)
return p
def reap_dead_protocols(self):
logger.vdebug('Reaping dead connections.')
count = 0
start_time = datetime.datetime.now()
for protocol in self.protocols.itervalues():
total_seconds = (
protocol
.client_protocol
.packet_stream
.last_received_timestamp -
start_time
).total_seconds()
if total_seconds > self.config.reap_time:
logger.debug(
'Reaping protocol %s. Reason: Server protocol timeout.',
protocol.id
)
protocol.connectionLost()
count += 1
continue
if (
protocol.client_protocol is not None and
total_seconds > self.config.reap_time
):
protocol.connectionLost()
logger.debug(
'Reaping protocol %s. Reason: Client protocol timeout.',
protocol.id
)
count += 1
if count == 1:
logger.info('1 connection reaped.')
elif count > 1:
logger.info('%d connections reaped.')
else:
logger.vdebug('No connections reaped.')
class StarboundClientFactory(ClientFactory):
"""
Factory which creates `StarboundClientProtocol` instances.
"""
protocol = ClientProtocol
def __init__(self, server_protocol):
logger.vdebug('Client protocol instantiated in client factory.')
self.server_protocol = server_protocol
def buildProtocol(self, address):
logger.vdebug(
'Building protocol in StarboundClientFactory to address %s',
address
)
protocol = ClientFactory.buildProtocol(self, address)
protocol.server_protocol = self.server_protocol
return protocol
def init_localization():
try:
locale.setlocale(locale.LC_ALL, '')
except:
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
if __name__ == '__main__':
init_localization()
print('Attempting initialization of configuration manager singleton.')
config = ConfigurationManager()
logger = logging.getLogger('starrypy')
logger.setLevel(9)
log_format = logging.Formatter(
'%(asctime)s - %(levelname)s - %(name)s # %(message)s'
)
if config.log_level == 'DEBUG':
log_level = logging.DEBUG
elif config.log_level == 'VDEBUG':
log_level = 'VDEBUG'
else:
log_level = logging.INFO
print('Setup console logging...')
console_handle = logging.StreamHandler(sys.stdout)
console_handle.setLevel(log_level)
logger.addHandler(console_handle)
console_handle.setFormatter(log_format)
print('Setup file-based logging...')
logfile_handle = logging.handlers.TimedRotatingFileHandler(
'server.log', when='midnight', interval=5, backupCount=4
)
logfile_handle.setLevel(log_level)
logger.addHandler(logfile_handle)
logfile_handle.setFormatter(log_format)
if config.port_check:
logger.debug(
'Port check enabled. Performing port check to %s:%d',
config.upstream_hostname,
config.upstream_port
)
if not port_check(config.upstream_hostname, config.upstream_port):
logger.critical(
'The starbound server is not connectable at the address'
' %s:%d.', config.upstream_hostname, config.upstream_port
)
logger.critical(
'Please ensure that you are running starbound_server on the '
'correct port and that is reflected in the StarryPy '
'configuration.'
)
sys.exit()
logger.debug('Port check succeeded. Continuing.')
logger.info('Started StarryPy server version %s', VERSION)
factory = StarryPyServerFactory()
logger.debug(
'Attempting to listen on TCP port %d', factory.config.bind_port
)
try:
reactor.listenTCP(
factory.config.bind_port,
factory,
interface=factory.config.bind_address
)
except CannotListenError:
logger.critical(
'Cannot listen on TCP port %d. Exiting.', factory.config.bind_port
)
sys.exit()
logger.info('Listening on port %s', factory.config.bind_port)
reactor.run()