register_page.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """DWC Network Server Emulator
  2. Copyright (C) 2014 SMTDDR
  3. Copyright (C) 2014 kyle95wm
  4. Copyright (C) 2014 AdmiralCurtiss
  5. Copyright (C) 2015 Sepalani
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License as
  8. published by the Free Software Foundation, either version 3 of the
  9. License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Affero General Public License for more details.
  14. You should have received a copy of the GNU Affero General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. from twisted.web import server, resource
  18. from twisted.internet import reactor
  19. from twisted.internet.error import ReactorAlreadyRunning
  20. import re
  21. import base64
  22. import codecs
  23. import sqlite3
  24. import collections
  25. import json
  26. import time
  27. import datetime
  28. import logging
  29. import other.utils as utils
  30. import gamespy.gs_utility as gs_utils
  31. import dwc_config
  32. logger = dwc_config.get_logger('RegisterPage')
  33. _, port = dwc_config.get_ip_port('RegisterPage')
  34. class RegPage(resource.Resource):
  35. isLeaf = True
  36. def __init__(self, regpage):
  37. self.regpage = regpage
  38. def get_header(self, title=None):
  39. if not title:
  40. title = 'Register a Console'
  41. s = """
  42. <html>
  43. <head>
  44. <title>%s</title>
  45. </head>
  46. <body>
  47. <p>
  48. <b>Register a console</b>
  49. </p>""" % title
  50. return s
  51. def get_footer(self):
  52. s = """
  53. </body>
  54. </html>"""
  55. return s
  56. def update_maclist(self, request):
  57. address = request.getClientIP()
  58. dbconn = sqlite3.connect('gpcm.db')
  59. macadr = request.args['macadr'][0].strip()
  60. actiontype = request.args['action'][0]
  61. macadr = macadr.lower()
  62. if not re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$",
  63. macadr):
  64. request.setResponseCode(500)
  65. return "The MAC you entered was invalid." \
  66. "Please click the back button and try again!"
  67. macadr = macadr.replace(":", "").replace("-", "")
  68. if actiontype == 'add':
  69. dbconn.cursor().execute(
  70. 'INSERT INTO pending VALUES(?)',
  71. (macadr,)
  72. )
  73. responsedata = "Added %s to pending list." % (macadr)
  74. responsedata += "Please close this window now."
  75. " It's also not a bad idea to check back on the status of your"
  76. " activation by attempting to connect your console to the server."
  77. dbconn.commit()
  78. dbconn.close()
  79. request.setHeader("Content-Type", "text/html; charset=utf-8")
  80. request.setResponseCode(303)
  81. return responsedata
  82. if not referer:
  83. referer = "/register"
  84. request.setHeader("Location", referer)
  85. request.setResponseCode(303)
  86. return responsedata
  87. def render_maclist(self, request):
  88. address = request.getClientIP()
  89. dbconn = sqlite3.connect('gpcm.db')
  90. responsedata = """
  91. <form action='updatemaclist' method='POST'>
  92. macadr (must be in the format of %s or %s):
  93. <input type='text' name='macadr'>
  94. <input type='hidden' name='action' value='add'>
  95. <input type='submit' value='Register console'>
  96. </form>
  97. <table border='1'>""" % ('aa:bb:cc:dd:ee:ff', 'aa-bb-cc-dd-ee-ff')
  98. dbconn.close()
  99. request.setHeader("Content-Type", "text/html; charset=utf-8")
  100. return responsedata
  101. def render_GET(self, request):
  102. title = None
  103. response = ''
  104. if request.path == "/register":
  105. title = 'Register a Console'
  106. response = self.render_maclist(request)
  107. return self.get_header(title) + response + self.get_footer()
  108. def render_POST(self, request):
  109. if request.path == "/updatemaclist":
  110. return self.update_maclist(request)
  111. else:
  112. return self.get_header() + self.get_footer()
  113. class RegPageServer(object):
  114. def start(self):
  115. site = server.Site(RegPage(self))
  116. reactor.listenTCP(port, site)
  117. logger.log(logging.INFO,
  118. "Now listening for connections on port %d...",
  119. port)
  120. try:
  121. if not reactor.running:
  122. reactor.run(installSignalHandlers=0)
  123. except ReactorAlreadyRunning:
  124. pass
  125. if __name__ == "__main__":
  126. RegPageServer().start()