main.py 25 KB

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