Skip to content

Commit

Permalink
Change domain match rule to suffix match
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyufjh committed Jan 13, 2025
1 parent ed89143 commit 0a7a8c5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ export default function Settings() {

<TabsContent value="blacklist">
<Textarea
placeholder="输入黑名单域名,每行一个,支持通配符 (例如 *.example.com)"
placeholder="输入黑名单域名,每行一个,检测后缀匹配 (例如 example.com)"
value={domainList}
onChange={(e) => setDomainListAndPersist(e.target.value)}
rows={4}
/>
</TabsContent>
<TabsContent value="whitelist">
<Textarea
placeholder="输入白名单域名,每行一个,支持通配符 (例如 *.example.com)"
placeholder="输入白名单域名,每行一个,检测后缀匹配 (例如 example.com)"
value={domainList}
onChange={(e) => setDomainList(e.target.value)}
rows={4}
Expand Down
30 changes: 8 additions & 22 deletions src/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,19 @@ import { describe, it, expect } from 'vitest'
import { compareSemver, matchDomain } from './utils'

describe('match function', () => {
it('matches exact domains', () => {
it('returns true if domain matches pattern exactly', () => {
expect(matchDomain('www.google.com', 'www.google.com')).toBe(true)
})

it('matches wildcard subdomains', () => {
expect(matchDomain('www.google.com', '*.google.com')).toBe(true)
it('returns true if domain ends with pattern', () => {
expect(matchDomain('www.google.com', 'google.com')).toBe(true)
expect(matchDomain('www.google.com', 'com')).toBe(true)
})

it('does not match when subdomain is missing', () => {
expect(matchDomain('www.google.com', 'google.com')).toBe(false)
})

it('matches multiple wildcards', () => {
expect(matchDomain('www.google.com', '*.google.*')).toBe(true)
expect(matchDomain('www.google.com', 'www.*.com')).toBe(true)
})

it('does not match different TLDs', () => {
expect(matchDomain('www.google.com', 'google.us')).toBe(false)
})

it('matches wildcard TLD', () => {
expect(matchDomain('www.google.com', '*.com')).toBe(true)
})

it('matches universal wildcard', () => {
expect(matchDomain('www.google.com', '*')).toBe(true)
it('returns false if domain does not match pattern', () => {
expect(matchDomain('www.google.com', 'ogle.com')).toBe(false)
expect(matchDomain('www.google.com', 'www.google')).toBe(false)
expect(matchDomain('google.com', 'www.google.com')).toBe(false)
})
})

Expand Down
12 changes: 2 additions & 10 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

// Match domain names with wildcard (`*`)
// Test if domain matches pattern.
export function matchDomain(domain: string, pattern: string): boolean {
// Convert patterns to regex-safe strings and replace * with regex pattern
const escapeRegex = (str: string) => str.replaceAll(/[.*+?^${}()|[\]\\]/g, '\\$&').replaceAll('\\*', '.*')

// Create regex pattern from the wildcard pattern
const regexPattern = `^${escapeRegex(pattern)}$`

// Create RegExp object and test the domain
const regex = new RegExp(regexPattern)
return regex.test(domain)
return domain === pattern || domain.endsWith('.' + pattern);
}

// Compare semver version. Return true if v1 is greater than v2.
Expand Down

0 comments on commit 0a7a8c5

Please sign in to comment.