Skip to content

Commit

Permalink
Banner generation (and date/time evaluation) upon use
Browse files Browse the repository at this point in the history
  • Loading branch information
strictlymike committed Aug 15, 2017
1 parent de6a896 commit 798b6b6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
41 changes: 37 additions & 4 deletions fakenet/listeners/BannerFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@
import string
import datetime

class Banner():
"""Act like a string, but actually get date/time components on the fly."""

def __init__(self, banner, insertions):
self.banner = banner
self.insertions = insertions
# Indicate an error in the banner early-on as opposed to
# when a login or other event occurs.
test = self.failEarly()

def failEarly(self):
"""Raise exceptions upon construction rather than later."""
return self.fmt()

def __len__(self):
"""Needed for pyftpdlib.
If the length changes between the time when the caller obtains the
length and the time when they reference the string, then... *shrug*?
"""
return len(self.fmt())

def __repr__(self):
return self.fmt()

def fmt(self):
banner = self.banner
banner = datetime.datetime.now().strftime(banner)
banner = banner % self.insertions
banner = banner.replace('\\n', '\n').replace('\\t', '\t')
return banner

class BannerFactory():
def genBanner(self, config, bannerdict, defaultbannerkey='!generic'):
"""Select and format a banner.
Expand Down Expand Up @@ -55,11 +87,12 @@ def genBanner(self, config, bannerdict, defaultbannerkey='!generic'):

insertions = {'servername': servername, 'tz': 'UTC'}

banner = datetime.datetime.now().strftime(banner)
banner = banner % insertions
banner = banner.replace('\\n', '\n').replace('\\t', '\t')
return Banner(banner, insertions)

return banner
# banner = datetime.datetime.now().strftime(banner)
# banner = banner % insertions
# banner = banner.replace('\\n', '\n').replace('\\t', '\t')
# return banner

def randomizeHostname(self):
valid_hostname_charset = (string.ascii_letters + string.digits + '-')
Expand Down
4 changes: 2 additions & 2 deletions fakenet/listeners/FTPListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def start(self):
else:
self.handler = FakeFTPHandler

self.handler.banner = self.banner()
self.handler.banner = self.genBanner()

self.handler.ftproot_path = self.ftproot_path
self.handler.abstracted_fs = FakeFS
Expand All @@ -279,7 +279,7 @@ def stop(self):
if self.server:
self.server.close_all()

def banner(self):
def genBanner(self):
bannerfactory = BannerFactory.BannerFactory()
return bannerfactory.genBanner(self.config, BANNERS)

Expand Down
7 changes: 4 additions & 3 deletions fakenet/listeners/IRCListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
SRV_WELCOME = "Welcome to FakeNet."

BANNERS = {
'generic': 'Welcome to IRC - %%(servername)s - %a %b %d %H:%M:%S %%(tz)s %Y',
'generic': 'Welcome to IRC - %(servername)s - %a %b %d %H:%M:%S %(tz)s %Y',
'debian-ircd-irc2': (
'17/10/2011 11:50\n' +
' [ Debian GNU/Linux ]\n' +
Expand Down Expand Up @@ -49,6 +49,7 @@ def start(self):

self.server = ThreadedTCPServer((self.local_ip, int(self.config['port'])), ThreadedTCPRequestHandler)

self.banner = self.genBanner()
self.server.listener = self
self.server.logger = self.logger
self.server.config = self.config
Expand All @@ -64,7 +65,7 @@ def stop(self):
self.server.shutdown()
self.server.server_close()

def banner(self):
def genBanner(self):
bannerfactory = BannerFactory.BannerFactory()
return bannerfactory.genBanner(self.config, BANNERS)

Expand Down Expand Up @@ -117,7 +118,7 @@ def irc_NICK(self, cmd, params):

self.nick = params

banner = self.server.listener.banner()
banner = self.server.listener.banner

self.irc_send_server("001", "%s :%s" % (self.nick, banner))
self.irc_send_server("376", "%s :End of /MOTD command." % self.nick)
Expand Down

0 comments on commit 798b6b6

Please sign in to comment.