您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

147 行
4.6KB

  1. import binascii
  2. import os
  3. import exec_helpers
  4. import re
  5. import uuid
  6. import json
  7. from flask import request, url_for
  8. from flask_api import FlaskAPI, status, exceptions
  9. from flask_cors import CORS
  10. app = FlaskAPI(__name__)
  11. CORS(app)
  12. @app.route("/compile", methods=['POST'])
  13. def compile():
  14. if request.method == 'POST':
  15. note = str(request.data.get('source', ''))
  16. html = str(request.data.get('html', ''))
  17. flags = str(request.data.get('flags', ''))
  18. uuid_ = str(request.data.get('uuid', ''))
  19. if not uuid_:
  20. uuid_ = str(uuid.uuid1())
  21. counter = 0
  22. if os.path.exists("/data/{}/counter".format(uuid_)):
  23. with open("/data/{}/counter".format(uuid_), "r") as f:
  24. counter = int(f.read().strip()) + 1
  25. f.close()
  26. else:
  27. os.mkdir("/data/{}".format(uuid_))
  28. os.mkdir("/data/{}/{}".format(uuid_, counter))
  29. with open("/data/{}/{}/environ".format(uuid_, counter), "w") as f:
  30. for env in os.environ:
  31. f.write("{}={}\n".format(env, os.environ[env]))
  32. f.close()
  33. with open("/data/{}/{}/headers".format(uuid_, counter), "w") as f:
  34. for k, v in request.headers:
  35. f.write("{}: {}\n".format(k, v))
  36. f.close()
  37. with open("/data/{}/{}/source".format(uuid_, counter), "w") as f:
  38. f.write(note)
  39. f.close()
  40. with open("/data/{}/{}/html".format(uuid_, counter), "w") as f:
  41. f.write(html)
  42. f.close()
  43. with open("/data/{}/{}/flags".format(uuid_, counter), "w") as f:
  44. f.write(flags)
  45. f.close()
  46. with open("/data/{}/counter".format(uuid_), "w") as f:
  47. f.write(str(counter))
  48. f.close()
  49. with open("/tmp/test.cpp", "w") as f:
  50. f.write(note)
  51. f.close()
  52. stdout = ''
  53. stderr = ''
  54. retcode = 0
  55. output = ''
  56. output_wasm = ''
  57. flags = flags.replace("\n", " ")
  58. wasm = 'cheerp-mode=wasm' in flags
  59. # Simple way to disable malicious flags (hopefully)
  60. if not re.match('^[\s\ta-zA-Z0-9=\-_\.]*$', flags):
  61. flags = ""
  62. cmd = "/opt/cheerp/bin/clang {} /tmp/test.cpp -o /tmp/test.js".format(flags)
  63. if wasm:
  64. cmd = "/opt/cheerp/bin/clang {} /tmp/test.cpp -cheerp-wasm-loader=/tmp/test.js -o /tmp/test.wasm".format(flags)
  65. with exec_helpers.Subprocess() as executor:
  66. try:
  67. ret = executor.check_call(cmd, shell=True, timeout=10)
  68. if ret:
  69. retcode = ret.exit_code
  70. output = ret.stdout_str
  71. except exec_helpers.ExecHelperTimeoutError as e:
  72. #stdout = e.stdout
  73. #stderr = e.stderr
  74. stderr = str(e)
  75. except exec_helpers.CalledProcessError as e:
  76. stdout = e.stdout
  77. stderr = e.stderr
  78. if os.path.exists("/tmp/test.js"):
  79. with open("/tmp/test.js", "r") as f:
  80. output = f.read()
  81. f.close()
  82. if wasm and os.path.exists("/tmp/test.wasm"):
  83. with open("/tmp/test.wasm", "rb") as f:
  84. output_wasm = f.read()
  85. f.close()
  86. return {
  87. 'stdout': stdout,
  88. 'stderr': stderr,
  89. 'command': cmd,
  90. 'retcode': retcode,
  91. 'javascript': output,
  92. 'wasm': binascii.b2a_base64(output_wasm).decode("utf-8") if wasm else "",
  93. 'uuid': uuid_,
  94. 'version': counter,
  95. }, status.HTTP_201_CREATED
  96. @app.route("/retrieve", methods=['POST'])
  97. def retrieve():
  98. if request.method == 'POST':
  99. uuid_ = str(request.data.get('uuid', ''))
  100. version = str(request.data.get('version', ''))
  101. source = ''
  102. html = ''
  103. flags = ''
  104. if not os.path.exists("/data/{}/{}".format(uuid_, version)):
  105. return False
  106. with open("/data/{}/{}/source".format(uuid_, version), "r") as f:
  107. source = f.read()
  108. f.close()
  109. with open("/data/{}/{}/html".format(uuid_, version), "r") as f:
  110. html = f.read()
  111. f.close()
  112. with open("/data/{}/{}/flags".format(uuid_, version), "r") as f:
  113. flags = f.read()
  114. f.close()
  115. return {
  116. 'source': source,
  117. 'html': html,
  118. 'flags': flags,
  119. }, status.HTTP_201_CREATED
  120. if __name__ == "__main__":
  121. app.run(host="0.0.0.0", debug=True)