Skip to content

Commit

Permalink
🐛 修复端口通配的问题 #30
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 20, 2022
1 parent 1802c1c commit f5183bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/apps/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const Server = process.env.NODE_ENV == 'production' ? 'https://sc.icodef.
export const ExternalWhitelist = [
'greasyfork.org',
'scriptcat.org',
'tampermonkey.net.cn',
'openuserjs.org',
];
8 changes: 4 additions & 4 deletions src/pkg/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Match<T> {
};
}
if (url == 'http*') {
return { scheme: '*', host: '*', path: '', search: '' };
return { scheme: '*', host: '*', path: '*', search: '*' };
}
match = /^(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
if (match) {
Expand All @@ -41,10 +41,10 @@ export class Match<T> {
u.scheme = 'http[s]';
break;
}
u.host = u.host.replace(/\*/g, '.*?');
u.host = u.host.replace(/\*/g, '[^/]*?');
// 处理 *.开头
if (u.host.startsWith('.*?.')) {
u.host = '(.*?\.?)' + u.host.substr(4);
if (u.host.startsWith('[^/]*?.')) {
u.host = '([^/]*?\.?)' + u.host.substr(7);
}
// 处理顶域
if (u.host.endsWith('tld')) {
Expand Down
24 changes: 23 additions & 1 deletion tests/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ describe('UrlMatch2', () => {
const url = new UrlMatch<string>();
it('http*', () => {
url.add('http*', 'ok1');
expect(url.match('http://www.http*.com')).toEqual(['ok1']);
expect(url.match('http://www.http.com')).toEqual(['ok1']);
});
it('port', () => {
url.add('http://domain:8080', 'ok2');
expect(url.match('http://domain:8080/')).toEqual(['ok1', 'ok2']);
expect(url.match('http://domain:8080/123')).toEqual(['ok1']);
});
it('无/', () => {
url.add('http://domain2', 'ok3');
url.add('http://domain2*', 'ok4');
expect(url.match('http://domain2/')).toEqual(['ok1', 'ok3', 'ok4']);
expect(url.match('http://domain2.com/')).toEqual(['ok1', 'ok4']);
expect(url.match('http://domain2/123')).toEqual(['ok1']);
})
});

describe('UrlMatch3', () => {
const url = new UrlMatch<string>();
it('port', () => {
url.add('http://domain:*/', 'ok1');
expect(url.match('http://domain:8080/')).toEqual(['ok1']);
expect(url.match('http://domain:8080')).toEqual(['ok1']);
expect(url.match('http://domain:8080/123')).toEqual([]);
});
});

0 comments on commit f5183bd

Please sign in to comment.