Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from miniboa import TelnetServer
- IDLE_TIMEOUT = 300
- CLIENT_LIST = []
- SERVER_RUN = True
- LOGIN_LIST = []
- def on_connect(client):
- """
- Sample on_connect function.
- Handles new connections.
- """
- print "++ Opened connection to %s" % client.addrport()
- broadcast('%s joins the conversation.\n' % client.addrport() )
- CLIENT_LIST.append(client)
- client.send("Welcome to CTF Flags Server, %s.\n" % client.addrport() )
- client.log_level = 1
- def on_disconnect(client):
- """
- Sample on_disconnect function.
- Handles lost connections.
- """
- print "-- Lost connection to %s" % client.addrport()
- CLIENT_LIST.remove(client)
- broadcast('%s leaves the conversation.\n' % client.addrport() )
- def kick_idle():
- """
- Looks for idle clients and disconnects them by setting active to False.
- """
- ## Who hasn't been typing?
- for client in CLIENT_LIST:
- if client.idle() > IDLE_TIMEOUT:
- print('-- Kicking idle lobby client from %s' % client.addrport())
- client.active = False
- def process_clients():
- """
- Check each client, if client.cmd_ready == True then there is a line of
- input available via client.get_command().
- """
- for client in CLIENT_LIST:
- if client.active and client.cmd_ready:
- ## If the client sends input echo it to the chat room
- chat(client)
- def broadcast(msg):
- """
- Send msg to every client.
- """
- for client in CLIENT_LIST:
- client.send(msg)
- def login(client,cmd):
- if client.log_level == 1:
- client.log_level += 1
- client.send('You not in system.\n')
- client.send('login: ')
- elif client.log_level == 2:
- client.log_level += 1
- client.login = cmd
- client.send('password:')
- elif client.log_level == 3:
- client.password = cmd
- if client.login == 'lol' and client.password == 'lol':
- client.send('congratulations!')
- LOGIN_LIST.append(client)
- client.log_level = 0
- else:
- client.log_level = 1
- client.send('access denied!\n')
- def chat(client):
- """
- Echo whatever client types to everyone.
- """
- global SERVER_RUN
- msg = client.get_command()
- # print '%s says, "%s"' % (client.addrport(), msg)
- #
- # for guest in CLIENT_LIST:
- # if guest != client:
- # guest.send('%s says, %s\n' % (client.addrport(), msg))
- # else:
- # guest.send('You say, %s\n' % msg)
- cmd = msg.lower()
- if client not in LOGIN_LIST:
- login(client,cmd)
- ## bye = disconnect
- if cmd == 'bye':
- client.active = False
- ## shutdown == stop the server
- # elif cmd == 'shutdown':
- # SERVER_RUN = False
- #------------------------------------------------------------------------------
- # Main
- #------------------------------------------------------------------------------
- if __name__ == '__main__':
- ## Simple chat server to demonstrate connection handling via the
- ## async and telnet modules.
- ## Create a telnet server with a port, address,
- ## a function to call with new connections
- ## and one to call with lost connections.
- telnet_server = TelnetServer(
- port=40021,
- address='0.0.0.0',
- on_connect=on_connect,
- on_disconnect=on_disconnect,
- timeout = .05
- )
- print(">> Listening for connections on port %d. CTRL-C to break."
- % telnet_server.port)
- ## Server Loop
- while SERVER_RUN:
- telnet_server.poll() ## Send, Recv, and look for new connections
- kick_idle() ## Check for idle clients
- process_clients() ## Check for client input
- print(">> Server shutdown.")
Add Comment
Please, Sign In to add comment