-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
521 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,73 @@ | ||
import { browser } from '$app/environment'; | ||
import { env } from '$env/dynamic/public'; | ||
import type { Reroute } from '@sveltejs/kit'; | ||
|
||
const subdomainRegex = new RegExp( | ||
`(.*)\.${env.PUBLIC_DOMAIN.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}` | ||
); | ||
|
||
// // Install hack to make global `fetch()` work behind an http proxy during development | ||
// import { Agent, ProxyAgent, Dispatcher, setGlobalDispatcher } from 'undici'; | ||
// // If we are in development mode and there is an `http_proxy` set | ||
// if (process.env.NODE_ENV == 'development' && process.env.http_proxy && !browser) { | ||
// // Collect the list of domains that we should not use a proxy for | ||
// const noProxyList = (process.env.no_proxy && process.env.no_proxy.split(',')) || []; | ||
// // Parse the proxy URL | ||
// const proxyUrl = new URL(process.env.http_proxy); | ||
// // Create an access token if the proxy requires authentication | ||
// const token = proxyUrl.username && proxyUrl.password ? | ||
// `Basic ${Buffer.from(`${proxyUrl.username}:${proxyUrl.password}`).toString('base64')}` : undefined; | ||
|
||
// // Create a default agent that will be used for no_proxy origins | ||
// const defaultAgent = new Agent(); | ||
|
||
// // Create an interceptor that will use the appropriate agent based on the origin and the no_proxy | ||
// // environment variable. | ||
// const noProxyInterceptor = (dispatch: Dispatcher['dispatch']): Dispatcher['dispatch'] => { | ||
// return (opts, handler) => { | ||
// let noProxy = false; | ||
// for (const exclusion of noProxyList) { | ||
// if (opts.origin?.toString().search(exclusion) != -1) { | ||
// noProxy = true; | ||
// break; | ||
// } | ||
// } | ||
// return noProxy ? | ||
// defaultAgent.dispatch(opts, handler) : | ||
// dispatch(opts, handler); | ||
// } | ||
// }; | ||
|
||
// // Create a proxy agent that will send all requests through the configured proxy, unless the | ||
// // noProxyInterceptor bypasses it. | ||
// const proxyAgent = new ProxyAgent({ | ||
// uri: proxyUrl.protocol + proxyUrl.host, | ||
// token, | ||
// interceptors: { | ||
// Client: [noProxyInterceptor] | ||
// } | ||
// }); | ||
|
||
// // Make sure our configured proxy agent is used for all `fetch()` requests globally. | ||
// setGlobalDispatcher(proxyAgent); | ||
// } | ||
|
||
export const reroute: Reroute = ({ url }) => { | ||
if (url.host === env.PUBLIC_DOMAIN) { | ||
if (url.host == env.PUBLIC_DOMAIN || url.pathname.startsWith('/dns-challenge')) { | ||
return url.pathname; | ||
} | ||
|
||
if (url.host == env.PUBLIC_TRAEFIK_CONFIG_HOST) { | ||
return '/traefik-config'; | ||
} | ||
|
||
let username = url.host.match(subdomainRegex)?.[1]; | ||
if (!username) { | ||
throw 'Invalid domain'; | ||
} | ||
|
||
if (url.pathname == '/' || url.pathname == '') { | ||
return `/subsite/${username}`; | ||
} else if (url.pathname.startsWith(`/u/${username}`)) { | ||
let usernameSubdomain = url.host.match(subdomainRegex)?.[1]; | ||
const subsite = usernameSubdomain ? usernameSubdomain : url.host; | ||
return `/subsite/${subsite}`; | ||
} else if (url.pathname.startsWith(`/u/`)) { | ||
return url.pathname; | ||
} | ||
|
||
throw 'Invalid domain'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Keyv from 'keyv'; | ||
|
||
const store = new Keyv({ namespace: 'dns-challenge' }); | ||
|
||
export const createChallenge = async (userId: string): Promise<string> => { | ||
const challengeId = crypto.randomUUID(); | ||
await store.set(challengeId, userId, 30 * 60 * 1000); | ||
return challengeId; | ||
}; | ||
|
||
export const validateChallenge = async (challengeId: string, userId: string): Promise<boolean> => { | ||
return (await store.get(challengeId)) == userId; | ||
}; |
Oops, something went wrong.