Skip to content

subdomain offset=2 #3561

Description

@weiwenying
/**
 * Express 默认 subdomain offset=2(按 example.com 两段根域)。
 * 主域 puter.bhhaihuan.com 是三段,offset=2 会把 "puter" 当成用户子域,
 * 从而 / → site.puter.bhhaihuan.com → 再跳回主域,形成 302 循环。
 * 在 trust proxy 设置之后把 offset 调成根域标签数。
 */
'use strict';

const express = require('/opt/puter/node_modules/express');
const origSet = express.application.set;

function rootLabelCount() {
    const fromEnv = Number(process.env.PUTER_SUBDOMAIN_OFFSET);
    if (Number.isInteger(fromEnv) && fromEnv >= 2) return fromEnv;
    try {
        const cfg = require('/etc/puter/config.json');
        const domain = String(cfg.domain || '')
            .split(':')[0]
            .replace(/^\./, '');
        const n = domain.split('.').filter(Boolean).length;
        if (n >= 2) return n;
    } catch {
        /* use default */
    }
    return 3;
}

express.application.set = function setPatched(setting, val) {
    const result = origSet.apply(this, arguments);
    if (setting === 'trust proxy') {
        origSet.call(this, 'subdomain offset', rootLabelCount());
    }
    return result;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    BackendTasks dealing with backend codehelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions