Browse Source

Improve the method of checking the latest version.

oldj 4 years ago
parent
commit
a65d9d8a48
5 changed files with 26 additions and 14 deletions
  1. 1 1
      src/main/actions/checkUpdate.ts
  2. 11 7
      src/main/libs/cron.ts
  3. 4 4
      src/main/libs/request.ts
  4. 8 2
      src/main/main.ts
  5. 2 0
      src/main/types.d.ts

+ 1 - 1
src/main/actions/checkUpdate.ts

@@ -14,7 +14,7 @@ export default async (): Promise<boolean | null> => {
   // Check the latest version, also used for anonymous statistics of DAU,
   // no personal information will be sent.
 
-  let r = await GET(`${server_url}/api/check/`)
+  let r = await GET(`${server_url}/api/check/`, { sid: global.session_id })
   if (r.status !== 200 || !r.data?.success) {
     return null
   }

+ 11 - 7
src/main/libs/cron.ts

@@ -48,20 +48,24 @@ const checkRefresh = async () => {
   broadcast('reload_list')
 }
 
-const check = async () => {
-  checkRefresh()
-    .catch(e => console.error(e))
-
+const checkServer = async () => {
   let ts = (new Date()).getTime()
   if (!ts_last_server_check || (ts - ts_last_server_check) > 3600 * 1000) {
-    checkUpdate()
-      .catch(e => console.error(e))
+    await checkUpdate()
     ts_last_server_check = ts
   }
 }
 
+const check = async () => {
+  checkRefresh()
+    .catch(e => console.error(e))
+
+  checkServer()
+    .catch(e => console.error(e))
+}
+
 export const start = () => {
-  setTimeout(checkUpdate, 5000)
+  setTimeout(checkServer, 5000)
 
   clearInterval(t)
   t = setInterval(check, 60 * 1000)

+ 4 - 4
src/main/libs/request.ts

@@ -9,10 +9,6 @@ import axios, { AxiosRequestConfig } from 'axios'
 import querystring from 'querystring'
 import version from '@root/version.json'
 
-const default_headers = {
-  'user-agent': `Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 SwitchHosts/${version.join('.')}`,
-}
-
 interface IParams {
   [key: string]: string | string[] | number;
 }
@@ -31,6 +27,10 @@ export const GET = async (url: string, params: IParams | null = null, options: I
     url += (url.includes('?') ? '&' : '?') + s
   }
 
+  const default_headers = {
+    'user-agent': `${global.ua} SwitchHosts/${version.join('.')}`,
+  }
+
   let configs: AxiosRequestConfig = {
     timeout: options.timeout || 30000,
     headers: {

+ 8 - 2
src/main/main.ts

@@ -6,12 +6,13 @@ import '@main/data'
 import * as cron from '@main/libs/cron'
 import getIndex from '@main/libs/getIndex'
 import isDev from '@main/libs/isDev'
-import '@main/tray'
 import { makeMainMenu } from '@main/libs/menu'
+import '@main/tray'
 import version from '@root/version.json'
 import { app, BrowserWindow } from 'electron'
-import * as path from 'path'
 import windowStateKeeper from 'electron-window-state'
+import * as path from 'path'
+import { v4 as uuid4 } from 'uuid'
 
 let win: BrowserWindow | null
 let is_will_quit: boolean = false
@@ -42,6 +43,10 @@ const createWindow = async () => {
 
   main_window_state.manage(win)
 
+  const ses = win.webContents.session
+  // console.log(ses.getUserAgent())
+  global.ua = ses.getUserAgent()
+
   if (configs.hide_at_launch) {
     win.hide()
   }
@@ -107,6 +112,7 @@ const onActive = async () => {
 
 app.on('ready', async () => {
   console.log(`VERSION: ${version.join('.')}`)
+  global.session_id = uuid4()
   await createWindow()
   cron.start()
 })

+ 2 - 0
src/main/types.d.ts

@@ -25,6 +25,8 @@ declare global {
       db_dir?: string;
       swhdb: SwhDb;
       cfgdb: SwhDb;
+      ua: string; // user agent
+      session_id: string; // A random value, refreshed every time the app starts, used to identify different startup sessions.
     }
   }
 }