mm_os 3.3.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (380) hide show
  1. package/LICENSE +21 -201
  2. package/README.md +491 -99
  3. package/README_EN.md +498 -0
  4. package/adapter/adapter.js +431 -0
  5. package/adapter/custom_persistence.js +660 -0
  6. package/adapter/mqtt.js +273 -0
  7. package/adapter/socket.js +113 -0
  8. package/adapter/web.js +67 -0
  9. package/adapter/websocket.js +146 -0
  10. package/com/api/com.json +5 -0
  11. package/{core/com → com}/api/config.tpl.json +8 -8
  12. package/com/api/drive.js +708 -0
  13. package/com/api/index.js +198 -0
  14. package/com/api/oauth.js +200 -0
  15. package/com/api/script.tpl.js +32 -0
  16. package/com/cmd/README.md +11 -0
  17. package/com/cmd/com.json +5 -0
  18. package/com/cmd/config.tpl.json +122 -0
  19. package/com/cmd/drive.js +1548 -0
  20. package/com/cmd/index.js +1066 -0
  21. package/com/cmd/msg.json +48 -0
  22. package/com/cmd/nlp.js +525 -0
  23. package/com/cmd/script.tpl.js +32 -0
  24. package/com/db/com.json +5 -0
  25. package/com/db/drive.js +1999 -0
  26. package/com/db/index.js +242 -0
  27. package/{core/com → com}/event/README.md +4 -4
  28. package/com/event/com.json +5 -0
  29. package/{core/com → com}/event/config.tpl.json +18 -18
  30. package/com/event/drive.js +59 -0
  31. package/com/event/index.js +409 -0
  32. package/com/event/script.tpl.js +23 -0
  33. package/com/mqtt/com.json +5 -0
  34. package/{core/com → com}/mqtt/config.tpl.json +3 -5
  35. package/com/mqtt/drive.js +676 -0
  36. package/com/mqtt/index.js +822 -0
  37. package/com/mqtt/mm_mqtt.js +425 -0
  38. package/com/mqtt/script.tpl.js +723 -0
  39. package/com/nav/com.json +5 -0
  40. package/com/nav/config.tpl.json +84 -0
  41. package/com/nav/drive.js +702 -0
  42. package/com/nav/index.js +231 -0
  43. package/{core/com → com}/nav/tpl/admin_pc/page_config.vue +280 -280
  44. package/{core/com → com}/nav/tpl/admin_pc/page_config_form.vue +194 -194
  45. package/com/nav/tpl/admin_pc/page_form.vue +180 -0
  46. package/com/nav/tpl/admin_pc/page_view.vue +124 -0
  47. package/com/nav/tpl/dev_pc/page_default.vue +247 -0
  48. package/com/nav/tpl/dev_pc/page_type.vue +313 -0
  49. package/com/nav/tpl/home_pc/page_default.vue +234 -0
  50. package/com/nav/tpl/home_pc/page_form.vue +137 -0
  51. package/com/nav/tpl/home_pc/page_list.vue +234 -0
  52. package/com/nav/tpl/home_pc/page_nav.vue +221 -0
  53. package/com/nav/tpl/home_pc/page_type.vue +234 -0
  54. package/com/nav/tpl/home_pc/page_view.vue +125 -0
  55. package/com/nav/tpl/home_phone/page_channel.vue +234 -0
  56. package/com/nav/tpl/home_phone/page_default.vue +234 -0
  57. package/com/nav/tpl/home_phone/page_form.vue +137 -0
  58. package/com/nav/tpl/home_phone/page_nav.vue +237 -0
  59. package/com/nav/tpl/home_phone/page_type.vue +234 -0
  60. package/com/nav/tpl/home_phone/page_view.vue +125 -0
  61. package/com/nav/viewmodel.js +446 -0
  62. package/com/param/com.json +5 -0
  63. package/{core/com → com}/param/config.tpl.json +7 -1
  64. package/com/param/drive.js +502 -0
  65. package/com/param/index.js +155 -0
  66. package/com/param/script.tpl.js +12 -0
  67. package/com/pendant/com.json +5 -0
  68. package/{core/com/component → com/pendant}/config.tpl.json +15 -13
  69. package/com/pendant/drive.js +204 -0
  70. package/com/pendant/index.js +441 -0
  71. package/com/pendant/pendant.html +16 -0
  72. package/com/pendant/script.tpl.js +18 -0
  73. package/com/socket/com.json +5 -0
  74. package/com/socket/config.tpl.json +12 -0
  75. package/com/socket/drive.js +651 -0
  76. package/com/socket/index.js +351 -0
  77. package/com/socket/script.tpl.js +41 -0
  78. package/com/sql/com.json +5 -0
  79. package/{core/com → com}/sql/config.tpl.json +13 -9
  80. package/com/sql/drive.js +1259 -0
  81. package/com/sql/index.js +150 -0
  82. package/com/sql/script.tpl.js +47 -0
  83. package/com/static/com.json +5 -0
  84. package/{core/com → com}/static/config.tpl.json +10 -6
  85. package/com/static/drive.js +194 -0
  86. package/com/static/index.js +226 -0
  87. package/com/static/script.tpl.js +28 -0
  88. package/com/task/com.json +5 -0
  89. package/{core/com → com}/task/config.tpl.json +4 -6
  90. package/com/task/drive.js +405 -0
  91. package/com/task/index.js +148 -0
  92. package/com/task/script.tpl.js +37 -0
  93. package/com/template/com.json +5 -0
  94. package/com/template/config.tpl.json +16 -0
  95. package/com/template/drive.js +80 -0
  96. package/com/template/index.js +141 -0
  97. package/com.js +156 -0
  98. package/common/README.md +2 -0
  99. package/common/handler/msg/handler.json +22 -0
  100. package/common/handler/msg/index.js +23 -0
  101. package/common/handler/player/handler.json +22 -0
  102. package/common/handler/player/index.js +287 -0
  103. package/common/handler/user/handler.json +22 -0
  104. package/common/handler/user/index.js +23 -0
  105. package/common/middleware/web_after/index.js +29 -0
  106. package/common/middleware/web_after/middleware.json +9 -0
  107. package/common/middleware/web_base/index.js +113 -0
  108. package/common/middleware/web_base/middleware.json +19 -0
  109. package/common/middleware/web_before/index.js +33 -0
  110. package/common/middleware/web_before/middleware.json +9 -0
  111. package/common/middleware/web_cors/index.js +87 -0
  112. package/common/middleware/web_cors/middleware.json +24 -0
  113. package/common/middleware/web_error/index.js +119 -0
  114. package/common/middleware/web_error/middleware.json +18 -0
  115. package/common/middleware/web_ip/index.js +15 -0
  116. package/common/middleware/web_ip/middleware.json +14 -0
  117. package/common/middleware/web_logger/index.js +156 -0
  118. package/common/middleware/web_logger/middleware.json +14 -0
  119. package/common/middleware/web_main/index.js +24 -0
  120. package/common/middleware/web_main/middleware.json +9 -0
  121. package/common/middleware/web_static/index.js +73 -0
  122. package/common/middleware/web_static/middleware.json +54 -0
  123. package/common/middleware/web_waf/index.js +385 -0
  124. package/common/middleware/web_waf/middleware.json +13 -0
  125. package/common/model/msg/index.js +88 -0
  126. package/common/model/msg/model.json +401 -0
  127. package/common/model/player/index.js +63 -0
  128. package/common/model/player/model.json +185 -0
  129. package/common/model/user/index.js +11 -0
  130. package/common/model/user/model.json +219 -0
  131. package/core/app/config.tpl.json +67 -0
  132. package/core/app/index.js +632 -0
  133. package/core/app/script.tpl.js +52 -0
  134. package/core/channel/index.js +899 -0
  135. package/core/channel/matcher.js +585 -0
  136. package/core/com/config.tpl.json +16 -0
  137. package/core/com/index.js +74 -0
  138. package/core/com/script.tpl.js +5 -0
  139. package/core/component/component.js +42 -0
  140. package/core/component/config.tpl.json +63 -0
  141. package/core/component/index.js +273 -0
  142. package/core/component/script.tpl.js +19 -0
  143. package/core/controller/config.tpl.json +14 -0
  144. package/core/controller/index.js +373 -0
  145. package/core/controller/script.tpl.js +27 -0
  146. package/core/factory/config.tpl.json +14 -0
  147. package/core/factory/entity.js +275 -0
  148. package/core/factory/index.js +241 -0
  149. package/core/factory/script.tpl.js +16 -0
  150. package/core/game/bat/index.js +137 -0
  151. package/core/game/bat/world.js +622 -0
  152. package/core/game/config.tpl.json +16 -0
  153. package/core/game/entity_admin.js +230 -0
  154. package/core/game/index.js +186 -0
  155. package/core/handler/config.tpl.json +22 -0
  156. package/core/handler/index.js +181 -0
  157. package/core/handler/script.tpl.js +23 -0
  158. package/core/logic/config.tpl.json +14 -0
  159. package/core/logic/index.js +59 -0
  160. package/core/logic/script.tpl.js +19 -0
  161. package/core/middleware/config.tpl.json +16 -0
  162. package/core/middleware/index.js +125 -0
  163. package/core/middleware/script.tpl.js +37 -0
  164. package/core/mod/config.tpl.json +22 -0
  165. package/core/mod/index.js +130 -0
  166. package/core/mod/script.tpl.js +34 -0
  167. package/core/model/config.tpl.json +219 -0
  168. package/core/model/index.js +272 -0
  169. package/core/model/model.js +27 -0
  170. package/core/model/script.tpl.js +20 -0
  171. package/core/notifier/config.tpl.json +14 -0
  172. package/core/notifier/index.js +77 -0
  173. package/core/notifier/script.tpl.js +20 -0
  174. package/core/plugin/config.tpl.json +24 -0
  175. package/core/plugin/index.js +232 -0
  176. package/core/plugin/script.tpl.js +51 -0
  177. package/core/pusher/config.tpl.json +14 -0
  178. package/core/pusher/index.js +161 -0
  179. package/core/pusher/script.tpl.js +20 -0
  180. package/core/room/bat/index.js +170 -0
  181. package/core/room/bat/room.js +524 -0
  182. package/core/room/config.tpl.json +20 -0
  183. package/core/room/index.js +249 -0
  184. package/core/room/room.js +61 -0
  185. package/core/scene/config.tpl.json +14 -0
  186. package/core/scene/index.js +466 -0
  187. package/core/scene/loop.js +1255 -0
  188. package/core/scene/map.js +28 -0
  189. package/core/scene/script.tpl.js +22 -0
  190. package/core/sender/config.tpl.json +14 -0
  191. package/core/sender/index.js +79 -0
  192. package/core/sender/script.tpl.js +20 -0
  193. package/core/service/config.tpl.json +14 -0
  194. package/core/service/index.js +100 -0
  195. package/core/service/script.tpl.js +25 -0
  196. package/core/store/config.tpl.json +26 -0
  197. package/core/store/index.js +1755 -0
  198. package/core/store/script.tpl.js +22 -0
  199. package/core/store/sql.js +1464 -0
  200. package/core/system/config.tpl.json +18 -0
  201. package/core/system/index.js +312 -0
  202. package/core/system/script.tpl.js +77 -0
  203. package/core/view/config.tpl.json +14 -0
  204. package/core/view/index.js +91 -0
  205. package/core/view/script.tpl.js +20 -0
  206. package/core/zone/bat/index.js +725 -0
  207. package/core/zone/config.tpl.json +54 -0
  208. package/core/zone/index.js +614 -0
  209. package/core/zone/script.tpl.js +10 -0
  210. package/core/zone/zone_bat.js +136 -0
  211. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +24 -0
  212. package/index.js +17 -333
  213. package/os.js +57 -0
  214. package/package.json +58 -58
  215. package/server.js +598 -0
  216. package/README.en.md +0 -36
  217. package/conf.json +0 -3
  218. package/core/base/mqtt/index.js +0 -1110
  219. package/core/base/mqtt/lib.js +0 -40
  220. package/core/base/web/index.js +0 -245
  221. package/core/com/api/com.json +0 -4
  222. package/core/com/api/drive.js +0 -668
  223. package/core/com/api/index.js +0 -108
  224. package/core/com/api/oauth.js +0 -158
  225. package/core/com/api/script.js +0 -32
  226. package/core/com/app/README.md +0 -3
  227. package/core/com/app/com.json +0 -4
  228. package/core/com/app/config.tpl.json +0 -16
  229. package/core/com/app/drive.js +0 -309
  230. package/core/com/app/index.js +0 -211
  231. package/core/com/app/script.js +0 -155
  232. package/core/com/cmd/com.json +0 -4
  233. package/core/com/cmd/config.tpl.json +0 -66
  234. package/core/com/cmd/drive.js +0 -513
  235. package/core/com/cmd/index.js +0 -354
  236. package/core/com/cmd/old/5w2h.js +0 -54
  237. package/core/com/cmd/old/drive.js +0 -423
  238. package/core/com/cmd/script.js +0 -11
  239. package/core/com/component/README.md +0 -3
  240. package/core/com/component/com.json +0 -4
  241. package/core/com/component/component.html +0 -16
  242. package/core/com/component/drive.js +0 -197
  243. package/core/com/component/index.js +0 -312
  244. package/core/com/component/script.js +0 -18
  245. package/core/com/db/com.json +0 -4
  246. package/core/com/db/drive.js +0 -1160
  247. package/core/com/db/index.js +0 -176
  248. package/core/com/event/com.json +0 -4
  249. package/core/com/event/drive.js +0 -133
  250. package/core/com/event/index.js +0 -345
  251. package/core/com/event/script.js +0 -26
  252. package/core/com/eventer/com.js +0 -477
  253. package/core/com/eventer/com.json +0 -4
  254. package/core/com/middleware/com.js +0 -154
  255. package/core/com/middleware/com.json +0 -4
  256. package/core/com/middleware/config.tpl.json +0 -8
  257. package/core/com/middleware/script.js +0 -9
  258. package/core/com/mqtt/com.json +0 -4
  259. package/core/com/mqtt/drive.js +0 -600
  260. package/core/com/mqtt/index.js +0 -572
  261. package/core/com/mqtt/mm_mqtt.js +0 -330
  262. package/core/com/mqtt/script.js +0 -604
  263. package/core/com/msg/com.js +0 -296
  264. package/core/com/msg/com.json +0 -4
  265. package/core/com/nav/com.json +0 -4
  266. package/core/com/nav/config.tpl.json +0 -75
  267. package/core/com/nav/drive.js +0 -549
  268. package/core/com/nav/index.js +0 -182
  269. package/core/com/nav/tpl/admin_pc/page_form.vue +0 -180
  270. package/core/com/nav/tpl/admin_pc/page_view.vue +0 -124
  271. package/core/com/nav/tpl/dev_pc/page_default.vue +0 -247
  272. package/core/com/nav/tpl/dev_pc/page_type.vue +0 -313
  273. package/core/com/nav/tpl/home_pc/page_default.vue +0 -234
  274. package/core/com/nav/tpl/home_pc/page_form.vue +0 -137
  275. package/core/com/nav/tpl/home_pc/page_list.vue +0 -234
  276. package/core/com/nav/tpl/home_pc/page_nav.vue +0 -221
  277. package/core/com/nav/tpl/home_pc/page_type.vue +0 -234
  278. package/core/com/nav/tpl/home_pc/page_view.vue +0 -125
  279. package/core/com/nav/tpl/home_phone/page_channel.vue +0 -234
  280. package/core/com/nav/tpl/home_phone/page_default.vue +0 -234
  281. package/core/com/nav/tpl/home_phone/page_form.vue +0 -137
  282. package/core/com/nav/tpl/home_phone/page_nav.vue +0 -237
  283. package/core/com/nav/tpl/home_phone/page_type.vue +0 -234
  284. package/core/com/nav/tpl/home_phone/page_view.vue +0 -125
  285. package/core/com/nav/viewmodel.js +0 -296
  286. package/core/com/param/drive.js +0 -366
  287. package/core/com/param/index.js +0 -80
  288. package/core/com/param/script.js +0 -12
  289. package/core/com/param/test.js +0 -98
  290. package/core/com/plugin/README.md +0 -3
  291. package/core/com/plugin/com.json +0 -4
  292. package/core/com/plugin/config.tpl.json +0 -26
  293. package/core/com/plugin/drive.js +0 -536
  294. package/core/com/plugin/index.js +0 -259
  295. package/core/com/plugin/script.js +0 -213
  296. package/core/com/rpc/com.json +0 -4
  297. package/core/com/rpc/drive.js +0 -160
  298. package/core/com/rpc/index.js +0 -87
  299. package/core/com/rpc/rpc.js +0 -118
  300. package/core/com/socket/com.json +0 -4
  301. package/core/com/socket/config.tpl.json +0 -14
  302. package/core/com/socket/drive.js +0 -403
  303. package/core/com/socket/index.js +0 -62
  304. package/core/com/socket/script.js +0 -42
  305. package/core/com/sql/drive.js +0 -1087
  306. package/core/com/sql/index.js +0 -83
  307. package/core/com/sql/script.js +0 -48
  308. package/core/com/static/com.json +0 -4
  309. package/core/com/static/drive.js +0 -220
  310. package/core/com/static/index.js +0 -149
  311. package/core/com/static/script.js +0 -28
  312. package/core/com/task/com.json +0 -4
  313. package/core/com/task/drive.js +0 -403
  314. package/core/com/task/index.js +0 -110
  315. package/core/com/task/script.js +0 -37
  316. package/core/com/timer/com.js +0 -217
  317. package/core/com/timer/com.json +0 -4
  318. package/core/com/tpl/com.js +0 -19
  319. package/core/com/tpl/com.json +0 -4
  320. package/lib/actions.js +0 -50
  321. package/lib/base.js +0 -361
  322. package/lib/com.js +0 -29
  323. package/lib/ref.js +0 -121
  324. package/middleware/cors/index.js +0 -119
  325. package/middleware/cors/middleware.json +0 -20
  326. package/middleware/csrf/index.js +0 -202
  327. package/middleware/csrf/middleware.json +0 -24
  328. package/middleware/ip_firewall/index.js +0 -476
  329. package/middleware/ip_firewall/middleware.json +0 -109
  330. package/middleware/mqtt_base/index.js +0 -10
  331. package/middleware/mqtt_base/middleware.json +0 -11
  332. package/middleware/security_audit/index.js +0 -543
  333. package/middleware/security_audit/middleware.json +0 -48
  334. package/middleware/waf/index.js +0 -343
  335. package/middleware/waf/middleware.json +0 -10
  336. package/middleware/waf_ddos/index.js +0 -520
  337. package/middleware/waf_ddos/middleware.json +0 -38
  338. package/middleware/waf_xss/index.js +0 -269
  339. package/middleware/waf_xss/middleware.json +0 -18
  340. package/middleware/web_after/index.js +0 -33
  341. package/middleware/web_after/middleware.json +0 -10
  342. package/middleware/web_base/index.js +0 -90
  343. package/middleware/web_base/middleware.json +0 -10
  344. package/middleware/web_before/index.js +0 -27
  345. package/middleware/web_before/middleware.json +0 -10
  346. package/middleware/web_check/index.js +0 -28
  347. package/middleware/web_check/middleware.json +0 -10
  348. package/middleware/web_main/index.js +0 -28
  349. package/middleware/web_main/middleware.json +0 -10
  350. package/middleware/web_proxy/index.js +0 -37
  351. package/middleware/web_proxy/middleware.json +0 -10
  352. package/middleware/web_render/index.js +0 -87
  353. package/middleware/web_render/middleware.json +0 -10
  354. package/middleware/web_socket/index.js +0 -34
  355. package/middleware/web_socket/middleware.json +0 -10
  356. package/middleware/web_static/index.js +0 -115
  357. package/middleware/web_static/middleware.json +0 -10
  358. /package/{core/com → com}/api/README.md +0 -0
  359. /package/{core/com → com}/db/README.md +0 -0
  360. /package/{core/com → com}/mqtt/README.md +0 -0
  361. /package/{core/com → com}/nav/README.md +0 -0
  362. /package/{core/com → com}/nav/tpl/admin_pc/page_channel.vue +0 -0
  363. /package/{core/com → com}/nav/tpl/admin_pc/page_default.vue +0 -0
  364. /package/{core/com → com}/nav/tpl/admin_pc/page_lang.vue +0 -0
  365. /package/{core/com → com}/nav/tpl/admin_pc/page_nav.vue +0 -0
  366. /package/{core/com → com}/nav/tpl/admin_pc/page_table.vue +0 -0
  367. /package/{core/com → com}/nav/tpl/admin_pc/page_type.vue +0 -0
  368. /package/{core/com → com}/nav/tpl/dev_pc/page_channel.vue +0 -0
  369. /package/{core/com → com}/nav/tpl/dev_pc/page_config.vue +0 -0
  370. /package/{core/com → com}/nav/tpl/dev_pc/page_form.vue +0 -0
  371. /package/{core/com → com}/nav/tpl/dev_pc/page_nav.vue +0 -0
  372. /package/{core/com → com}/nav/tpl/dev_pc/page_table.vue +0 -0
  373. /package/{core/com → com}/nav/tpl/home_pc/page_channel.vue +0 -0
  374. /package/{core/com → com}/nav/tpl/home_phone/page_list.vue +0 -0
  375. /package/{core/com → com}/param/README.md +0 -0
  376. /package/{core/com/cmd → com/pendant}/README.md +0 -0
  377. /package/{core/com → com}/socket/README.md +0 -0
  378. /package/{core/com → com}/sql/README.md +0 -0
  379. /package/{core/com → com}/static/README.md +0 -0
  380. /package/{core/com → com}/task/README.md +0 -0
