Guest User

Untitled

a guest
Nov 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. from miniboa import TelnetServer
  2.  
  3. IDLE_TIMEOUT = 300
  4. CLIENT_LIST = []
  5. SERVER_RUN = True
  6. LOGIN_LIST = []
  7.  
  8.  
  9. def on_connect(client):
  10. """
  11. Sample on_connect function.
  12. Handles new connections.
  13. """
  14. print "++ Opened connection to %s" % client.addrport()
  15. broadcast('%s joins the conversation.\n' % client.addrport() )
  16. CLIENT_LIST.append(client)
  17. client.send("Welcome to CTF Flags Server, %s.\n" % client.addrport() )
  18. client.log_level = 1
  19.  
  20. def on_disconnect(client):
  21. """
  22. Sample on_disconnect function.
  23. Handles lost connections.
  24. """
  25. print "-- Lost connection to %s" % client.addrport()
  26. CLIENT_LIST.remove(client)
  27. broadcast('%s leaves the conversation.\n' % client.addrport() )
  28.  
  29.  
  30. def kick_idle():
  31. """
  32. Looks for idle clients and disconnects them by setting active to False.
  33. """
  34. ## Who hasn't been typing?
  35. for client in CLIENT_LIST:
  36. if client.idle() > IDLE_TIMEOUT:
  37. print('-- Kicking idle lobby client from %s' % client.addrport())
  38. client.active = False
  39.  
  40.  
  41. def process_clients():
  42. """
  43. Check each client, if client.cmd_ready == True then there is a line of
  44. input available via client.get_command().
  45. """
  46. for client in CLIENT_LIST:
  47. if client.active and client.cmd_ready:
  48. ## If the client sends input echo it to the chat room
  49. chat(client)
  50.  
  51.  
  52. def broadcast(msg):
  53. """
  54. Send msg to every client.
  55. """
  56. for client in CLIENT_LIST:
  57. client.send(msg)
  58.  
  59.  
  60. def login(client,cmd):
  61.  
  62. if client.log_level == 1:
  63. client.log_level += 1
  64. client.send('You not in system.\n')
  65. client.send('login: ')
  66. elif client.log_level == 2:
  67. client.log_level += 1
  68. client.login = cmd
  69. client.send('password:')
  70. elif client.log_level == 3:
  71. client.password = cmd
  72. if client.login == 'lol' and client.password == 'lol':
  73. client.send('congratulations!')
  74. LOGIN_LIST.append(client)
  75. client.log_level = 0
  76. else:
  77. client.log_level = 1
  78. client.send('access denied!\n')
  79.  
  80.  
  81. def chat(client):
  82. """
  83. Echo whatever client types to everyone.
  84. """
  85. global SERVER_RUN
  86. msg = client.get_command()
  87. # print '%s says, "%s"' % (client.addrport(), msg)
  88. #
  89. # for guest in CLIENT_LIST:
  90. # if guest != client:
  91. # guest.send('%s says, %s\n' % (client.addrport(), msg))
  92. # else:
  93. # guest.send('You say, %s\n' % msg)
  94.  
  95. cmd = msg.lower()
  96.  
  97. if client not in LOGIN_LIST:
  98. login(client,cmd)
  99.  
  100. ## bye = disconnect
  101. if cmd == 'bye':
  102. client.active = False
  103. ## shutdown == stop the server
  104. # elif cmd == 'shutdown':
  105. # SERVER_RUN = False
  106.  
  107.  
  108. #------------------------------------------------------------------------------
  109. # Main
  110. #------------------------------------------------------------------------------
  111.  
  112. if __name__ == '__main__':
  113.  
  114. ## Simple chat server to demonstrate connection handling via the
  115. ## async and telnet modules.
  116.  
  117. ## Create a telnet server with a port, address,
  118. ## a function to call with new connections
  119. ## and one to call with lost connections.
  120.  
  121. telnet_server = TelnetServer(
  122. port=40021,
  123. address='0.0.0.0',
  124. on_connect=on_connect,
  125. on_disconnect=on_disconnect,
  126. timeout = .05
  127. )
  128.  
  129. print(">> Listening for connections on port %d. CTRL-C to break."
  130. % telnet_server.port)
  131.  
  132. ## Server Loop
  133. while SERVER_RUN:
  134. telnet_server.poll() ## Send, Recv, and look for new connections
  135. kick_idle() ## Check for idle clients
  136. process_clients() ## Check for client input
  137.  
  138. print(">> Server shutdown.")
Add Comment
Please, Sign In to add comment