From d84c9bf5a1d19c85fbeff8e1db001ea1c8c310b5 Mon Sep 17 00:00:00 2001 From: wizawu Date: Wed, 10 Feb 2021 22:20:04 +0800 Subject: [PATCH] Fix socks5x --- roles/socks5x/files/src/store.ts | 11 +++++++---- roles/socks5x/files/src/switcher.ts | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/roles/socks5x/files/src/store.ts b/roles/socks5x/files/src/store.ts index d3e7490..bd223c6 100644 --- a/roles/socks5x/files/src/store.ts +++ b/roles/socks5x/files/src/store.ts @@ -65,17 +65,20 @@ export function countConnErr(domain: string) { sum(case when choose = 0 then 1 else 0 end) as error0, sum(case when choose = 1 then 1 else 0 end) as error1 from history - where domain = @domain and duration < 32000 and traffic < 32 and error > '' and time > @lastDay - `).get({ domain, lastDay: Date.now() - 86400 * 1000 }) + where domain = @domain and traffic <= 26 and (error > '' or duration < 1000) + `).get({ domain }) } export function countReadErr(domain: string, prefer: number) { - db.exec(`delete from history where time < ${Date.now() - 30 * 86400 * 1000}`) return db.prepare(` select sum(case when error = '' then 1 else 0 end) as ok, sum(case when error > '' then 1 else 0 end) as fail from history - where domain = @domain and choose = @prefer and traffic > 32 + where domain = @domain and choose = @prefer and traffic > 26 `).get({ domain, prefer }) } + +export function trimHistory(days: number) { + db.exec(`delete from history where time < ${Date.now() - days * 86400 * 1000}`) +} diff --git a/roles/socks5x/files/src/switcher.ts b/roles/socks5x/files/src/switcher.ts index ec2777f..4d1feb3 100644 --- a/roles/socks5x/files/src/switcher.ts +++ b/roles/socks5x/files/src/switcher.ts @@ -6,6 +6,7 @@ export function select(upstream: string): Backend { let result = store.selectDomain(upstream) if (result) { setTimeout(() => { + store.trimHistory(30) let { error0, error1 } = store.countConnErr(upstream) if (error0 > error1) { store.updateDomain(upstream, 1)