core: fix onCache

This commit is contained in:
Micooz 2018-02-15 11:45:48 +08:00
parent 4b7c2e091c
commit 1fcf4e2f6c
4 changed files with 12 additions and 12 deletions

@ -137,11 +137,11 @@ export class Config {
for (let i = 0; i < server.presets.length; i++) {
const {name, params = {}} = server.presets[i];
const clazz = getPresetClassByName(name);
const data = clazz.onCache(params);
const data = clazz.onCache(params, this.stores[i]);
if (data instanceof Promise) {
data.then((d) => this.stores[i] = d);
} else {
this.stores[i] = clazz.onCache(params);
} else if (typeof data !== 'undefined') {
this.stores[i] = data;
}
}
}

@ -31,13 +31,13 @@ export class Pipe extends EventEmitter {
return this._presets;
}
constructor({presets, isUdp = false}, config) {
constructor({config, presets, isUdp = false}) {
super();
this._config = config;
this.broadcast = this.broadcast.bind(this);
this.onReadProperty = this.onReadProperty.bind(this);
this.createMiddlewares(presets);
this._config = config;
this._isPipingUdp = isUdp;
this.createMiddlewares(presets);
}
broadcast(name, action) {

@ -272,7 +272,7 @@ export class Relay extends EventEmitter {
* create pipes for both data forward and backward
*/
createPipe(presets) {
const pipe = new Pipe({presets, isUdp: this._transport === 'udp'}, this._config);
const pipe = new Pipe({config: this._config, presets, isUdp: this._transport === 'udp'});
pipe.on('broadcast', this.onBroadcast.bind(this)); // if no action were caught by presets
pipe.on(`post_${PIPE_ENCODE}`, this.onEncoded);
pipe.on(`post_${PIPE_DECODE}`, this.onDecoded);

@ -197,12 +197,13 @@ export default class V2rayVmessPreset extends IPresetAddressing {
}
}
static onCache(_, store) {
setInterval(() => V2rayVmessPreset.updateAuthCache(store), 1e3);
V2rayVmessPreset.updateAuthCache(store);
static onCache({id}, store) {
const uuid = Buffer.from(id.split('-').join(''), 'hex');
setInterval(() => V2rayVmessPreset.updateAuthCache(uuid, store), 1e3);
V2rayVmessPreset.updateAuthCache(uuid, store);
}
static updateAuthCache(store) {
static updateAuthCache(uuid, store) {
const items = store.userHashCache || [
// {timestamp, authInfo},
// ...
@ -218,7 +219,6 @@ export default class V2rayVmessPreset extends IPresetAddressing {
}
for (let ts = from; ts <= to; ++ts) {
// account auth info, 16 bytes
const uuid = this._uuid;
const authInfo = hmac('md5', uuid, ntb(ts, 8));
newItems.push({timestamp: ts, authInfo: authInfo});
}