@@ -0,0 +1,273 @@
1
+ const { Aedes } = require('aedes');
2
+ const net = require('net');
3
+ const { Adapter } = require('./adapter');
4
+ const { Persist } = require('./custom_persistence.js');
5
+
6
+ /**
7
+ * MQTT适配器
8
+ */
9
+ class Mqtt extends Adapter {
10
+ static config = {
11
+ host: '127.0.0.1',
12
+ port: 1883,
13
+ cache: true,
14
+ prefix: 'mqtt_',
15
+ ttl: 3600000,
16
+ push_topic: 'client/iot/${client_id}',
17
+ receive_topic: 'client/iot/${client_id}',
18
+ auth_enabled: false,
19
+ auth_users: [], // 用户名密码列表,格式: {username: 'user', password: 'pass'}
20
+ };
21
+ /**
22
+ * 构造函数
23
+ * @param {object} config 配置参数
24
+ * @param {object} parent 父对象
25
+ */
26
+ constructor(config, parent) {
27
+ super({ ...Mqtt.config, ...config }, parent);
28
+ }
29
+ }
30
+
31
+ /**
32
+ * 初始化适配器
33
+ * @param {object} cache 缓存管理器
34
+ * @private
35
+ */
36
+ Mqtt.prototype._init = function (cache) {
37
+ // 创建自定义持久化实例
38
+ let persist = null;
39
+ if (this.server && cache) {
40
+ persist = new Persist(this.config, cache);
41
+ this.log('info', 'MQTT使用自定义持久化');
42
+ }
43
+ // 创建aedes实例,传入自定义持久化
44
+ this.broker = new Aedes({
45
+ persist: persist
46
+ });
47
+ // 创建TCP服务器
48
+ this.web = net.createServer(this.broker.handle);
49
+
50
+ // 配置身份验证
51
+ this._setupAuth();
52
+
53
+ // 监听客户端连接
54
+ this.broker.on('client', (client) => {
55
+ // 存储客户端连接
56
+ this.clients.set(client.id, client);
57
+
58
+ // 记录连接日志
59
+ this.log('info', `MQTT客户端连接成功: ${client.id}`);
60
+ });
61
+ return this;
62
+ };
63
+
64
+ /**
65
+ * 检查用户是否存在
66
+ * @param {string} client_id 客户端ID
67
+ * @param {string} username 用户名
68
+ * @param {string} password 密码
69
+ * @returns {Promise<boolean>} 是否存在
70
+ */
71
+ Mqtt.prototype.checkUser = async function (client_id, username, password) {
72
+ this.log('debug', '检查用户', { client_id });
73
+ let pass = password ? password.toString() : '';
74
+ // 查找匹配的用户
75
+ // 在实际应用中,这里可以改为异步数据库查询
76
+ // 例如: return await db.query('SELECT * FROM users WHERE username = ? AND password = ?', [username, password_str]);
77
+ return this.config.auth_users.find(u => u.username === username && u.password === pass);
78
+ };
79
+
80
+ /**
81
+ * 验证客户端身份
82
+ * @param {object} client 客户端对象
83
+ * @param {string} username 用户名
84
+ * @param {object} password 密码
85
+ * @returns {Promise<boolean>} 是否验证通过
86
+ */
87
+ Mqtt.prototype.auth = async function (client, username, password) {
88
+ // 查找匹配的用户
89
+ return await this.checkUser(client.id, username, password);
90
+ };
91
+
92
+ /**
93
+ * 配置身份验证
94
+ * @private
95
+ */
96
+ Mqtt.prototype._setupAuth = function () {
97
+ // 如果启用了身份验证
98
+ if (this.config.auth_enabled) {
99
+ // 监听认证事件
100
+ this.broker.auth = async (client, username, password, callback) => {
101
+ try {
102
+ // 转换密码格式为字符串
103
+ let password_str = password ? password.toString() : '';
104
+
105
+ // 记录认证尝试
106
+ this.log('debug', `MQTT认证尝试: clientId=${client.id}, username=${username}, password=${password_str ? '[存在]' : '[不存在]'}`);
107
+
108
+ // 如果提供了自定义验证函数,则使用自定义验证
109
+ let user = await this.auth(client, username, password_str);
110
+ if (user) {
111
+ this.log('info', `MQTT认证成功: clientId=${client.id}, username=${username}`);
112
+ return callback(null, true);
113
+ } else {
114
+ this.log('warn', `MQTT认证失败: clientId=${client.id}, username=${username}, 用户名或密码错误`);
115
+ return callback(new Error('用户名或密码错误'));
116
+ }
117
+ } catch (error) {
118
+ this.log('error', `MQTT认证错误: clientId=${client.id}`, error);
119
+ return callback(new Error('认证服务内部错误'));
120
+ }
121
+ };
122
+
123
+ this.log('info', 'MQTT身份验证已启用');
124
+ }
125
+ };
126
+
127
+ /**
128
+ * 启动MQTT服务器
129
+ */
130
+ Mqtt.prototype.start = function () {
131
+ let {
132
+ host,
133
+ port
134
+ } = this.config;
135
+
136
+ this.web.listen(port, host, () => {
137
+ this.log('info', `MQTT启动成功!访问地址:mqtt://${host}:${port}`);
138
+ if (this.config.auth_enabled) {
139
+ this.log('info', 'MQTT身份验证已启用');
140
+ }
141
+ });
142
+
143
+ // 监听服务器错误
144
+ this.web.on('error', (error) => {
145
+ this.log('error', 'MQTT服务器启动失败:', error);
146
+ });
147
+ };
148
+
149
+ /**
150
+ * 发送消息
151
+ * @private
152
+ * @param {string} client_id 客户端ID
153
+ * @param {string} message 消息内容
154
+ * @param {object} [options] 发送选项
155
+ * @param {number} [options.qos=0] QoS级别(0, 1, 2)
156
+ * @param {boolean} [options.retain=false] 是否为retain消息
157
+ * @returns {boolean} 发送是否成功
158
+ */
159
+ Mqtt.prototype._send = function (client_id, message, options) {
160
+ let client = this.clients.get(client_id);
161
+ if (client) {
162
+ try {
163
+ // 默认选项
164
+ const OPTS = {
165
+ qos: 0,
166
+ retain: false,
167
+ push_topic: null, ...options || {}};
168
+
169
+ // MQTT通常需要发布到特定主题,这里简单处理
170
+ // 实际应用中可能需要根据协议规范处理
171
+ let topic = OPTS.push_topic || this.config.push_topic.replace('${client_id}', client_id);
172
+ this.broker.publish({
173
+ topic: topic,
174
+ payload: message,
175
+ qos: OPTS.qos,
176
+ retain: OPTS.retain
177
+ });
178
+
179
+ this.log('debug', `MQTT服务器向 ${client_id} 发送消息: ${message}, qos: ${OPTS.qos}, retain: ${OPTS.retain}`);
180
+ return true;
181
+ } catch (error) {
182
+ this.log('error', 'MQTT发送消息失败:', error);
183
+ return false;
184
+ }
185
+ } else {
186
+ this.log('warn', `MQTT客户端 ${client_id} 不存在或已关闭`);
187
+ return false;
188
+ }
189
+ };
190
+
191
+ /**
192
+ * 发布消息到指定主题
193
+ * @param {string} topic 主题
194
+ * @param {string} message 消息内容
195
+ * @param {object} [options] 发布选项
196
+ * @param {number} [options.qos=0] QoS级别(0, 1, 2)
197
+ * @param {boolean} [options.retain=false] 是否为retain消息
198
+ * @returns {boolean} 发布是否成功
199
+ */
200
+ Mqtt.prototype.publish = function (topic, message, options) {
201
+ try {
202
+ // 默认选项
203
+ const OPTS = {qos: 0,
204
+ retain: false,
205
+ push_topic: null, ...options || {}};
206
+
207
+ this.broker.publish({
208
+ topic: topic,
209
+ payload: message,
210
+ qos: OPTS.qos,
211
+ retain: OPTS.retain
212
+ });
213
+
214
+ this.log('debug', `MQTT服务器发布消息到主题 ${topic}: ${message}, qos: ${OPTS.qos}, retain: ${OPTS.retain}`);
215
+ return true;
216
+ } catch (error) {
217
+ this.log('error', 'MQTT发布消息失败:', error);
218
+ return false;
219
+ }
220
+ };
221
+
222
+ /**
223
+ * 订阅主题
224
+ * @param {string} topic 主题
225
+ * @param {Function} callback 回调函数
226
+ */
227
+ Mqtt.prototype.subscribe = function (topic, callback) {
228
+ this.broker.subscribe(topic, callback);
229
+ };
230
+
231
+ /**
232
+ * 取消订阅主题
233
+ * @param {string} topic 主题
234
+ * @param {Function} callback 回调函数
235
+ */
236
+ Mqtt.prototype.unsubscribe = function (topic, callback) {
237
+ this.broker.unsubscribe(topic, callback);
238
+ };
239
+
240
+ /**
241
+ * 停止服务器
242
+ */
243
+ Mqtt.prototype.stop = function () {
244
+ // 调用父类的_stop方法
245
+ this._stop();
246
+
247
+ // 关闭服务器
248
+ this.web.close(() => {
249
+ // 清空客户端集合
250
+ this.clients.clear();
251
+ this.log('info', 'MQTT服务器已停止');
252
+ });
253
+ };
254
+
255
+ /**
256
+ * 停止Adapter服务器
257
+ * @private
258
+ */
259
+ Mqtt.prototype._stop = function () {
260
+ // 关闭所有客户端连接
261
+ for (let [client_id, client] of this.clients) {
262
+ try {
263
+ // 使用MQTT客户端的断开连接方法
264
+ if (client && client.close) {
265
+ client.close();
266
+ }
267
+ } catch (error) {
268
+ this.log('error', `关闭MQTT客户端 ${client_id} 失败:`, error);
269
+ }
270
+ }
271
+ };
272
+
273
+ exports.Mqtt = Mqtt;
@@ -0,0 +1,113 @@
1
+ const net = require('net');
2
+ const { Adapter } = require('./adapter');
3
+
4
+ /**
5
+ * Socket适配器
6
+ */
7
+ class Socket extends Adapter {
8
+ static config = {
9
+ host: '0.0.0.0',
10
+ port: 8200
11
+ };
12
+ /**
13
+ * 构造函数
14
+ * @param {object} config 配置对象
15
+ * @param {object} parent 父对象
16
+ */
17
+ constructor(config, parent) {
18
+ super({ ...Socket.config, ...config }, parent);
19
+ }
20
+ }
21
+
22
+ /**
23
+ * 初始化适配器
24
+ * @private
25
+ */
26
+ Socket.prototype._init = function () {
27
+ // 创建TCP服务器
28
+ this.web = net.createServer(this._onConn.bind(this));
29
+
30
+ return this;
31
+ };
32
+
33
+ /**
34
+ * 客户端连接处理
35
+ * @param {Socket} socket 客户端socket
36
+ * @private
37
+ */
38
+ Socket.prototype._onConn = function (socket) {
39
+ // 生成唯一客户端ID
40
+ let client_id = `client_${this.genClientId()}`;
41
+ // 存储客户端连接
42
+ this.clients.set(client_id, socket);
43
+
44
+ // 记录连接日志
45
+ this.log('info', `客户端连接成功: ${client_id} (${socket.remoteAddress}:${socket.remotePort})`);
46
+
47
+ // 监听数据接收
48
+ socket.on('data', (data) => {
49
+ this.log('debug', `服务器接收 ${client_id} 消息: ${data.toString()}`);
50
+ this._onMessage(client_id, data.toString());
51
+ });
52
+
53
+ // 监听连接关闭
54
+ socket.on('close', () => {
55
+ this._onClose(client_id);
56
+ });
57
+
58
+ // 监听错误事件
59
+ socket.on('error', (error) => {
60
+ this._onError(client_id, error);
61
+ });
62
+ };
63
+
64
+ /**
65
+ * 启动适配器
66
+ */
67
+ Socket.prototype.start = function () {
68
+ let { host, port } = this.config;
69
+ this.web.listen(port, host, () => {
70
+ this.log('info', `启动成功!监听地址:${host}:${port}`);
71
+ });
72
+
73
+ // 监听服务器错误
74
+ this.web.on('error', (error) => {
75
+ this.log('error', '启动失败:', error);
76
+ });
77
+ };
78
+
79
+ /**
80
+ * 发送消息
81
+ * @param {string} client_id 客户端ID
82
+ * @param {string} message 消息内容
83
+ * @returns {boolean} 发送是否成功
84
+ * @private
85
+ */
86
+ Socket.prototype._send = function (client_id, message) {
87
+ let socket = this.clients.get(client_id);
88
+ if (socket && !socket.destroyed) {
89
+ socket.write(message);
90
+ this.log('debug', `服务器向 ${client_id} 发送消息: ${message}`);
91
+ return true;
92
+ } else {
93
+ this.log('warn', `客户端 ${client_id} 不存在或已关闭`);
94
+ return false;
95
+ }
96
+ };
97
+
98
+ /**
99
+ * 停止适配器
100
+ */
101
+ Socket.prototype.stop = function () {
102
+ // 调用父类的_stop方法
103
+ this._stop();
104
+
105
+ // 关闭服务器
106
+ this.web.close(() => {
107
+ // 清空客户端集合
108
+ this.clients.clear();
109
+ this.log('info', '服务器已停止');
110
+ });
111
+ };
112
+
113
+ exports.Socket = Socket;
package/adapter/web.js ADDED
@@ -0,0 +1,67 @@
1
+ const Koa = require('koa');
2
+ const { Adapter } = require('./adapter');
3
+
4
+ /**
5
+ * Web适配器类
6
+ * 基于Koa框架的HTTP服务器适配器
7
+ */
8
+ class Web extends Adapter {
9
+ static config = {
10
+ host: '0.0.0.0',
11
+ port: 8000
12
+ };
13
+
14
+ /**
15
+ * 构造函数
16
+ * @param {object} config 配置参数
17
+ * @param {object} parent 父对象
18
+ */
19
+ constructor(config, parent) {
20
+ super({ ...Web.config, ...config }, parent);
21
+ }
22
+ }
23
+
24
+ /**
25
+ * 预置
26
+ */
27
+ Web.prototype._preset = function () {
28
+ this.web = new Koa(this.config);
29
+ // 设置cookie签名密钥
30
+ this.web.keys = ['mos-secret-key-2024', 'mos-backup-key-2024'];
31
+ };
32
+
33
+ /**
34
+ * 使用中间件
35
+ * @param {function} middleware 中间件函数
36
+ */
37
+ Web.prototype.use = function (middleware) {
38
+ this.web.use(middleware);
39
+ };
40
+
41
+ /**
42
+ * 初始化
43
+ * @param {object} server 服务器实例
44
+ */
45
+ Web.prototype.init = function (server) {
46
+ if (server) {
47
+ this.getServer = function () {
48
+ return server;
49
+ };
50
+ }
51
+ return this;
52
+ };
53
+
54
+ /**
55
+ * 启动
56
+ */
57
+ Web.prototype.start = function () {
58
+ var {
59
+ host,
60
+ port
61
+ } = this.config;
62
+ this.web.listen(port, host, () => {
63
+ this.log('info', `web启动成功!访问网址:http://${host}:${port}`);
64
+ });
65
+ };
66
+
67
+ exports.Web = Web;
@@ -0,0 +1,146 @@
1
+ const Koa = require('koa');
2
+ const websocket = require('koa-websocket');
3
+ const { Adapter } = require('./adapter.js');
4
+
5
+ /**
6
+ * WebSocket适配器
7
+ */
8
+ class WebSocket extends Adapter {
9
+ static config = {
10
+ host: '0.0.0.0',
11
+ port: 8300
12
+ };
13
+ /**
14
+ * 构造函数
15
+ * @param {object} config 配置对象
16
+ * @param {object} parent 父对象
17
+ */
18
+ constructor(config, parent) {
19
+ super({ ...WebSocket.config, ...config }, parent);
20
+ }
21
+ }
22
+
23
+ /**
24
+ * 初始化WebSocket适配器
25
+ * @param {object} web 应用实例
26
+ * @private
27
+ */
28
+ WebSocket.prototype._init = function (web) {
29
+ // 如果提供了应用实例
30
+ if (web) {
31
+ // 检查是否已经是websocket实例
32
+ if (web.ws) {
33
+ // 已经是websocket实例,直接使用
34
+ this._web = web;
35
+ this.external_app = true;
36
+ } else {
37
+ // 不是websocket实例,包装它
38
+ this._web = websocket(web);
39
+ this.external_app = true;
40
+ }
41
+ } else {
42
+ // 没有提供应用实例,创建新的
43
+ this._web = websocket(new Koa());
44
+ this.external_app = false;
45
+ }
46
+ this.broker = this._web.ws;
47
+ // 简化实现:不设置中间件,在连接时处理
48
+ };
49
+
50
+ /**
51
+ * 发送消息
52
+ * @private
53
+ * @param {string} client_id 客户端ID
54
+ * @param {string} message 消息内容
55
+ * @returns {boolean} 发送是否成功
56
+ */
57
+ WebSocket.prototype._send = function (client_id, message) {
58
+ let ws = this.clients.get(client_id);
59
+ if (ws && ws.readyState === 1) { // WebSocket.OPEN
60
+ ws.send(message);
61
+ this.log('debug', `服务器向 ${client_id} 发送消息: ${message}`);
62
+ return true;
63
+ } else {
64
+ this.log('warn', `客户端 ${client_id} 不存在或已关闭`);
65
+ return false;
66
+ }
67
+ };
68
+
69
+ /**
70
+ * 启动WebSocket服务器
71
+ * @returns {object|undefined} 服务器实例或undefined
72
+ */
73
+ WebSocket.prototype.start = function () {
74
+ let {
75
+ host,
76
+ port
77
+ } = this.config;
78
+ // 如果没有提供外部app实例,启动自己的服务器
79
+ if (!this.external_app) {
80
+ return this._web.listen(port, host, () => {
81
+ this.log('info', `启动成功!监听地址:ws://127.0.0.1:${port}`);
82
+ });
83
+ } else {
84
+ // 如果提供了外部app实例,只需要记录日志
85
+ this.log('info', `启动成功!访问地址:ws://127.0.0.1:${port}`);
86
+ }
87
+
88
+ // 监听服务器错误(仅当是自己创建的服务器时)
89
+ if (this._tcp_server && !this.external_app) {
90
+ this._tcp_server.on('error', (error) => {
91
+ this.log('error', '服务器启动失败:', error);
92
+ });
93
+ }
94
+ };
95
+
96
+ /**
97
+ * 停止Adapter服务器
98
+ * @private
99
+ */
100
+ WebSocket.prototype._stop = function () {
101
+ // 关闭所有客户端连接
102
+ for (let [, ws] of this.clients) {
103
+ ws.close();
104
+ }
105
+ };
106
+
107
+ /**
108
+ * 处理消息接收
109
+ * @private
110
+ * @param {string} client_id 客户端ID
111
+ * @param {object} ws WebSocket实例
112
+ * @param {string} message 消息内容
113
+ */
114
+ WebSocket.prototype._onMessage = function (client_id, ws, message) {
115
+ this.log('debug', '收到消息', { client_id: client_id });
116
+ };
117
+
118
+ /**
119
+ * 处理连接关闭
120
+ * @private
121
+ * @param {string} client_id 客户端ID
122
+ */
123
+ WebSocket.prototype._onClose = function (client_id) {
124
+ this.clients.delete(client_id);
125
+ this.log('info', `客户端断开连接: ${client_id}`);
126
+ };
127
+
128
+ /**
129
+ * 处理错误事件
130
+ * @private
131
+ * @param {string} client_id 客户端ID
132
+ * @param {Error} error 错误对象
133
+ */
134
+ WebSocket.prototype._onError = function (client_id, error) {
135
+ this.log('error', `客户端 ${client_id} 发生错误:`, error);
136
+ };
137
+
138
+ /**
139
+ * 使用中间件
140
+ * @param {function} middleware 中间件函数
141
+ */
142
+ WebSocket.prototype.use = function (middleware) {
143
+ this.broker.use(middleware);
144
+ };
145
+
146
+ exports.WebSocket = WebSocket;
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "Api",
3
+ "title": "API管理器",
4
+ "state": 1
5
+ }
@@ -1,22 +1,22 @@
1
1
  {
2
2
  // 路由路径
3
- "path": "{path}",
3
+ "path": "{path || '/'}",
4
4
  // 名称, 用于动态增删改API配置
5
- "name": "{name}",
5
+ "name": "{name || 'api'}",
6
6
  // 标题, 用于开发文档显示
7
- "title": "示例接口",
7
+ "title": "{title || '示例接口'}",
8
8
  // 描述, 用于介绍该路由的作用
9
- "description": "描述接口使用方法",
9
+ "description": "{description || '描述接口使用方法'}",
10
10
  // 函数文件, 用于驱动脚本, 实现业务
11
- "func_file": "./index.js",
11
+ "main": "{main || './index.js'}",
12
12
  // 请求方法, 选填 GET, POST, ALL。 ALL指同时支持GET/POST
13
13
  "method": "ALL",
14
14
  // 开放域, 如果域未开放, 则只有特定的协议头才能访问该API, 同时API文档不接见
15
15
  "scope": true,
16
- // 缓存时长, 单位: 分钟。使用缓存后, 二次访问API直接从缓存读取, 不重复执行业务脚本
17
- "cache": 0,
18
16
  // 是否启用
19
17
  "state": 1,
18
+ // 缓存时长, 单位: 分钟。使用缓存后, 二次访问API直接从缓存读取, 不重复执行业务脚本
19
+ "cache": 0,
20
20
  // 是否客户端缓存, 使用客户端则不再访问服务器, 直接从浏览器中拿; 使用服务端缓存则会访问服务器
21
21
  "client_cache": false,
22
22
  // SQL配置路径, 用于将请求参数拼接成SQL, 实现比较复杂的查询
@@ -26,7 +26,7 @@
26
26
  /* 授权协议 */
27
27
  "oauth": {
28
28
  // 是否需要登录, true表示需要登录才能访问该接口
29
- "signIn": false,
29
+ "sign_in": false,
30
30
  // 会员权限级别, 如果小于该值, 则无法访问, 0为不限制
31
31
  "vip": 0,
32
32
  // 管理员权限级别, 如果小于该值, 则无法访问, 0为不限制