<commit_before>const frida = require('frida')
const IO = require('koa-socket')

const {
  serializeDevice,
  serializeApp,
  FridaUtil
} = require('./utils')

const deviceMgr = frida.getDeviceManager()
const channels = {}

for (let channel of ['devices', 'session', 'shell']) {
  channels[channel] = new IO({ namespace: '/' + channel, ioOptions: { path: '/msg' } })
}

deviceMgr.events.listen('added', async device => {
  channels.devices.broadcast('deviceAdd', serializeDevice(device))
})

deviceMgr.events.listen('removed', async device => {
  channels.devices.broadcast('deviceRemove', serializeDevice(device))
})

channels.session.on('attach', async(ctx, { device, bundle }) => {
  console.log('connect to', device, bundle)

  let { socket, acknowledge } = ctx
  let dev, session, app

  if (!device || !bundle)
    return socket.disconnect('connection_error: invalid parameters')

  try {
    dev = await frida.getDevice(device)
    if (dev.type != 'tether')
      throw new Error('device not found')

    socket.emit('device', serializeDevice(dev))
    let apps = await dev.enumerateApplications()
    app = apps.find(app => app.identifier == bundle)
    if (!app)
      throw new Error('app not installed')

    socket.emit('app', serializeApp(app))
    session = await dev.attach(app.name)
    acknowledge({
      status: 'ok',
      pid: session.pid
    })
  } catch(ex) {
    acknowledge({status: 'error', msg: ex})
    console.error(ex)
    return socket.disconnect(ex)
  }

  session.events.listen('detached', reason => {
    socket.emit('detached', reason)
    socket.disconnect('session detached')
  })

  // todo: remove koa-socket
  // totally a crap
  socket.socket.on('modules', async (data, ack) => {
    let modules = await session.enumerateModules()
    ack(modules)
  }).on('detach', async data => {
    socket.disconnect()<commit_msg>do not add argument when disconnect<commit_after>const frida = require('frida')
const IO = require('koa-socket')

const {
  serializeDevice,
  serializeApp,
  FridaUtil
} = require('./utils')

const deviceMgr = frida.getDeviceManager()
const channels = {}

for (let channel of ['devices', 'session', 'shell']) {
  channels[channel] = new IO({ namespace: '/' + channel, ioOptions: { path: '/msg' } })
}

deviceMgr.events.listen('added', async device => {
  channels.devices.broadcast('deviceAdd', serializeDevice(device))
})

deviceMgr.events.listen('removed', async device => {
  channels.devices.broadcast('deviceRemove', serializeDevice(device))
})

channels.session.on('attach', async(ctx, { device, bundle }) => {
  console.log('connect to', device, bundle)

  let { socket, acknowledge } = ctx
  let dev, session, app

  if (!device || !bundle)
    return socket.emit('err', 'invalid parameters').disconnect()

  try {
    dev = await frida.getDevice(device)
    if (dev.type != 'tether')
      throw new Error('device not found')

    socket.emit('device', serializeDevice(dev))
    let apps = await dev.enumerateApplications()
    app = apps.find(app => app.identifier == bundle)
    if (!app)
      throw new Error('app not installed')

    socket.emit('app', serializeApp(app))
    session = await dev.attach(app.name)
    acknowledge({
      status: 'ok',
      pid: session.pid
    })
  } catch(ex) {
    acknowledge({status: 'error', msg: ex})
    console.error(ex)
    return socket.disconnect()
  }

  session.events.listen('detached', reason =>
    socket.emit('detached', reason).disconnect())

  // todo: remove koa-socket
  // totally a crap
  socket.socket.on('modules', async (data, ack) => {
    let modules = await session.enumerateModules()
    ack(modules)
  }).on('detach', async data => {
    socket.disconnect()
{'fraction_non_alphanumeric': 0.15832205683355885, 'fraction_numerical': 0.0, 'mean_word_length': 3.85676741130092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6489523', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1131, 'n_words': 274}