This commit is contained in:
fflorent 2024-03-25 16:01:48 +01:00
parent e251c2087b
commit 7e0e21613e
5 changed files with 28 additions and 14 deletions

View File

@ -185,12 +185,20 @@ export interface OrgUrlInfo {
orgInPath?: string; // If /o/{orgInPath} should be used to access the requested org. orgInPath?: string; // If /o/{orgInPath} should be used to access the requested org.
} }
function isDocInternalUrl(host: string) { function isInternalUrl(host: string, envValue?: string) {
if (!process.env.APP_DOC_INTERNAL_URL) { return false; } if (!envValue) { return false; }
const internalUrl = new URL('/', process.env.APP_DOC_INTERNAL_URL); const internalUrl = new URL('/', envValue);
return internalUrl.host === host; return internalUrl.host === host;
} }
function isDocInternalUrl(host: string) {
return isInternalUrl(host, process.env.APP_DOC_INTERNAL_URL);
}
function isHomeInternalUrl(host: string) {
return isInternalUrl(host, process.env.APP_HOME_INTERNAL_URL);
}
/** /**
* Given host (optionally with port), baseDomain, and pluginUrl, determine whether to interpret host * Given host (optionally with port), baseDomain, and pluginUrl, determine whether to interpret host
* as a custom domain, a native domain, or a plugin domain. * as a custom domain, a native domain, or a plugin domain.
@ -208,7 +216,12 @@ export function getHostType(host: string, options: {
const hostname = host.split(":")[0]; const hostname = host.split(":")[0];
if (!options.baseDomain) { return 'native'; } if (!options.baseDomain) { return 'native'; }
if (hostname === 'localhost' || isDocInternalUrl(host) || hostname.endsWith(options.baseDomain)) { if (
hostname === 'localhost' ||
isDocInternalUrl(host) ||
hostname.endsWith(options.baseDomain) ||
isHomeInternalUrl(host)
) {
return 'native'; return 'native';
} }
return 'custom'; return 'custom';

View File

@ -77,7 +77,7 @@ const _homeUrlReachableProbe: Probe = {
id: 'reachable', id: 'reachable',
name: 'Grist is reachable', name: 'Grist is reachable',
apply: async (server, req) => { apply: async (server, req) => {
const url = server.getHomeUrl(req); const url = server.getHomeInternalUrl(req);
try { try {
const resp = await fetch(url); const resp = await fetch(url);
if (resp.status !== 200) { if (resp.status !== 200) {
@ -102,7 +102,7 @@ const _statusCheckProbe: Probe = {
id: 'health-check', id: 'health-check',
name: 'Built-in Health check', name: 'Built-in Health check',
apply: async (server, req) => { apply: async (server, req) => {
const baseUrl = server.getHomeUrl(req); const baseUrl = server.getHomeInternalUrl(req);
const url = new URL(baseUrl); const url = new URL(baseUrl);
url.pathname = removeTrailingSlash(url.pathname) + '/status'; url.pathname = removeTrailingSlash(url.pathname) + '/status';
try { try {

View File

@ -1098,7 +1098,8 @@ export class DocWorkerApi {
if (req.body.sourceDocId) { if (req.body.sourceDocId) {
options.sourceDocId = await this._confirmDocIdForRead(req, String(req.body.sourceDocId)); options.sourceDocId = await this._confirmDocIdForRead(req, String(req.body.sourceDocId));
// Make sure that if we wanted to download the full source, we would be allowed. // Make sure that if we wanted to download the full source, we would be allowed.
const result = await fetch(this._grist.getHomeUrl(req, `/api/docs/${options.sourceDocId}/download?dryrun=1`), { const homeUrl = this._grist.getHomeInternalUrl(req, `/api/docs/${options.sourceDocId}/download?dryrun=1`);
const result = await fetch(homeUrl, {
method: 'GET', method: 'GET',
headers: { headers: {
...getTransitiveHeaders(req), ...getTransitiveHeaders(req),
@ -1111,7 +1112,7 @@ export class DocWorkerApi {
} }
// We should make sure the source document has flushed recently. // We should make sure the source document has flushed recently.
// It may not be served by the same worker, so work through the api. // It may not be served by the same worker, so work through the api.
await fetch(this._grist.getHomeUrl(req, `/api/docs/${options.sourceDocId}/flush`), { await fetch(this._grist.getHomeInternalUrl(req, `/api/docs/${options.sourceDocId}/flush`), {
method: 'POST', method: 'POST',
headers: { headers: {
...getTransitiveHeaders(req), ...getTransitiveHeaders(req),
@ -1170,7 +1171,7 @@ export class DocWorkerApi {
const showDetails = isAffirmative(req.query.detail); const showDetails = isAffirmative(req.query.detail);
const docSession = docSessionFromRequest(req); const docSession = docSessionFromRequest(req);
const {states} = await this._getStates(docSession, activeDoc); const {states} = await this._getStates(docSession, activeDoc);
const ref = await fetch(this._grist.getHomeUrl(req, `/api/docs/${req.params.docId2}/states`), { const ref = await fetch(this._grist.getHomeInternalUrl(req, `/api/docs/${req.params.docId2}/states`), {
headers: { headers: {
...getTransitiveHeaders(req), ...getTransitiveHeaders(req),
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -1199,7 +1200,7 @@ export class DocWorkerApi {
// Calculate changes from the (common) parent to the current version of the other document. // Calculate changes from the (common) parent to the current version of the other document.
const url = `/api/docs/${req.params.docId2}/compare?left=${parent.h}`; const url = `/api/docs/${req.params.docId2}/compare?left=${parent.h}`;
const rightChangesReq = await fetch(this._grist.getHomeUrl(req, url), { const rightChangesReq = await fetch(this._grist.getHomeInternalUrl(req, url), {
headers: { headers: {
...getTransitiveHeaders(req), ...getTransitiveHeaders(req),
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@ -330,7 +330,7 @@ export class FlexServer implements GristServer {
* based on domain). * based on domain).
*/ */
public async getHomeUrlByDocId(docId: string, relPath: string = ''): Promise<string> { public async getHomeUrlByDocId(docId: string, relPath: string = ''): Promise<string> {
return new URL(relPath, this.getDefaultHomeUrl()).href; return new URL(relPath, this.getDefaultHomeInternalUrl()).href;
} }
// Get the port number the server listens on. This may be different from the port // Get the port number the server listens on. This may be different from the port
@ -1448,7 +1448,7 @@ export class FlexServer implements GristServer {
const permitStore = this.getPermitStore(); const permitStore = this.getPermitStore();
const notifier = this.getNotifier(); const notifier = this.getNotifier();
const loginSystem = await this.resolveLoginSystem(); const loginSystem = await this.resolveLoginSystem();
const homeUrl = this.getHomeUrl(req).replace(/\/$/, ''); const homeUrl = this.getHomeInternalUrl(req).replace(/\/$/, '');
return new Doom(dbManager, permitStore, notifier, loginSystem, homeUrl); return new Doom(dbManager, permitStore, notifier, loginSystem, homeUrl);
}; };
@ -2432,7 +2432,7 @@ export class FlexServer implements GristServer {
const workspace = workspaces.find(w => w.name === 'Home'); const workspace = workspaces.find(w => w.name === 'Home');
if (!workspace) { throw new Error('Home workspace not found'); } if (!workspace) { throw new Error('Home workspace not found'); }
const copyDocUrl = this.getHomeUrl(req, '/api/docs'); const copyDocUrl = this.getHomeInternalUrl(req, '/api/docs');
const response = await fetch(copyDocUrl, { const response = await fetch(copyDocUrl, {
headers: { headers: {
...getTransitiveHeaders(req), ...getTransitiveHeaders(req),

View File

@ -417,7 +417,7 @@ export async function fetchDoc(server: GristServer, docId: string, req: Request,
// Find the doc worker responsible for the document we wish to copy. // Find the doc worker responsible for the document we wish to copy.
// The backend needs to be well configured for this to work. // The backend needs to be well configured for this to work.
const homeUrl = server.getHomeUrl(req); const homeUrl = server.getHomeInternalUrl(req);
const fetchUrl = new URL(`/api/worker/${docId}`, homeUrl); const fetchUrl = new URL(`/api/worker/${docId}`, homeUrl);
const response: FetchResponse = await Deps.fetch(fetchUrl.href, {headers}); const response: FetchResponse = await Deps.fetch(fetchUrl.href, {headers});
await _checkForError(response); await _checkForError(response);