main.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import os
  2. import random
  3. import string
  4. import traceback
  5. import shutil
  6. import uuid
  7. from flask import Flask, request
  8. app = Flask(__name__)
  9. app.base_project = {
  10. "android": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCode",
  11. "android_flutter": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCodeFlutter-Android",
  12. "android_ionic": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCodeIonic-Android",
  13. "android_react": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCodeReact-Android",
  14. "ios": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCode-iOS",
  15. "ios_flutter": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCodeFlutter-iOS",
  16. "ios_ionic": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCodeIonic-iOS",
  17. "ios_react": "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/NexilisSampleCodeReact-iOS"
  18. }
  19. app.temp_folder = "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/BuildExample"
  20. app.zip_folder = "/Users/maronakins/Documents/AppBuilder/src/EmbedFeatures/uploads"
  21. # app.ssl = ('/usr/src/app/ssl/STAR_newuniverse_io.crt', '/usr/src/app/ssl/STAR_newuniverse.io.key')
  22. app.ssl = None
  23. app.verbose = True
  24. def vprint(*data):
  25. if app.verbose:
  26. print(*data)
  27. def indented_str(n, s, line=True, spaces=4) -> str:
  28. return " " * n * spaces + s + os.linesep if line else ""
  29. def create_folder(platform, uid):
  30. path = os.path.join(app.temp_folder, uid)
  31. if not os.path.exists(path):
  32. os.mkdir(path)
  33. else:
  34. shutil.rmtree(path)
  35. os.mkdir(path)
  36. vprint(path)
  37. base_project = app.base_project[platform]
  38. base_project_name = os.path.basename(base_project)
  39. path_dest = os.path.join(path, base_project_name)
  40. if not os.path.exists(path_dest):
  41. shutil.copytree(base_project, path_dest)
  42. return path_dest
  43. def change(platform: str, path_dest: str, features: dict, security: dict):
  44. if platform == "android":
  45. main_act_path = os.path.join(path_dest,
  46. "app/src/main/java/com/example/nexilissamplecodeburger/MainActivity.java")
  47. xml_menu_path = os.path.join(path_dest, "app/src/main/res/menu/menu_main.xml")
  48. with open(main_act_path, "r") as f:
  49. lines = f.readlines()
  50. with open(main_act_path, "w") as f:
  51. for line in lines:
  52. if "//FEATURES" in line:
  53. if features["cc"]["status"]:
  54. f.write(indented_str(2, "if (id == R.id.action_cc) {"))
  55. f.write(indented_str(3, "API.openContactCenter();"))
  56. f.write(indented_str(3, "return true;"))
  57. f.write(indented_str(2, "}"))
  58. if features["nc"]["status"]:
  59. f.write(indented_str(2, "if (id == R.id.action_nc) {"))
  60. f.write(indented_str(3, "API.openNotificationCenter();"))
  61. f.write(indented_str(3, "return true;"))
  62. f.write(indented_str(2, "}"))
  63. if features["im"]["status"]:
  64. f.write(indented_str(2, "if (id == R.id.action_chats) {"))
  65. f.write(indented_str(3, "API.openChat();"))
  66. f.write(indented_str(3, "return true;"))
  67. f.write(indented_str(2, "}"))
  68. if features["call"]["status"]:
  69. f.write(indented_str(2, "if (id == R.id.action_call) {"))
  70. f.write(indented_str(3, "API.openCall();"))
  71. f.write(indented_str(3, "return true;"))
  72. f.write(indented_str(2, "}"))
  73. if features["ls"]["status"]:
  74. f.write(indented_str(2, "if (id == R.id.action_ls) {"))
  75. f.write(indented_str(3, "API.openOptionsStreaming();"))
  76. f.write(indented_str(3, "return true;"))
  77. f.write(indented_str(2, "}"))
  78. if features["settings"]["status"]:
  79. f.write(indented_str(2, "if (id == R.id.action_settings) {"))
  80. f.write(indented_str(3, "API.openSettings();"))
  81. f.write(indented_str(3, "return true;"))
  82. f.write(indented_str(2, "}"))
  83. elif "//SECURITY" in line:
  84. if security["show_security"]:
  85. f.write(indented_str(2, "API.setShowSecurityShieldDialog(true);"))
  86. f.write(os.linesep)
  87. if security["emulator"]:
  88. f.write(indented_str(2, "API.setCheckEmulator(true);"))
  89. f.write(os.linesep)
  90. if security["debug"]:
  91. f.write(indented_str(2, "API.setCheckAdb(true);"))
  92. f.write(os.linesep)
  93. if security["sim_swap"]:
  94. f.write(indented_str(2,
  95. "API.setCheckSimCardSwapListener(MainActivity.this, new SimCardDetectionCallback() {"))
  96. f.write(os.linesep)
  97. f.write(indented_str(3, "@Override"))
  98. f.write(indented_str(3, "public boolean onSimCardChange() {"))
  99. f.write(indented_str(4, "return false;"))
  100. f.write(indented_str(3, "}"))
  101. f.write(os.linesep)
  102. f.write(indented_str(3, "@Override"))
  103. f.write(indented_str(3, "public boolean onError(String s) {"))
  104. f.write(os.linesep)
  105. f.write(indented_str(3, "}"))
  106. f.write(indented_str(2, "});"))
  107. f.write(os.linesep)
  108. if security["malware"]:
  109. f.write(indented_str(2, "API.setCheckMalware(true);"))
  110. f.write(os.linesep)
  111. if security["capture"]:
  112. f.write(indented_str(2, "API.setPreventScreenCapture(true);"))
  113. f.write(os.linesep)
  114. if security["call_forwarding"]:
  115. f.write(indented_str(2, "API.setCheckCallForwarding(true);"))
  116. f.write(os.linesep)
  117. if security["secure_folder"]:
  118. f.write(indented_str(2, "API.openSecureFolder();"))
  119. f.write(os.linesep)
  120. elif "//SMS" in line and features["sms"]["status"]:
  121. f.write(indented_str(2, "API.setEnabledSMS(true);"))
  122. elif "//EMAIL" in line and features["email"]["status"]:
  123. f.write(indented_str(2, "API.setEnabledEmail(true);"))
  124. else:
  125. f.write(line)
  126. with open(xml_menu_path, "r") as f:
  127. lines = f.readlines()
  128. with open(xml_menu_path, "w") as f:
  129. for line in lines:
  130. if "<!-- FEATURES -->" in line:
  131. n = 100
  132. if features["cc"]["status"]:
  133. f.write(indented_str(1, "<item"))
  134. f.write(indented_str(2, 'android:id="@+id/action_cc"'))
  135. f.write(indented_str(2, f'android:orderInCategory="{n}"'))
  136. f.write(indented_str(2, 'android:title="Contact Center"'))
  137. f.write(indented_str(2, 'app:showAsAction="never" />'))
  138. n = n + 1
  139. if features["nc"]["status"]:
  140. f.write(indented_str(1, "<item"))
  141. f.write(indented_str(2, 'android:id="@+id/action_nc"'))
  142. f.write(indented_str(2, f'android:orderInCategory="{n}"'))
  143. f.write(indented_str(2, 'android:title="Notification Center"'))
  144. f.write(indented_str(2, 'app:showAsAction="never" />'))
  145. n = n + 1
  146. if features["im"]["status"]:
  147. f.write(indented_str(1, "<item"))
  148. f.write(indented_str(2, 'android:id="@+id/action_chats"'))
  149. f.write(indented_str(2, f'android:orderInCategory="{n}"'))
  150. f.write(indented_str(2, 'android:title="Instant Messaging"'))
  151. f.write(indented_str(2, 'app:showAsAction="never" />'))
  152. n = n + 1
  153. if features["call"]["status"]:
  154. f.write(indented_str(1, "<item"))
  155. f.write(indented_str(2, 'android:id="@+id/action_call"'))
  156. f.write(indented_str(2, f'android:orderInCategory="{n}"'))
  157. f.write(indented_str(2, 'android:title="Call"'))
  158. f.write(indented_str(2, 'app:showAsAction="never" />'))
  159. n = n + 1
  160. if features["ls"]["status"]:
  161. f.write(indented_str(1, "<item"))
  162. f.write(indented_str(2, 'android:id="@+id/action_ls"'))
  163. f.write(indented_str(2, f'android:orderInCategory="{n}"'))
  164. f.write(indented_str(2, 'android:title="Streaming"'))
  165. f.write(indented_str(2, 'app:showAsAction="never" />'))
  166. n = n + 1
  167. if features["settings"]["status"]:
  168. f.write(indented_str(1, "<item"))
  169. f.write(indented_str(2, 'android:id="@+id/action_settings"'))
  170. f.write(indented_str(2, f'android:orderInCategory="{n}"'))
  171. f.write(indented_str(2, 'android:title="Settings"'))
  172. f.write(indented_str(2, 'app:showAsAction="never" />'))
  173. n = n + 1
  174. else:
  175. f.write(line)
  176. elif platform == "android_flutter":
  177. main_act_path = os.path.join(path_dest, "lib/main.dart")
  178. with open(main_act_path, "r") as f:
  179. lines = f.readlines()
  180. with open(main_act_path, "w") as f:
  181. features_str = []
  182. for line in lines:
  183. if "'Features List'" in line:
  184. if features["cc"]["status"]:
  185. features_str.append("Contact Center")
  186. if features["nc"]["status"]:
  187. features_str.append("Notification Center")
  188. if features["im"]["status"]:
  189. features_str.append("Instant Messaging")
  190. if features["call"]["status"]:
  191. features_str.append("Call")
  192. if features["ls"]["status"]:
  193. features_str.append("Streaming")
  194. if features["settings"]["status"]:
  195. features_str.append("Settings")
  196. result_str = ", ".join([f"'{n}'" for n in features_str])
  197. replaced = line.replace("'Features List'", result_str)
  198. f.write(replaced)
  199. elif "//FEATURES" in line:
  200. if features["cc"]["status"]:
  201. f.write(indented_str(3, 'case "Contact Center":', spaces=2))
  202. f.write(indented_str(4, 'nativeChannel.invokeMethod("openContactCenter");', spaces=2))
  203. f.write(indented_str(4, "break;", spaces=2))
  204. if features["nc"]["status"]:
  205. f.write(indented_str(3, 'case "Notification Center":', spaces=2))
  206. f.write(indented_str(4, 'nativeChannel.invokeMethod("openNotificationCenter");', spaces=2))
  207. f.write(indented_str(4, "break;", spaces=2))
  208. if features["im"]["status"]:
  209. f.write(indented_str(3, 'case "Instant Messaging":', spaces=2))
  210. f.write(indented_str(4, 'nativeChannel.invokeMethod("openInstantMessaging");', spaces=2))
  211. f.write(indented_str(4, "break;", spaces=2))
  212. if features["call"]["status"]:
  213. f.write(indented_str(3, 'case "Call":', spaces=2))
  214. f.write(indented_str(4, 'nativeChannel.invokeMethod("openCall");', spaces=2))
  215. f.write(indented_str(4, "break;", spaces=2))
  216. if features["ls"]["status"]:
  217. f.write(indented_str(3, 'case "Streaming":', spaces=2))
  218. f.write(indented_str(4, 'nativeChannel.invokeMethod("openStreaming");', spaces=2))
  219. f.write(indented_str(4, "break;", spaces=2))
  220. if features["settings"]["status"]:
  221. f.write(indented_str(3, 'case "Settings":', spaces=2))
  222. f.write(indented_str(4, 'nativeChannel.invokeMethod("openSettings");', spaces=2))
  223. f.write(indented_str(4, "break;", spaces=2))
  224. else:
  225. f.write(line)
  226. elif platform == "android_ionic":
  227. pass
  228. elif platform == "android_react":
  229. pass
  230. elif platform == "ios":
  231. main_act_path = os.path.join(path_dest,
  232. "ExampleCode/ViewController.swift")
  233. with open(main_act_path, "r") as f:
  234. lines = f.readlines()
  235. with open(main_act_path, "w") as f:
  236. for line in lines:
  237. if "//FEATURES" in line:
  238. if features["cc"]["status"]:
  239. f.write(indented_str(3, 'UIAction(title: "Contact Center".localized(), handler: {(_) in'))
  240. f.write(indented_str(4, "APIS.openContactCenter();"))
  241. f.write(indented_str(3, "}),"))
  242. if features["nc"]["status"]:
  243. f.write(indented_str(3, 'UIAction(title: "Notification Center".localized(), handler: {(_) in'))
  244. f.write(indented_str(4, "APIS.openNotificationCenter();"))
  245. f.write(indented_str(3, "}),"))
  246. if features["im"]["status"]:
  247. f.write(indented_str(3, 'UIAction(title: "Chat".localized(), handler: {(_) in'))
  248. f.write(indented_str(4, "APIS.openChat();"))
  249. f.write(indented_str(3, "}),"))
  250. if features["call"]["status"]:
  251. f.write(indented_str(3, 'UIAction(title: "Call".localized(), handler: {(_) in'))
  252. f.write(indented_str(4, "APIS.openCall();"))
  253. f.write(indented_str(3, "}),"))
  254. if features["ls"]["status"]:
  255. f.write(indented_str(3, 'UIAction(title: "Live Streaming".localized(), handler: {(_) in'))
  256. f.write(indented_str(4, "APIS.openStreaming();"))
  257. f.write(indented_str(3, "}),"))
  258. if features["settings"]["status"]:
  259. f.write(indented_str(3, 'UIAction(title: "Setting".localized(), handler: {(_) in'))
  260. f.write(indented_str(4, "APIS.openSetting();"))
  261. f.write(indented_str(3, "}),"))
  262. else:
  263. f.write(line)
  264. elif platform == "ios_flutter":
  265. main_act_path = os.path.join(path_dest, "lib/main.dart")
  266. with open(main_act_path, "r") as f:
  267. lines = f.readlines()
  268. with open(main_act_path, "w") as f:
  269. features_str = []
  270. for line in lines:
  271. if "'Features List'" in line:
  272. if features["cc"]["status"]:
  273. features_str.append("Contact Center")
  274. if features["nc"]["status"]:
  275. features_str.append("Notification Center")
  276. if features["im"]["status"]:
  277. features_str.append("Instant Messaging")
  278. if features["call"]["status"]:
  279. features_str.append("Call")
  280. if features["ls"]["status"]:
  281. features_str.append("Streaming")
  282. if features["settings"]["status"]:
  283. features_str.append("Settings")
  284. result_str = ", ".join([f"'{n}'" for n in features_str])
  285. replaced = line.replace("'Features List'", result_str)
  286. f.write(replaced)
  287. elif "//FEATURES" in line:
  288. if features["cc"]["status"]:
  289. f.write(indented_str(3, 'case "Contact Center":', spaces=2))
  290. f.write(indented_str(4, 'nativeChannel.invokeMethod("openContactCenter");', spaces=2))
  291. f.write(indented_str(4, "break;", spaces=2))
  292. if features["nc"]["status"]:
  293. f.write(indented_str(3, 'case "Notification Center":', spaces=2))
  294. f.write(indented_str(4, 'nativeChannel.invokeMethod("openNotificationCenter");', spaces=2))
  295. f.write(indented_str(4, "break;", spaces=2))
  296. if features["im"]["status"]:
  297. f.write(indented_str(3, 'case "Instant Messaging":', spaces=2))
  298. f.write(indented_str(4, 'nativeChannel.invokeMethod("openInstantMessaging");', spaces=2))
  299. f.write(indented_str(4, "break;", spaces=2))
  300. if features["call"]["status"]:
  301. f.write(indented_str(3, 'case "Call":', spaces=2))
  302. f.write(indented_str(4, 'nativeChannel.invokeMethod("openCall");', spaces=2))
  303. f.write(indented_str(4, "break;", spaces=2))
  304. if features["ls"]["status"]:
  305. f.write(indented_str(3, 'case "Streaming":', spaces=2))
  306. f.write(indented_str(4, 'nativeChannel.invokeMethod("openStreaming");', spaces=2))
  307. f.write(indented_str(4, "break;", spaces=2))
  308. if features["settings"]["status"]:
  309. f.write(indented_str(3, 'case "Settings":', spaces=2))
  310. f.write(indented_str(4, 'nativeChannel.invokeMethod("openSettings");', spaces=2))
  311. f.write(indented_str(4, "break;", spaces=2))
  312. else:
  313. f.write(line)
  314. elif platform == "ios_ionic":
  315. pass
  316. elif platform == "ios_react":
  317. pass
  318. def deliver_zip(path_dest, uid):
  319. rand_name = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(32))
  320. zip_name = f"{rand_name}"
  321. new_dir = os.path.join(app.zip_folder, zip_name)
  322. try:
  323. shutil.make_archive(new_dir, 'zip', path_dest)
  324. project_path = os.path.join(app.temp_folder, uid)
  325. shutil.rmtree(project_path)
  326. return {"status": "0", "name": zip_name + ".zip"}
  327. except Exception as e:
  328. return {"status": "4", "message": "Deliver ZIP failed"}
  329. @app.route('/', methods=["GET", "POST"])
  330. def build_project():
  331. vprint('==============================================================')
  332. if request.method == 'POST':
  333. platform = "android"
  334. feature_dict = {
  335. "im": {
  336. "android_name": "action_chats",
  337. "status": False
  338. },
  339. "cc": {
  340. "android_name": "action_cc",
  341. "status": False
  342. },
  343. "call": {
  344. "android_name": "action_call",
  345. "status": False
  346. },
  347. "ls": {
  348. "android_name": "action_ls",
  349. "status": False
  350. },
  351. "settings": {
  352. "android_name": "action_settings",
  353. "status": False
  354. },
  355. "nc": {
  356. "android_name": "action_nc",
  357. "status": False
  358. },
  359. "sms": {
  360. "status": False
  361. },
  362. "email": {
  363. "status": False
  364. },
  365. }
  366. security_dict = {
  367. "malware": False,
  368. "clone": False,
  369. "emulator": False,
  370. "debug": False,
  371. "sim_swap": False,
  372. "capture": False,
  373. "call_forwarding": False,
  374. "secure_folder": False,
  375. "show_security": False,
  376. }
  377. try:
  378. if "feature_im" in request.form:
  379. feature_dict["im"]["status"] = request.form["feature_im"] == "1"
  380. if "feature_cc" in request.form:
  381. feature_dict["cc"]["status"] = request.form["feature_cc"] == "1"
  382. if "feature_vc" in request.form:
  383. feature_dict["call"]["status"] = request.form["feature_vc"] == "1"
  384. if "feature_ac" in request.form:
  385. feature_dict["call"]["status"] = request.form["feature_ac"] == "1"
  386. if "feature_ls" in request.form:
  387. feature_dict["ls"]["status"] = request.form["feature_ls"] == "1"
  388. if "feature_nc" in request.form:
  389. feature_dict["nc"]["status"] = request.form["feature_nc"] == "1"
  390. if "feature_sms" in request.form:
  391. feature_dict["sms"]["status"] = request.form["feature_sms"] == "1"
  392. if "feature_email" in request.form:
  393. feature_dict["email"]["status"] = request.form["feature_email"] == "1"
  394. if "feature_settings" in request.form:
  395. feature_dict["settings"]["status"] = request.form["feature_settings"] == "1"
  396. if "security_malware" in request.form:
  397. security_dict["malware"] = request.form["security_malware"] == "1"
  398. if "security_clone" in request.form:
  399. security_dict["clone"] = request.form["security_clone"] == "1"
  400. if "security_emulator" in request.form:
  401. security_dict["emulator"] = request.form["security_emulator"] == "1"
  402. if "security_debug" in request.form:
  403. security_dict["debug"] = request.form["security_debug"] == "1"
  404. if "security_sim_swap" in request.form:
  405. security_dict["sim_swap"] = request.form["security_sim_swap"] == "1"
  406. if "security_capture" in request.form:
  407. security_dict["capture"] = request.form["security_capture"] == "1"
  408. if "security_call_forwarding" in request.form:
  409. security_dict["call_forwarding"] = request.form["security_call_forwarding"] == "1"
  410. if "security_secure_folder" in request.form:
  411. security_dict["secure_folder"] = request.form["security_secure_folder"] == "1"
  412. if "show_security" in request.form:
  413. security_dict["show_security"] = request.form["show_security"] == "1"
  414. else:
  415. security_dict["show_security"] = (security_dict["malware"] or security_dict["clone"]
  416. or security_dict["emulator"] or security_dict["debug"]
  417. or security_dict["sim_swap"] or security_dict["capture"]
  418. or security_dict["call_forwarding"]
  419. or security_dict["secure_folder"])
  420. if "platform" in request.form:
  421. platform = request.form["platform"]
  422. except BaseException as e:
  423. vprint(traceback.format_exc())
  424. return {"status": "1", "message": "Parameter mismatch\n{}\n".format(str(e))}, 400
  425. try:
  426. uu_id = str(uuid.uuid4())
  427. path_dest = create_folder(platform, uu_id)
  428. change(platform, path_dest, feature_dict, security_dict)
  429. return deliver_zip(path_dest, uu_id)
  430. except BaseException as e:
  431. vprint(traceback.format_exc())
  432. return {"status": "2", "message": "Process failure\n{}\n".format(str(e))}, 200
  433. else:
  434. if 'e' in request.args:
  435. return request.args['e']
  436. return "Hello World!"
  437. if __name__ == '__main__':
  438. app.run(host='0.0.0.0', port=8056, debug=app.verbose, ssl_context=app.ssl)