Skip to content

Commit

Permalink
fix: 允许用户透传icqq配置
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-cn committed Aug 24, 2023
1 parent eb05ed1 commit 852b990
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ general: # 通用配置,在单个配置省略时的默认值
use_ws: true # 是否启用 websocket
webhook: [ ] # http 上报地址
ws_reverse: [ ] # 反向ws连接地址
protocol:
platform: 2
# 每个账号的单独配置(用于覆盖通用配置)
123456789:
version: V11 # 使用的oneBot版本
password: abcedfghi # 账号密码,未配置则扫码登陆
# 。。。其他配置项参见上方对应oneBot版本的通用配置
25 changes: 13 additions & 12 deletions src/onebot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'icqq-cq-enable'
import {EventEmitter} from 'events'
import {App} from "./server/app";
import {deepClone, deepMerge, omit} from "./utils";
import {deepClone, deepMerge} from "./utils";
import {join} from "path";
import {Client, Platform} from "icqq";
import {genDmMessageId, genGroupMessageId} from 'icqq'
import {Client} from "icqq";
import {genDmMessageId, genGroupMessageId,Config as IcqqConfig} from 'icqq'
import {V11} from "./service/V11";
import {V12} from "./service/V12";
import {MayBeArray} from "./types";
Expand All @@ -24,12 +24,14 @@ export class OneBot<V extends OneBot.Version> extends EventEmitter {
constructor(public app: App, public readonly uin: number, config: MayBeArray<OneBotConfig>) {
super()
config = [].concat(config)
let platform=this.app.config.platform
let sign_api_addr=this.app.config.sign_api_addr
const protocolConfig:IcqqConfig={
data_dir: join(App.configDir, 'data'),
...this.app.config.general
}
this.config = config.map(c => {
if(c.platform) platform=c.platform
if (c.password) this.password = c.password
if (!c.version) c.version = 'V11'
if(!c.protocol) c.protocol={}
Object.assign(protocolConfig,c.protocol)
switch (c.version) {
case 'V11':
return deepMerge(deepClone(this.app.config.general.V11), c)
Expand All @@ -39,13 +41,13 @@ export class OneBot<V extends OneBot.Version> extends EventEmitter {
throw new Error('不支持的oneBot版本:' + c.version)
}
})
this.client = new Client({platform,sign_api_addr, data_dir: join(App.configDir, 'data')})
this.client = new Client(protocolConfig)
this.instances = this.config.map(c => {
switch (c.version) {
case 'V11':
return new V11(this, this.client, <V11.Config>omit(c, ['version', 'password']))
return new V11(this, this.client, <V11.Config>c)
case 'V12':
return new V12(this, this.client, <V12.Config>omit(c, ['version', 'password']))
return new V12(this, this.client, <V12.Config>c)
default:
throw new Error('不支持的oneBot版本:' + c.version)
}
Expand Down Expand Up @@ -183,8 +185,7 @@ export namespace OneBot {
export type Version = 'V11' | 'V12'
export type Config<V extends Version = 'V11'> = ({
version?: V
platform?:Platform
password?: string
protocol?:IcqqConfig
} & (V extends 'V11' ? V11.Config : V12.Config))

export interface Base {
Expand Down
14 changes: 8 additions & 6 deletions src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {copyFileSync, existsSync, mkdirSync, writeFileSync} from "fs";
import {Logger, getLogger} from "log4js";
import {createServer, Server} from "http";
import yaml from 'js-yaml'
import {Config as IcqqConfig} from "icqq";
import KoaBodyParser from "koa-bodyparser";
import {OneBot} from "@/onebot";

Expand Down Expand Up @@ -116,7 +117,7 @@ export class App extends Koa {
ctx.body = this.oneBots.map(bot => {
return {
uin: bot.uin,
config: bot.config.map(c => protectedFields(c, 'password', "access_token")),
config: bot.config.map(c => protectedFields(c, 'protocol', "access_token")),
urls: bot.config.map(c => `/${c.version}/${bot.uin}`)
}
})
Expand All @@ -143,7 +144,7 @@ export class App extends Koa {
const oneBot = this.oneBots.find(bot => bot.uin === Number(uin))
ctx.body = {
uin,
config: oneBot.config.map(c => protectedFields(c, 'password', "access_token")),
config: oneBot.config.map(c => protectedFields(c, 'protocol', "access_token")),
urls: oneBot.config.map(c => `/${uin}/${c.version}`)
}
})
Expand Down Expand Up @@ -219,20 +220,21 @@ export namespace App {
path?: string
timeout?: number
log_level?: LogLevel
sign_api_addr?:string
platform?: Platform
general?: {
V11?: V11.Config
V12?: V12.Config
protocol?: IcqqConfig
}
} & KoaOptions & Record<`${number}`, MayBeArray<OneBot.Config<OneBot.Version>>>
export const defaultConfig: Config = {
port: 6727,
timeout: 30,
platform: 5,
general: {
V11: V11.defaultConfig,
V12: V12.defaultConfig
V12: V12.defaultConfig,
protocol:{
platform: 2
}
},
log_level: 'info',
}
Expand Down
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,15 @@ export function uuid() {
}

export function protectedFields<T>(source: T, ...keys: (keyof T)[]): T {
const protocolValue=(value)=>{
if (value && typeof value === 'object') return Object.fromEntries(Object.entries(value).map(([key, value]) => {
return [key, protocolValue(value)]
}))
return `${value}`.split('').map(()=>'*').join('')
}
if (!source || typeof source !== 'object') throw new Error('source must is object')
return Object.fromEntries(Object.entries(source).map(([key, value]) => {
return [key, keys.includes(key as keyof T) ? value.split('').map(c => '*').join('') : value]
return [key, keys.includes(key as keyof T) ? protocolValue(value) : value]
})) as T
}

Expand Down

0 comments on commit 852b990

Please sign in to comment.