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

Commit

Permalink
feat: get token from remote server
Browse files Browse the repository at this point in the history
  • Loading branch information
piquark6046 committed Jul 12, 2024
1 parent 6985a8f commit dc32a9b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "microshield",
"version": "4.5.23",
"version": "4.6.0",
"description": "",
"type": "module",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions sources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// @downloadURL https://cdn.jsdelivr.net/gh/List-KR/microShield@latest/microShield.user.js
// @license Apache-2.0
//
// @version 4.5.23
// @version 4.6.0
// @author PiQuark6046 and contributors
//
// @match *://ygosu.com/*
Expand Down Expand Up @@ -175,7 +175,7 @@
// @description microShield allows AdGuard, uBlock Origin, Brave and ABP to resist against Ad-Shield widely.
// @description:ko microShield는 AdGuard, uBlock Origin, Brave 와 ABP가 애드쉴드에 널리 저항할 수 있도록 합니다.
//
// @grant none
// @grant GM.xmlHttpRequest
// @run-at document-start
// ==/UserScript==
// Original Srouce Code:
Expand Down
16 changes: 16 additions & 0 deletions sources/src/GM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,20 @@ export type GM = {
version: string
}
}
xmlHttpRequest({
method,
url,
headers,
responseType,
data
}: {
method?: string,
url: string,
headers?: Record<string, string>,
responseType?: 'text' | 'json',
data?: string
}): Promise<{
status: number
response: string
}>
}
32 changes: 31 additions & 1 deletion sources/src/adshield/resources.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {UnprotectedFetch} from '../utils/secret.js'
import {GetRandomAdShieldHost} from './validators.js'
import type {GM} from '../GM.js'

declare const GM: GM

export const GetCachableHtml = async (Url: string) => {
const ResponseRaw = await UnprotectedFetch(Url, {
Expand All @@ -23,12 +26,39 @@ export const GetResourceToken = async (ScriptUrl: string) => {
const Match = /eyJ[\w-]*\.eyJ[\w-]*\.[\w-]*/.exec(Text)

if (Match === null) {
throw new Error('Failed to match predefined token in the script!')
const ResponseHash = Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode('asdf')))).map(Block =>Block.toString(16).padStart(2, '0')).join('')
try {
return await GetResourceTokenFromCDN(ResponseHash)
}
catch {
return await GetResourceTokenFromRemote(Text)
}
}

return Match[0]
}

const GetResourceTokenFromRemote = async (Body: string) => {
const XHR = await GM.xmlHttpRequest({
url: 'https://microshield.piquark6046.dev/token',
data: Body
})
if (XHR.status !== 200) {
throw new Error('Failed to fetch token!')
}
return XHR.response
}

const GetResourceTokenFromCDN = async (Hash: string) => {
const XHR = await GM.xmlHttpRequest({
url: `https://cdn.jsdelivr.net/gh/List-KR/microShield-token@main/${Hash}`
})
if (XHR.status !== 200) {
throw new Error('Failed to fetch token!')
}
return XHR.response
}

export const ResolveResourceUrls = async (Html: string, Token: string) => {
const Pattern = /(resources:\/\/[^'"]+)/g
const Host = GetRandomAdShieldHost()
Expand Down

0 comments on commit dc32a9b

Please sign in to comment.