File size: 4,893 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const NodeRSA = require('node-rsa')
const { randomStr } = require('./utils/tools')
const { AESEncryptAsync, SHA1Encrypt } = require('./utils/encrypt')
const { KeyDB, GroupDB, NotifyDB, NotifyConfigDB, ScriptGroupDB } = require('./utils/db-class')

async function initKeyDB() {
  const keyDB = new KeyDB().getInstance()
  let keyData = await keyDB.findOneAsync({})

  if (keyData?.user) {

    if (!keyData?.jwtToken) {
      logger.info('🔒初始化jwtToken')
      const jwtToken = randomStr(32)
      await keyDB.updateAsync({ _id: keyData._id }, { $set: { jwtToken } })
    }

    const { _id, ipWhiteList = [] } = keyData
    try {
      let { ipWhiteList = [] } = await keyDB.findOneAsync({})
      const filteredList = ipWhiteList.filter(ip => typeof ip === 'string' && ip.trim() !== '')
      if (filteredList.length > 0) global.ALLOWED_IPS = filteredList
    } catch (error) {
      logger.error('设置全局IP白名单失败:', error)
    }
    logger.info('公私钥已存在[重新生成会导致已保存的ssh密钥信息失效]')
    return
  }

  const randomUsername = randomStr(8)
  const randomPassword = randomStr(8)

  let newConfig = {
    user: randomUsername,
    pwd: SHA1Encrypt(randomPassword),
    commonKey: randomStr(32),
    jwtToken: randomStr(32),
    publicKey: '',
    privateKey: ''
  }
  await keyDB.insertAsync(newConfig)
  let key = new NodeRSA({ b: 2048 })
  key.setOptions({ encryptionScheme: 'pkcs1', environment: 'browser' })
  let privateKey = key.exportKey('pkcs1-private-pem')
  let publicKey = key.exportKey('pkcs8-public-pem')
  newConfig.privateKey = await AESEncryptAsync(privateKey, newConfig.commonKey) // 加密私钥
  newConfig.publicKey = publicKey // 公开公钥
  await keyDB.updateAsync({}, { $set: newConfig }, { upsert: true })

  // 在控制台打印随机生成的账号密码
  logger.info('========================================')
  logger.info('EasyNode 默认登录凭据 (请及时更改):')
  logger.info(`用户名: ${ randomUsername }`)
  logger.info(`密码: ${ randomPassword }`)
  logger.info('========================================')

}

async function initGroupDB() {
  const groupDB = new GroupDB().getInstance()
  let count = await groupDB.countAsync({})
  if (count === 0) {
    logger.info('初始化groupDB✔')
    const defaultData = [{ '_id': 'default', 'name': '默认分组', 'index': 0 }]
    return groupDB.insertAsync(defaultData)
  }
  return Promise.resolve()
}

async function initNotifyDB() {
  const notifyDB = new NotifyDB().getInstance()
  let count = await notifyDB.countAsync({})
  if (count !== 0) return
  logger.info('初始化notifyDB✔')
  let defaultData = [{
    'type': 'login',
    'desc': '登录面板提醒',
    'sw': false
  }, {
    'type': 'err_login',
    'desc': '登录错误提醒(连续5次)',
    'sw': false
  }, {
    'type': 'updatePwd',
    'desc': '修改密码提醒',
    'sw': false
  }, {
    'type': 'host_login',
    'desc': '服务器登录提醒',
    'sw': false
  }, {
    'type': 'onekey_complete',
    'desc': '批量指令执行完成提醒',
    'sw': false
  }, {
    'type': 'host_expired',
    'desc': '服务器到期提醒',
    'sw': false
  }]
  return notifyDB.insertAsync(defaultData)
}

async function initNotifyConfigDB() {
  const notifyConfigDB = new NotifyConfigDB().getInstance()
  let notifyConfig = await notifyConfigDB.findOneAsync({})
  logger.info('初始化NotifyConfigDB✔')
  const defaultData = {
    type: 'sct',
    sct: {
      sendKey: ''
    },
    email: {
      service: 'QQ',
      user: '',
      pass: '',
      useCustom: false,
      host: '',
      port: 465,
      secure: true,
      to: ''
    },
    tg: {
      token: '',
      chatId: ''
    },
    webhook: {
      url: '',
      method: 'POST',
      contentType: 'application/json',
      headers: '',
      template: '{\n  "title": "{{title}}",\n  "content": "{{content}}",\n  "description": "{{content}}",\n  "timestamp": "{{timestamp}}",\n  "datetime": "{{datetime}}"\n}'
    }
  }
  if (notifyConfig) {
    await notifyConfigDB.removeAsync({ _id: notifyConfig._id })
    delete notifyConfig._id
    return notifyConfigDB.insertAsync(Object.assign({}, defaultData, notifyConfig))
  }
  return notifyConfigDB.insertAsync(defaultData)
}

async function initScriptGroupDB() {
  const scriptGroupDB = new ScriptGroupDB().getInstance()
  let count = await scriptGroupDB.countAsync({})
  if (count === 0) {
    logger.info('初始化ScriptGroupDB✔')
    const defaultData = [
      { '_id': 'default', 'name': '默认分组', 'index': 0 },
      { '_id': 'builtin', 'name': '内置脚本', 'index': -1 }
    ]
    return scriptGroupDB.insertAsync(defaultData)
  }
  return Promise.resolve()
}

module.exports = async () => {
  await initKeyDB()
  await initNotifyDB()
  await initGroupDB()
  await initScriptGroupDB()
  await initNotifyConfigDB()
}