Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
feat: add microshield-token-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
piquark6046 committed Aug 19, 2024
1 parent 8e1080c commit 44ede23
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"license": "Apache-2.0",
"dependencies": {
"@list-kr/microshield-token-parser": "^1.0.2",
"@types/node": "^20.12.12",
"crypto-random-string": "^5.0.0",
"parse-domain": "^8.0.2"
Expand Down
4 changes: 4 additions & 0 deletions sources/banner-ios-recovery.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@
// @description:ja microShieldはAdGuard、uBlock Origin、Brave、ABPの環境でAd-Shieldを広範囲に回避します。
//
// @grant GM.xmlHttpRequest
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.listValues
// @grant GM.deleteValue
// @run-at document-end
// ==/UserScript==
// Original Srouce Code:
Expand Down
4 changes: 4 additions & 0 deletions sources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@
// @description:ja microShieldはAdGuard、uBlock Origin、Brave、ABPの環境でAd-Shieldを広範囲に回避します。
//
// @grant GM.xmlHttpRequest
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.listValues
// @grant GM.deleteValue
// @run-at document-start
// ==/UserScript==
// Original Srouce Code:
Expand Down
2 changes: 1 addition & 1 deletion sources/src/GM.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
export type GM = {
getValue(key: string, defaultValue: undefined): Promise<string | undefined>
getValue<T>(key: string, defaultValue: T): Promise<string | T>
setValue(key: string, value: string): Promise<void>
deleteValue(key: string): Promise<void>
listValues(): Promise<string[]>
Expand Down
14 changes: 13 additions & 1 deletion sources/src/adshield/resources.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {UnprotectedFetch} from '../utils/secret.js'
import {GetRandomAdShieldHost} from './validators.js'
import type {GM} from '../GM.js'
import { AdvancedExtractor } from '@list-kr/microshield-token-parser'

declare const GM: GM

Expand All @@ -27,7 +28,18 @@ export const GetResourceToken = async (ScriptUrl: string) => {

if (Match === null) {
const ResponseHash = Array.from(new Uint8Array(await crypto.subtle.digest('SHA-1', new TextEncoder().encode(Text)))).map(Block =>Block.toString(16).padStart(2, '0')).join('')
return await GetResourceTokenFromCDN(ResponseHash)
try {
return await GetResourceTokenFromCDN(ResponseHash)
}
catch {
if (await GM.getValue(ResponseHash, null) === null) {
const Token = new AdvancedExtractor(Text).GetToken()
await GM.setValue(ResponseHash, Token)
return Token
} else {
return await GM.getValue(ResponseHash, null)
}
}
}

return Match[0]
Expand Down
3 changes: 2 additions & 1 deletion sources/src/index-ios-recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {CreateDebug} from './utils/logger.js'
import {ProtectFunctionDescriptors} from './utils/secret.js'
import {ProtectStorageApis} from './utils/storage.js'
import {HasSubstringSetsInString} from './utils/string.js'
import { CheckVersion } from './utils/ucache.js'

type unsafeWindow = typeof window
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -82,5 +83,5 @@ const Bootstrap = () => {
// Observe()
// })
}

CheckVersion()
Bootstrap()
2 changes: 2 additions & 0 deletions sources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {CreateDebug} from './utils/logger.js'
import {ProtectFunctionDescriptors} from './utils/secret.js'
import {ProtectStorageApis} from './utils/storage.js'
import {HasSubstringSetsInString} from './utils/string.js'
import { CheckVersion } from './utils/ucache.js'

type unsafeWindow = typeof window
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -83,4 +84,5 @@ const Bootstrap = () => {
})
}

CheckVersion()
Bootstrap()
14 changes: 14 additions & 0 deletions sources/src/utils/ucache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type {GM} from '../GM.js'

declare const GM: GM

export async function CheckVersion() {
if (await GM.getValue('version', null) === null) {
await GM.setValue('version', GM.info.script.version)
} else if (await GM.getValue('version', null) !== GM.info.script.version) {
for (const Key of await GM.listValues()) {
await GM.deleteValue(Key)
}
await GM.setValue('version', GM.info.script.version)
}
}

0 comments on commit 44ede23

Please sign in to comment.