Magic World game server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.8 KiB

5 years ago
  1. #!/usr/bin/env python
  2. # **********************************************************************
  3. #
  4. # Copyright (c) 2003-2018 ZeroC, Inc. All rights reserved.
  5. #
  6. # **********************************************************************
  7. import os, sys, socket, getopt
  8. try:
  9. import IceCertUtils
  10. except Exception as ex:
  11. print("couldn't load IceCertUtils, did you install the `zeroc-icecertutils'\n"
  12. "package from the Python package repository?\nerror: " + str(ex))
  13. sys.exit(1)
  14. def usage():
  15. print("Usage: " + sys.argv[0] + " [options]")
  16. print("")
  17. print("Options:")
  18. print("-h Show this message.")
  19. print("-d | --debug Debugging output.")
  20. print("--ip <ip> The IP address for the server certificate.")
  21. print("--dns <dns> The DNS name for the server certificate.")
  22. print("--use-dns Use the DNS name for the server certificate common")
  23. print(" name (default is to use the IP address)." )
  24. sys.exit(1)
  25. #
  26. # Check arguments
  27. #
  28. debug = False
  29. ip = None
  30. dns = None
  31. usedns = False
  32. impl = ""
  33. try:
  34. opts, args = getopt.getopt(sys.argv[1:], "hd", ["help", "debug", "ip=", "dns=","use-dns","impl="])
  35. except getopt.GetoptError as e:
  36. print("Error %s " % e)
  37. usage()
  38. sys.exit(1)
  39. for (o, a) in opts:
  40. if o == "-h" or o == "--help":
  41. usage()
  42. sys.exit(0)
  43. elif o == "-d" or o == "--debug":
  44. debug = True
  45. elif o == "--ip":
  46. ip = a
  47. elif o == "--dns":
  48. dns = a
  49. elif o == "--use-dns":
  50. usedns = True
  51. elif o == "--impl":
  52. impl = a
  53. def request(question, newvalue, value):
  54. while True:
  55. sys.stdout.write(question)
  56. sys.stdout.flush()
  57. input = sys.stdin.readline().strip()
  58. if input == 'n':
  59. sys.stdout.write(newvalue)
  60. sys.stdout.flush()
  61. return sys.stdin.readline().strip()
  62. else:
  63. return value
  64. #
  65. # Change to the directory where the certs files are stored
  66. #
  67. os.chdir(os.path.dirname(os.path.abspath(__file__)))
  68. if not ip:
  69. try:
  70. ip = socket.gethostbyname(socket.gethostname())
  71. except:
  72. ip = "127.0.0.1"
  73. ip = request("The IP address used for the server certificate will be: " + ip + "\n"
  74. "Do you want to keep this IP address? (y/n) [y]", "IP : ", ip)
  75. if not dns:
  76. dns = "localhost"
  77. dns = request("The DNS name used for the server certificate will be: " + dns + "\n"
  78. "Do you want to keep this DNS name? (y/n) [y]", "DNS : ", dns)
  79. CertificateFactory = vars(IceCertUtils)[impl + "CertificateFactory"]
  80. factory = CertificateFactory(debug=debug, cn="Ice Demos CA")
  81. #
  82. # CA certificate
  83. #
  84. factory.getCA().save("cacert.pem").save("cacert.der")
  85. # Client certificate
  86. client = factory.create("client")
  87. client.save("client.p12")
  88. # Server certificate
  89. server = factory.create("server", cn = (dns if usedns else ip), ip=ip, dns=dns)
  90. server.save("server.p12")
  91. try:
  92. factory.getCA().save("cacert.pem").save("cacert.jks") # Used by the Database/library demo
  93. server.save("server.jks", caalias="cacert")
  94. client.save("client.jks", caalias="cacert")
  95. # Don't try to generate the BKS if the JKS generation fails
  96. try:
  97. server.save("server.bks", caalias="cacert")
  98. client.save("client.bks", caalias="cacert")
  99. except Exception as ex:
  100. for f in ["server.bks", "client.bks"]:
  101. if os.path.exists(f): os.remove(f)
  102. print("warning: couldn't generate BKS certificates for Android applications:\n" + str(ex))
  103. print("Please fix this issue if you want to run the Android demos.")
  104. except Exception as ex:
  105. for f in ["server.jks", "client.jks"]:
  106. if os.path.exists(f): os.remove(f)
  107. print("warning: couldn't generate JKS certificates for Java applications:\n" + str(ex))
  108. print("Please fix this issue if you want to run the Java demos.")
  109. factory.destroy()