Skip to content

Commit

Permalink
perf (core): DomainResolveProcessor now cache results
Browse files Browse the repository at this point in the history
  • Loading branch information
Peng-YM committed Jul 19, 2022
1 parent 8f5d027 commit 77604a3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
6 changes: 3 additions & 3 deletions backend/dist/cron-sync-artifacts.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions backend/dist/sub-store-parser.loon.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.11.5",
"version": "2.12.0",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
29 changes: 23 additions & 6 deletions backend/src/core/proxy-utils/processors/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import resourceCache from '@/utils/resource-cache';
import { isIPv4, isIPv6 } from '@/utils';
import { FULL } from '@/utils/logical';
import { getFlag } from '@/utils/geo';
import lodash from 'lodash';
import $ from '@/core/app';
import { hex_md5 } from '@/vendor/md5';

/**
The rule "(name CONTAINS "🇨🇳") AND (port IN [80, 443])" can be expressed as follows:
{
The rule "(name CONTAINS "🇨🇳") AND (port IN [80, 443])" can be expressed as follows:
{
operator: "AND",
child: [
{
Expand All @@ -21,7 +23,7 @@ The rule "(name CONTAINS "🇨🇳") AND (port IN [80, 443])" can be expressed a
}
]
}
*/
*/

function ConditionalFilter({ rule }) {
return {
Expand Down Expand Up @@ -310,6 +312,9 @@ function ScriptOperator(script, targetPlatform, $arguments) {

const DOMAIN_RESOLVERS = {
Google: async function (domain) {
const id = hex_md5(`GOOGLE:${domain}`);
const cached = resourceCache.get(id);
if (cached) return cached;
const resp = await $.http.get({
url: `https://8.8.4.4/resolve?name=${encodeURIComponent(
domain,
Expand All @@ -326,9 +331,14 @@ const DOMAIN_RESOLVERS = {
if (answers.length === 0) {
throw new Error('No answers');
}
return answers[answers.length - 1].data;
const result = answers[answers.length - 1].data;
resourceCache.set(id, result);
return result;
},
'IP-API': async function (domain) {
const id = hex_md5(`IP-API:${domain}`);
const cached = resourceCache.get(id);
if (cached) return cached;
const resp = await $.http.get({
url: `http://ip-api.com/json/${encodeURIComponent(
domain,
Expand All @@ -338,9 +348,14 @@ const DOMAIN_RESOLVERS = {
if (body['status'] !== 'success') {
throw new Error(`Status is ${body['status']}`);
}
return body.query;
const result = body.query;
resourceCache.set(id, result);
return result;
},
Cloudflare: async function (domain) {
const id = hex_md5(`CLOUDFLARE:${domain}`);
const cached = resourceCache.get(id);
if (cached) return cached;
const resp = await $.http.get({
url: `https://1.0.0.1/dns-query?name=${encodeURIComponent(
domain,
Expand All @@ -357,7 +372,9 @@ const DOMAIN_RESOLVERS = {
if (answers.length === 0) {
throw new Error('No answers');
}
return answers[answers.length - 1].data;
const result = answers[answers.length - 1].data;
resourceCache.set(id, result);
return result;
},
};

Expand Down
6 changes: 3 additions & 3 deletions backend/sub-store.min.js

Large diffs are not rendered by default.

0 comments on commit 77604a3

Please sign in to comment.