|
@@ -17,6 +17,7 @@ const LOCK_WAIT_ROUNDS = 40
|
|
|
const LOCK_WAIT_MS = 150
|
|
const LOCK_WAIT_MS = 150
|
|
|
|
|
|
|
|
let warnedTlsRejectUnauthorized = false
|
|
let warnedTlsRejectUnauthorized = false
|
|
|
|
|
+let projectSettingsTableEnsured = false
|
|
|
|
|
|
|
|
const logger = new Logger(path.join(__dirname, '../logs/QgProxyManager.log'), 'INFO')
|
|
const logger = new Logger(path.join(__dirname, '../logs/QgProxyManager.log'), 'INFO')
|
|
|
|
|
|
|
@@ -50,6 +51,36 @@ function hasTunnelServer() {
|
|
|
return tunnelServer.length > 0
|
|
return tunnelServer.length > 0
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function normalizeProjectKey(raw) {
|
|
|
|
|
+ return String(raw || '')
|
|
|
|
|
+ .trim()
|
|
|
|
|
+ .replace(/[^\w\u4e00-\u9fa5:.-]/g, '_')
|
|
|
|
|
+ .slice(0, 128)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function getProjectKey() {
|
|
|
|
|
+ return (
|
|
|
|
|
+ normalizeProjectKey(process.env.RUNFORGE_PROXY_SCOPE) ||
|
|
|
|
|
+ normalizeProjectKey(config.qgProxyScope) ||
|
|
|
|
|
+ normalizeProjectKey(config.qgChannelProxy?.scopeKey) ||
|
|
|
|
|
+ normalizeProjectKey(config.server) ||
|
|
|
|
|
+ normalizeProjectKey(config.port) ||
|
|
|
|
|
+ 'default'
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+async function ensureProjectSettingsTable() {
|
|
|
|
|
+ if (projectSettingsTableEnsured) return
|
|
|
|
|
+ await db.query(
|
|
|
|
|
+ `CREATE TABLE IF NOT EXISTS lepao_proxy_project_settings (
|
|
|
|
|
+ scope_key VARCHAR(128) NOT NULL PRIMARY KEY,
|
|
|
|
|
+ proxy_enabled TINYINT NOT NULL,
|
|
|
|
|
+ updated_at BIGINT NOT NULL
|
|
|
|
|
+ )`
|
|
|
|
|
+ )
|
|
|
|
|
+ projectSettingsTableEnsured = true
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
async function ensureSettingsRow() {
|
|
async function ensureSettingsRow() {
|
|
|
const now = Date.now()
|
|
const now = Date.now()
|
|
|
await db.query(
|
|
await db.query(
|
|
@@ -57,6 +88,12 @@ async function ensureSettingsRow() {
|
|
|
VALUES (1, 0, '', '', NULL, 1, ?)`,
|
|
VALUES (1, 0, '', '', NULL, 1, ?)`,
|
|
|
[now]
|
|
[now]
|
|
|
)
|
|
)
|
|
|
|
|
+ await ensureProjectSettingsTable()
|
|
|
|
|
+ await db.query(
|
|
|
|
|
+ `INSERT IGNORE INTO lepao_proxy_project_settings (scope_key, proxy_enabled, updated_at)
|
|
|
|
|
+ SELECT ?, proxy_enabled, ? FROM lepao_proxy_settings WHERE id = 1`,
|
|
|
|
|
+ [getProjectKey(), now]
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function loadSettings() {
|
|
async function loadSettings() {
|
|
@@ -64,7 +101,25 @@ async function loadSettings() {
|
|
|
const rows = await db.query(
|
|
const rows = await db.query(
|
|
|
`SELECT proxy_enabled, area, area_ex, isp, distinct_extract, updated_at FROM lepao_proxy_settings WHERE id = 1`
|
|
`SELECT proxy_enabled, area, area_ex, isp, distinct_extract, updated_at FROM lepao_proxy_settings WHERE id = 1`
|
|
|
)
|
|
)
|
|
|
- return rows?.[0] || null
|
|
|
|
|
|
|
+ const row = rows?.[0] || null
|
|
|
|
|
+ if (!row) return null
|
|
|
|
|
+
|
|
|
|
|
+ const projectRows = await db.query(
|
|
|
|
|
+ `SELECT proxy_enabled, updated_at FROM lepao_proxy_project_settings WHERE scope_key = ? LIMIT 1`,
|
|
|
|
|
+ [getProjectKey()]
|
|
|
|
|
+ )
|
|
|
|
|
+ const project = projectRows?.[0] || null
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...row,
|
|
|
|
|
+ project_scope_key: getProjectKey(),
|
|
|
|
|
+ project_proxy_enabled: project?.proxy_enabled ?? row.proxy_enabled,
|
|
|
|
|
+ project_updated_at: project?.updated_at ?? row.updated_at
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function getProjectProxyEnabledFromSettings(settings) {
|
|
|
|
|
+ if (!settings) return false
|
|
|
|
|
+ return Number(settings.project_proxy_enabled) === 1
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function parseDeadlineMs(deadlineStr) {
|
|
function parseDeadlineMs(deadlineStr) {
|
|
@@ -252,7 +307,7 @@ async function fetchResourceAreas() {
|
|
|
async function isOutboundProxyEnabled() {
|
|
async function isOutboundProxyEnabled() {
|
|
|
if (!hasExtractCredentials() && !(hasTunnelServer() && hasProxyAuth())) return false
|
|
if (!hasExtractCredentials() && !(hasTunnelServer() && hasProxyAuth())) return false
|
|
|
const row = await loadSettings()
|
|
const row = await loadSettings()
|
|
|
- return row && Number(row.proxy_enabled) === 1
|
|
|
|
|
|
|
+ return getProjectProxyEnabledFromSettings(row)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -266,7 +321,7 @@ async function getOutboundAxiosFragment(opt = {}) {
|
|
|
if (!hasExtractCredentials() && !(hasTunnelServer() && hasProxyAuth())) return { proxy: false }
|
|
if (!hasExtractCredentials() && !(hasTunnelServer() && hasProxyAuth())) return { proxy: false }
|
|
|
|
|
|
|
|
const settings = await loadSettings()
|
|
const settings = await loadSettings()
|
|
|
- if (!settings || Number(settings.proxy_enabled) !== 1) return { proxy: false }
|
|
|
|
|
|
|
+ if (!getProjectProxyEnabledFromSettings(settings)) return { proxy: false }
|
|
|
|
|
|
|
|
// 隧道代理模式:直接使用隧道入口地址,不需要 /pool 提取。
|
|
// 隧道代理模式:直接使用隧道入口地址,不需要 /pool 提取。
|
|
|
const tunnelFrag = buildTunnelProxyOpts(settings)
|
|
const tunnelFrag = buildTunnelProxyOpts(settings)
|
|
@@ -440,7 +495,10 @@ async function getStatusSnapshot() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
- proxy_enabled: row ? Number(row.proxy_enabled) === 1 : false,
|
|
|
|
|
|
|
+ project_scope_key: getProjectKey(),
|
|
|
|
|
+ proxy_enabled: getProjectProxyEnabledFromSettings(row),
|
|
|
|
|
+ proxy_enabled_default: row ? Number(row.proxy_enabled) === 1 : false,
|
|
|
|
|
+ project_proxy_updated_at: row?.project_updated_at ?? 0,
|
|
|
area: row?.area ?? '',
|
|
area: row?.area ?? '',
|
|
|
area_ex: row?.area_ex ?? '',
|
|
area_ex: row?.area_ex ?? '',
|
|
|
isp: row?.isp == null ? null : Number(row.isp),
|
|
isp: row?.isp == null ? null : Number(row.isp),
|
|
@@ -458,6 +516,8 @@ module.exports = {
|
|
|
getQgConfig,
|
|
getQgConfig,
|
|
|
hasExtractCredentials,
|
|
hasExtractCredentials,
|
|
|
hasProxyAuth,
|
|
hasProxyAuth,
|
|
|
|
|
+ getProjectKey,
|
|
|
|
|
+ getProjectProxyEnabledFromSettings,
|
|
|
ensureSettingsRow,
|
|
ensureSettingsRow,
|
|
|
loadSettings,
|
|
loadSettings,
|
|
|
isOutboundProxyEnabled,
|
|
isOutboundProxyEnabled,
|