mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-26 14:56:39 +00:00
fix traefik config file
This commit is contained in:
@@ -146,12 +146,6 @@ function validatePathRewriteConfig(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rewritePathType === "regex") {
|
|
||||||
// For regex rewrite type, we don't validate the replacement pattern
|
|
||||||
// as it may contain capture groups like $1, $2, etc.
|
|
||||||
// The regex engine will handle validation at runtime
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate path formats for non-regex types
|
// Validate path formats for non-regex types
|
||||||
if (pathMatchType !== "regex" && !path.startsWith("/")) {
|
if (pathMatchType !== "regex" && !path.startsWith("/")) {
|
||||||
return {
|
return {
|
||||||
@@ -184,7 +178,7 @@ function createPathRewriteMiddleware(
|
|||||||
pathMatchType: string,
|
pathMatchType: string,
|
||||||
rewritePath: string,
|
rewritePath: string,
|
||||||
rewritePathType: string
|
rewritePathType: string
|
||||||
): { [key: string]: any } {
|
): { middlewares: { [key: string]: any }; chain?: string[] } {
|
||||||
const middlewares: { [key: string]: any } = {};
|
const middlewares: { [key: string]: any } = {};
|
||||||
|
|
||||||
switch (rewritePathType) {
|
switch (rewritePathType) {
|
||||||
@@ -259,13 +253,12 @@ function createPathRewriteMiddleware(
|
|||||||
|
|
||||||
// If rewritePath is provided and not empty, add it as a prefix after stripping
|
// If rewritePath is provided and not empty, add it as a prefix after stripping
|
||||||
if (rewritePath && rewritePath !== "" && rewritePath !== "/") {
|
if (rewritePath && rewritePath !== "" && rewritePath !== "/") {
|
||||||
const addPrefixMiddlewareName = `${middlewareName.replace('-rewrite', '')}-add-prefix-middleware`;
|
const addPrefixMiddlewareName = `addprefix-${middlewareName.replace('rewrite-', '')}`;
|
||||||
middlewares[addPrefixMiddlewareName] = {
|
middlewares[addPrefixMiddlewareName] = {
|
||||||
addPrefix: {
|
addPrefix: {
|
||||||
prefix: rewritePath
|
prefix: rewritePath
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Return both middlewares with a special flag to indicate chaining
|
|
||||||
return {
|
return {
|
||||||
middlewares,
|
middlewares,
|
||||||
chain: [middlewareName, addPrefixMiddlewareName]
|
chain: [middlewareName, addPrefixMiddlewareName]
|
||||||
@@ -279,7 +272,6 @@ function createPathRewriteMiddleware(
|
|||||||
} else if (pathMatchType === "regex") {
|
} else if (pathMatchType === "regex") {
|
||||||
regexPattern = path;
|
regexPattern = path;
|
||||||
} else {
|
} else {
|
||||||
// This shouldn't happen due to earlier validation, but handle gracefully
|
|
||||||
regexPattern = `^${escapeRegex(path)}`;
|
regexPattern = `^${escapeRegex(path)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -538,9 +530,10 @@ export async function getTraefikConfig(
|
|||||||
resource.path &&
|
resource.path &&
|
||||||
resource.pathMatchType &&
|
resource.pathMatchType &&
|
||||||
resource.rewritePathType) {
|
resource.rewritePathType) {
|
||||||
|
|
||||||
const rewriteMiddlewareName = `${resource.id}-${key}-rewrite`;
|
// Create a unique middleware name
|
||||||
|
const rewriteMiddlewareName = `rewrite-r${resource.resourceId}-${sanitizeForMiddlewareName(key)}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const rewriteResult = createPathRewriteMiddleware(
|
const rewriteResult = createPathRewriteMiddleware(
|
||||||
rewriteMiddlewareName,
|
rewriteMiddlewareName,
|
||||||
@@ -567,17 +560,17 @@ export async function getTraefikConfig(
|
|||||||
routerMiddlewares.push(rewriteMiddlewareName);
|
routerMiddlewares.push(rewriteMiddlewareName);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`Created path rewrite middleware for ${key}: ${resource.pathMatchType}(${resource.path}) -> ${resource.rewritePathType}(${resource.rewritePath})`);
|
logger.info(`Created path rewrite middleware ${rewriteMiddlewareName}: ${resource.pathMatchType}(${resource.path}) -> ${resource.rewritePathType}(${resource.rewritePath})`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Failed to create path rewrite middleware for ${key}: ${error}`);
|
logger.error(`Failed to create path rewrite middleware for resource ${resource.resourceId}: ${error}`);
|
||||||
// Continue without the rewrite middleware rather than failing completely
|
// Continue without the rewrite middleware rather than failing completely
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle custom headers middleware
|
// Handle custom headers middleware
|
||||||
if (resource.headers || resource.setHostHeader) {
|
if (resource.headers || resource.setHostHeader) {
|
||||||
// if there are headers, parse them into an object
|
|
||||||
const headersObj: { [key: string]: string } = {};
|
const headersObj: { [key: string]: string } = {};
|
||||||
|
|
||||||
if (resource.headers) {
|
if (resource.headers) {
|
||||||
let headersArr: { name: string; value: string }[] = [];
|
let headersArr: { name: string; value: string }[] = [];
|
||||||
try {
|
try {
|
||||||
@@ -586,9 +579,7 @@ export async function getTraefikConfig(
|
|||||||
value: string;
|
value: string;
|
||||||
}[];
|
}[];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.warn(
|
logger.warn(`Failed to parse headers for resource ${resource.resourceId}: ${e}`);
|
||||||
`Failed to parse headers for resource ${resource.resourceId}: ${e}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
headersArr.forEach((header) => {
|
headersArr.forEach((header) => {
|
||||||
@@ -600,9 +591,7 @@ export async function getTraefikConfig(
|
|||||||
headersObj["Host"] = resource.setHostHeader;
|
headersObj["Host"] = resource.setHostHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the object is not empty
|
|
||||||
if (Object.keys(headersObj).length > 0) {
|
if (Object.keys(headersObj).length > 0) {
|
||||||
// Add the headers middleware
|
|
||||||
if (!config_output.http.middlewares) {
|
if (!config_output.http.middlewares) {
|
||||||
config_output.http.middlewares = {};
|
config_output.http.middlewares = {};
|
||||||
}
|
}
|
||||||
@@ -616,8 +605,10 @@ export async function getTraefikConfig(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build routing rules
|
||||||
let rule = `Host(\`${fullDomain}\`)`;
|
let rule = `Host(\`${fullDomain}\`)`;
|
||||||
let priority = 100;
|
let priority = 100;
|
||||||
|
|
||||||
if (resource.path && resource.pathMatchType) {
|
if (resource.path && resource.pathMatchType) {
|
||||||
priority += 1;
|
priority += 1;
|
||||||
// add path to rule based on match type
|
// add path to rule based on match type
|
||||||
@@ -763,7 +754,7 @@ export async function getTraefikConfig(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Non-HTTP (TCP/UDP) configuration
|
// Non-HTTP (TCP/UDP) configuration
|
||||||
if (!resource.enableProxy) {
|
if (!resource.enableProxy || !resource.proxyPort) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -860,16 +851,22 @@ export async function getTraefikConfig(
|
|||||||
function sanitizePath(path: string | null | undefined): string | undefined {
|
function sanitizePath(path: string | null | undefined): string | undefined {
|
||||||
if (!path) return undefined;
|
if (!path) return undefined;
|
||||||
|
|
||||||
// For path rewriting, we need to be more careful about sanitization
|
const trimmed = path.trim();
|
||||||
// Only limit length and ensure it's a valid path structure
|
if (!trimmed) return undefined;
|
||||||
if (path.length > 50) {
|
|
||||||
path = path.substring(0, 50);
|
// Preserve path structure for rewriting, only warn if very long
|
||||||
logger.warn(`Path truncated to 50 characters: ${path}`);
|
if (trimmed.length > 1000) {
|
||||||
|
logger.warn(`Path exceeds 1000 characters: ${trimmed.substring(0, 100)}...`);
|
||||||
|
return trimmed.substring(0, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't remove special characters as they might be part of regex patterns
|
return trimmed;
|
||||||
// Just ensure it's not empty after trimming
|
}
|
||||||
return path.trim() || undefined;
|
|
||||||
|
function sanitizeForMiddlewareName(str: string): string {
|
||||||
|
// Replace any characters that aren't alphanumeric or dash with dash
|
||||||
|
// and remove consecutive dashes
|
||||||
|
return str.replace(/[^a-zA-Z0-9-]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeRegex(string: string): string {
|
function escapeRegex(string: string): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user