Skip to content

Commit

Permalink
better wildcard handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna-devv committed Oct 19, 2022
1 parent 45aebc7 commit 82137ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default {
"lunish.nl": {
target: 5001,
type: "WEB",
arc: true
arc: true,
},
"www.lunish.nl": {
target: 5001,
Expand All @@ -40,7 +40,7 @@ export default {
path: '/*',
type: "REDIRECT",
target: 'https://cdn.waya.one/{path}'
}
},
]
},

Expand All @@ -63,9 +63,24 @@ export default {
ip: '127.0.9.1',
overwrites: [
{
path: ['/sex'],
path: '/_/*',
type: "REDIRECT",
target: 'https://google.com/{total_path}'
},
{
path: '/search/*',
type: "REDIRECT",
target: 'https://google.com/search?q={after_path}'
},
{
path: '/search-w-query/*',
type: "REDIRECT",
target: 'https://google.com/search?q={after_path}{query}'
},
{
path: ['/alphabet', '/abc', 'xyz'],
type: "REDIRECT",
target: 'https://google.com/{path}'
target: 'https://abc.xyz'
}
]
}
Expand Down
12 changes: 10 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ export async function requestManager(req, res) {
// managing overwrites
if (overwrites) {
for (const overwrite of overwrites) {
if ((typeof overwrite.path == 'string' ? (req.url.includes('?') ? req.url.split('?')[0] : req.url) == overwrite.path : overwrite.path.includes(req.url)) || overwrite.path === '/*') {

if (
(typeof overwrite.path === 'string' ? (req.url.includes('?') ? req.url.split('?')[0] : req.url) == overwrite.path : overwrite.path.includes(req.url.includes('?') ? req.url.split('?')[0] : req.url))
|| (typeof overwrite.path === 'string' && overwrite.path.endsWith('/*') && (req.url.includes('?') ? req.url.split('?')[0] : req.url).startsWith(overwrite.path.slice(0, -1)))
|| overwrite.path === '/*'
) {

// Proxy overwrite
switch (overwrite.type) {
Expand All @@ -59,7 +64,10 @@ export async function requestManager(req, res) {
// Redirections
if (typeof overwrite.target === 'number') return onError('Redirect target cannot be number', req, res);
res.writeHead(302, {
'Location': overwrite.target.replace(/{path}/g, req.url.slice(1))
'Location': overwrite.target
.replace(/{after_path}/g, (req.url.includes('?') ? req.url.split('?')[0] : req.url).split(overwrite.path.slice(0, -1))[1])
.replace(/{total_path}/g, (req.url.includes('?') ? req.url.split('?')[0] : req.url).slice(1))
.replace(/{query}/g, req.url.split('?')[1] ? `?${req.url.split('?')[1]}` : '')
});
res.end();
break;
Expand Down

0 comments on commit 82137ee

Please sign in to comment.