search now requires authentication

This commit is contained in:
Yanlong Wang 2024-11-01 14:12:54 +08:00
parent 5d865651b1
commit bd629a836b
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37

View File

@ -93,62 +93,51 @@ export class SearcherHost extends RPCHost {
const noSlashPath = decodeURIComponent(ctx.req.path).slice(1); const noSlashPath = decodeURIComponent(ctx.req.path).slice(1);
if (!noSlashPath && !q) { if (!noSlashPath && !q) {
const latestUser = uid ? await auth.assertUser() : undefined; const latestUser = uid ? await auth.assertUser() : undefined;
const index = this.crawler.getIndex(latestUser);
if (!uid) {
index.note = 'Authentication is required to use this endpoint. Please provide a valid API key via Authorization header.';
}
if (!ctx.req.accepts('text/plain') && (ctx.req.accepts('text/json') || ctx.req.accepts('application/json'))) { if (!ctx.req.accepts('text/plain') && (ctx.req.accepts('text/json') || ctx.req.accepts('application/json'))) {
return this.crawler.getIndex(latestUser); return index;
} }
return assignTransferProtocolMeta(`${this.crawler.getIndex(latestUser)}`, return assignTransferProtocolMeta(`${index}`,
{ contentType: 'text/plain', envelope: null } { contentType: 'text/plain', envelope: null }
); );
} }
if (uid) { const user = await auth.assertUser();
const user = await auth.assertUser(); if (!(user.wallet.total_balance > 0)) {
if (!(user.wallet.total_balance > 0)) { throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
}
const rateLimitPolicy = auth.getRateLimits(rpcReflect.name.toUpperCase()) || [
parseInt(user.metadata?.speed_level) >= 2 ?
RateLimitDesc.from({
occurrence: 100,
periodSeconds: 60
}) :
RateLimitDesc.from({
occurrence: 40,
periodSeconds: 60
})
];
const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(
rpcReflect, uid, [rpcReflect.name.toUpperCase()],
...rateLimitPolicy
);
rpcReflect.finally(() => {
if (chargeAmount) {
auth.reportUsage(chargeAmount, `reader-${rpcReflect.name}`).catch((err) => {
this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
});
apiRoll.chargeAmount = chargeAmount;
}
});
} else if (ctx.req.ip) {
this.threadLocal.set('ip', ctx.req.ip);
const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, [rpcReflect.name.toUpperCase()],
[
// 5 requests per minute
new Date(Date.now() - 60 * 1000), 5
]
);
rpcReflect.finally(() => {
if (chargeAmount) {
apiRoll.chargeAmount = chargeAmount;
}
});
} }
const rateLimitPolicy = auth.getRateLimits(rpcReflect.name.toUpperCase()) || [
parseInt(user.metadata?.speed_level) >= 2 ?
RateLimitDesc.from({
occurrence: 100,
periodSeconds: 60
}) :
RateLimitDesc.from({
occurrence: 40,
periodSeconds: 60
})
];
const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(
rpcReflect, uid!, [rpcReflect.name.toUpperCase()],
...rateLimitPolicy
);
rpcReflect.finally(() => {
if (chargeAmount) {
auth.reportUsage(chargeAmount, `reader-${rpcReflect.name}`).catch((err) => {
this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
});
apiRoll.chargeAmount = chargeAmount;
}
});
delete crawlerOptions.html; delete crawlerOptions.html;
const crawlOpts = this.crawler.configure(crawlerOptions); const crawlOpts = this.crawler.configure(crawlerOptions);