mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-24 19:46:38 +00:00
Compare commits
18 Commits
1.16.2-s.1
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
19f8c1772f | ||
|
|
37d331e813 | ||
|
|
c660df55cd | ||
|
|
7c8b865379 | ||
|
|
3cca0c09c0 | ||
|
|
7c2b4f422a | ||
|
|
ad2a0ae127 | ||
|
|
6c2c620c99 | ||
|
|
f643abf19a | ||
|
|
a1729033cf | ||
|
|
7311766512 | ||
|
|
17105f3a51 | ||
|
|
edcfbd26e4 | ||
|
|
0c4d9ea164 | ||
|
|
b01fcc70fe | ||
|
|
35fed74e49 | ||
|
|
6cf1b9b010 | ||
|
|
dae169540b |
123
server/lib/ip.ts
123
server/lib/ip.ts
@@ -571,6 +571,129 @@ export function generateSubnetProxyTargets(
|
|||||||
return targets;
|
return targets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SubnetProxyTargetV2 = {
|
||||||
|
sourcePrefixes: string[]; // must be cidrs
|
||||||
|
destPrefix: string; // must be a cidr
|
||||||
|
disableIcmp?: boolean;
|
||||||
|
rewriteTo?: string; // must be a cidr
|
||||||
|
portRange?: {
|
||||||
|
min: number;
|
||||||
|
max: number;
|
||||||
|
protocol: "tcp" | "udp";
|
||||||
|
}[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export function generateSubnetProxyTargetV2(
|
||||||
|
siteResource: SiteResource,
|
||||||
|
clients: {
|
||||||
|
clientId: number;
|
||||||
|
pubKey: string | null;
|
||||||
|
subnet: string | null;
|
||||||
|
}[]
|
||||||
|
): SubnetProxyTargetV2 | undefined {
|
||||||
|
if (clients.length === 0) {
|
||||||
|
logger.debug(
|
||||||
|
`No clients have access to site resource ${siteResource.siteResourceId}, skipping target generation.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let target: SubnetProxyTargetV2 | null = null;
|
||||||
|
|
||||||
|
const portRange = [
|
||||||
|
...parsePortRangeString(siteResource.tcpPortRangeString, "tcp"),
|
||||||
|
...parsePortRangeString(siteResource.udpPortRangeString, "udp")
|
||||||
|
];
|
||||||
|
const disableIcmp = siteResource.disableIcmp ?? false;
|
||||||
|
|
||||||
|
if (siteResource.mode == "host") {
|
||||||
|
let destination = siteResource.destination;
|
||||||
|
// check if this is a valid ip
|
||||||
|
const ipSchema = z.union([z.ipv4(), z.ipv6()]);
|
||||||
|
if (ipSchema.safeParse(destination).success) {
|
||||||
|
destination = `${destination}/32`;
|
||||||
|
|
||||||
|
target = {
|
||||||
|
sourcePrefixes: [],
|
||||||
|
destPrefix: destination,
|
||||||
|
portRange,
|
||||||
|
disableIcmp
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (siteResource.alias && siteResource.aliasAddress) {
|
||||||
|
// also push a match for the alias address
|
||||||
|
target = {
|
||||||
|
sourcePrefixes: [],
|
||||||
|
destPrefix: `${siteResource.aliasAddress}/32`,
|
||||||
|
rewriteTo: destination,
|
||||||
|
portRange,
|
||||||
|
disableIcmp
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} else if (siteResource.mode == "cidr") {
|
||||||
|
target = {
|
||||||
|
sourcePrefixes: [],
|
||||||
|
destPrefix: siteResource.destination,
|
||||||
|
portRange,
|
||||||
|
disableIcmp
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!target) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const clientSite of clients) {
|
||||||
|
if (!clientSite.subnet) {
|
||||||
|
logger.debug(
|
||||||
|
`Client ${clientSite.clientId} has no subnet, skipping for site resource ${siteResource.siteResourceId}.`
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientPrefix = `${clientSite.subnet.split("/")[0]}/32`;
|
||||||
|
|
||||||
|
// add client prefix to source prefixes
|
||||||
|
target.sourcePrefixes.push(clientPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
// print a nice representation of the targets
|
||||||
|
// logger.debug(
|
||||||
|
// `Generated subnet proxy targets for: ${JSON.stringify(targets, null, 2)}`
|
||||||
|
// );
|
||||||
|
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a SubnetProxyTargetV2 to an array of SubnetProxyTarget (v1)
|
||||||
|
* by expanding each source prefix into its own target entry.
|
||||||
|
* @param targetV2 - The v2 target to convert
|
||||||
|
* @returns Array of v1 SubnetProxyTarget objects
|
||||||
|
*/
|
||||||
|
export function convertSubnetProxyTargetsV2ToV1(
|
||||||
|
targetsV2: SubnetProxyTargetV2[]
|
||||||
|
): SubnetProxyTarget[] {
|
||||||
|
return targetsV2.flatMap((targetV2) =>
|
||||||
|
targetV2.sourcePrefixes.map((sourcePrefix) => ({
|
||||||
|
sourcePrefix,
|
||||||
|
destPrefix: targetV2.destPrefix,
|
||||||
|
...(targetV2.disableIcmp !== undefined && {
|
||||||
|
disableIcmp: targetV2.disableIcmp
|
||||||
|
}),
|
||||||
|
...(targetV2.rewriteTo !== undefined && {
|
||||||
|
rewriteTo: targetV2.rewriteTo
|
||||||
|
}),
|
||||||
|
...(targetV2.portRange !== undefined && {
|
||||||
|
portRange: targetV2.portRange
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Custom schema for validating port range strings
|
// Custom schema for validating port range strings
|
||||||
// Format: "80,443,8000-9000" or "*" for all ports, or empty string
|
// Format: "80,443,8000-9000" or "*" for all ports, or empty string
|
||||||
export const portRangeStringSchema = z
|
export const portRangeStringSchema = z
|
||||||
|
|||||||
@@ -302,8 +302,8 @@ export const configSchema = z
|
|||||||
.optional()
|
.optional()
|
||||||
.default({
|
.default({
|
||||||
block_size: 24,
|
block_size: 24,
|
||||||
subnet_group: "100.90.128.0/24",
|
subnet_group: "100.90.128.0/20",
|
||||||
utility_subnet_group: "100.96.128.0/24"
|
utility_subnet_group: "100.96.128.0/20"
|
||||||
}),
|
}),
|
||||||
rate_limits: z
|
rate_limits: z
|
||||||
.object({
|
.object({
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import logger from "@server/logger";
|
|||||||
import {
|
import {
|
||||||
generateAliasConfig,
|
generateAliasConfig,
|
||||||
generateRemoteSubnets,
|
generateRemoteSubnets,
|
||||||
generateSubnetProxyTargets,
|
generateSubnetProxyTargetV2,
|
||||||
parseEndpoint,
|
parseEndpoint,
|
||||||
formatEndpoint
|
formatEndpoint
|
||||||
} from "@server/lib/ip";
|
} from "@server/lib/ip";
|
||||||
@@ -660,19 +660,16 @@ async function handleSubnetProxyTargetUpdates(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (addedClients.length > 0) {
|
if (addedClients.length > 0) {
|
||||||
const targetsToAdd = generateSubnetProxyTargets(
|
const targetToAdd = generateSubnetProxyTargetV2(
|
||||||
siteResource,
|
siteResource,
|
||||||
addedClients
|
addedClients
|
||||||
);
|
);
|
||||||
|
|
||||||
if (targetsToAdd.length > 0) {
|
if (targetToAdd) {
|
||||||
logger.info(
|
|
||||||
`Adding ${targetsToAdd.length} subnet proxy targets for siteResource ${siteResource.siteResourceId}`
|
|
||||||
);
|
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
addSubnetProxyTargets(
|
addSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
targetsToAdd,
|
[targetToAdd],
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -700,19 +697,16 @@ async function handleSubnetProxyTargetUpdates(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (removedClients.length > 0) {
|
if (removedClients.length > 0) {
|
||||||
const targetsToRemove = generateSubnetProxyTargets(
|
const targetToRemove = generateSubnetProxyTargetV2(
|
||||||
siteResource,
|
siteResource,
|
||||||
removedClients
|
removedClients
|
||||||
);
|
);
|
||||||
|
|
||||||
if (targetsToRemove.length > 0) {
|
if (targetToRemove) {
|
||||||
logger.info(
|
|
||||||
`Removing ${targetsToRemove.length} subnet proxy targets for siteResource ${siteResource.siteResourceId}`
|
|
||||||
);
|
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
removeSubnetProxyTargets(
|
removeSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
targetsToRemove,
|
[targetToRemove],
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -1169,7 +1163,7 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const resource of resources) {
|
for (const resource of resources) {
|
||||||
const targets = generateSubnetProxyTargets(resource, [
|
const target = generateSubnetProxyTargetV2(resource, [
|
||||||
{
|
{
|
||||||
clientId: client.clientId,
|
clientId: client.clientId,
|
||||||
pubKey: client.pubKey,
|
pubKey: client.pubKey,
|
||||||
@@ -1177,11 +1171,11 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (targets.length > 0) {
|
if (target) {
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
addSubnetProxyTargets(
|
addSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
targets,
|
[target],
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -1246,7 +1240,7 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const resource of resources) {
|
for (const resource of resources) {
|
||||||
const targets = generateSubnetProxyTargets(resource, [
|
const target = generateSubnetProxyTargetV2(resource, [
|
||||||
{
|
{
|
||||||
clientId: client.clientId,
|
clientId: client.clientId,
|
||||||
pubKey: client.pubKey,
|
pubKey: client.pubKey,
|
||||||
@@ -1254,11 +1248,11 @@ async function handleMessagesForClientResources(
|
|||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (targets.length > 0) {
|
if (target) {
|
||||||
proxyJobs.push(
|
proxyJobs.push(
|
||||||
removeSubnetProxyTargets(
|
removeSubnetProxyTargets(
|
||||||
newt.newtId,
|
newt.newtId,
|
||||||
targets,
|
[target],
|
||||||
newt.version
|
newt.version
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
40
server/lib/sanitize.ts
Normal file
40
server/lib/sanitize.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Sanitize a string field before inserting into a database TEXT column.
|
||||||
|
*
|
||||||
|
* Two passes are applied:
|
||||||
|
*
|
||||||
|
* 1. Lone UTF-16 surrogates – JavaScript strings can hold unpaired surrogates
|
||||||
|
* (e.g. \uD800 without a following \uDC00-\uDFFF codepoint). These are
|
||||||
|
* valid in JS but cannot be encoded as UTF-8, triggering
|
||||||
|
* `report_invalid_encoding` in SQLite / Postgres. They are replaced with
|
||||||
|
* the Unicode replacement character U+FFFD so the data is preserved as a
|
||||||
|
* visible signal that something was malformed.
|
||||||
|
*
|
||||||
|
* 2. Null bytes and C0 control characters – SQLite stores TEXT as
|
||||||
|
* null-terminated C strings, so \x00 in a value causes
|
||||||
|
* `report_invalid_encoding`. Bots and scanners routinely inject null bytes
|
||||||
|
* into URLs (e.g. `/path\u0000.jpg`). All C0 control characters in the
|
||||||
|
* range \x00-\x1F are stripped except for the three that are legitimate in
|
||||||
|
* text payloads: HT (\x09), LF (\x0A), and CR (\x0D). DEL (\x7F) is also
|
||||||
|
* stripped.
|
||||||
|
*/
|
||||||
|
export function sanitizeString(value: string): string;
|
||||||
|
export function sanitizeString(
|
||||||
|
value: string | null | undefined
|
||||||
|
): string | undefined;
|
||||||
|
export function sanitizeString(
|
||||||
|
value: string | null | undefined
|
||||||
|
): string | undefined {
|
||||||
|
if (value == null) return undefined;
|
||||||
|
return (
|
||||||
|
value
|
||||||
|
// Replace lone high surrogates (not followed by a low surrogate)
|
||||||
|
// and lone low surrogates (not preceded by a high surrogate).
|
||||||
|
.replace(
|
||||||
|
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g,
|
||||||
|
"\uFFFD"
|
||||||
|
)
|
||||||
|
// Strip null bytes, C0 control chars (except HT/LF/CR), and DEL.
|
||||||
|
.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, "")
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -24,23 +24,31 @@ setInterval(() => {
|
|||||||
*/
|
*/
|
||||||
class AdaptiveCache {
|
class AdaptiveCache {
|
||||||
private useRedis(): boolean {
|
private useRedis(): boolean {
|
||||||
return redisManager.isRedisEnabled() && redisManager.getHealthStatus().isHealthy;
|
return (
|
||||||
|
redisManager.isRedisEnabled() &&
|
||||||
|
redisManager.getHealthStatus().isHealthy
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a value in the cache
|
* Set a value in the cache
|
||||||
* @param key - Cache key
|
* @param key - Cache key
|
||||||
* @param value - Value to cache (will be JSON stringified for Redis)
|
* @param value - Value to cache (will be JSON stringified for Redis)
|
||||||
* @param ttl - Time to live in seconds (0 = no expiration)
|
* @param ttl - Time to live in seconds (0 = no expiration; omit = 3600s for Redis)
|
||||||
* @returns boolean indicating success
|
* @returns boolean indicating success
|
||||||
*/
|
*/
|
||||||
async set(key: string, value: any, ttl?: number): Promise<boolean> {
|
async set(key: string, value: any, ttl?: number): Promise<boolean> {
|
||||||
const effectiveTtl = ttl === 0 ? undefined : ttl;
|
const effectiveTtl = ttl === 0 ? undefined : ttl;
|
||||||
|
const redisTtl = ttl === 0 ? undefined : (ttl ?? 3600);
|
||||||
|
|
||||||
if (this.useRedis()) {
|
if (this.useRedis()) {
|
||||||
try {
|
try {
|
||||||
const serialized = JSON.stringify(value);
|
const serialized = JSON.stringify(value);
|
||||||
const success = await redisManager.set(key, serialized, effectiveTtl);
|
const success = await redisManager.set(
|
||||||
|
key,
|
||||||
|
serialized,
|
||||||
|
redisTtl
|
||||||
|
);
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
logger.debug(`Set key in Redis: ${key}`);
|
logger.debug(`Set key in Redis: ${key}`);
|
||||||
@@ -48,7 +56,9 @@ class AdaptiveCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Redis failed, fall through to local cache
|
// Redis failed, fall through to local cache
|
||||||
logger.debug(`Redis set failed for key ${key}, falling back to local cache`);
|
logger.debug(
|
||||||
|
`Redis set failed for key ${key}, falling back to local cache`
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Redis set error for key ${key}:`, error);
|
logger.error(`Redis set error for key ${key}:`, error);
|
||||||
// Fall through to local cache
|
// Fall through to local cache
|
||||||
@@ -120,9 +130,14 @@ class AdaptiveCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Some Redis deletes failed, fall through to local cache
|
// Some Redis deletes failed, fall through to local cache
|
||||||
logger.debug(`Some Redis deletes failed, falling back to local cache`);
|
logger.debug(
|
||||||
|
`Some Redis deletes failed, falling back to local cache`
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Redis del error for keys ${keys.join(", ")}:`, error);
|
logger.error(
|
||||||
|
`Redis del error for keys ${keys.join(", ")}:`,
|
||||||
|
error
|
||||||
|
);
|
||||||
// Fall through to local cache
|
// Fall through to local cache
|
||||||
deletedCount = 0;
|
deletedCount = 0;
|
||||||
}
|
}
|
||||||
@@ -195,7 +210,9 @@ class AdaptiveCache {
|
|||||||
*/
|
*/
|
||||||
async flushAll(): Promise<void> {
|
async flushAll(): Promise<void> {
|
||||||
if (this.useRedis()) {
|
if (this.useRedis()) {
|
||||||
logger.warn("Adaptive cache flushAll called - Redis flush not implemented, only local cache will be flushed");
|
logger.warn(
|
||||||
|
"Adaptive cache flushAll called - Redis flush not implemented, only local cache will be flushed"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
localCache.flushAll();
|
localCache.flushAll();
|
||||||
@@ -239,7 +256,9 @@ class AdaptiveCache {
|
|||||||
getTtl(key: string): number {
|
getTtl(key: string): number {
|
||||||
// Note: This only works for local cache, Redis TTL is not supported
|
// Note: This only works for local cache, Redis TTL is not supported
|
||||||
if (this.useRedis()) {
|
if (this.useRedis()) {
|
||||||
logger.warn(`getTtl called for key ${key} but Redis TTL lookup is not implemented`);
|
logger.warn(
|
||||||
|
`getTtl called for key ${key} but Redis TTL lookup is not implemented`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ttl = localCache.getTtl(key);
|
const ttl = localCache.getTtl(key);
|
||||||
@@ -255,7 +274,9 @@ class AdaptiveCache {
|
|||||||
*/
|
*/
|
||||||
keys(): string[] {
|
keys(): string[] {
|
||||||
if (this.useRedis()) {
|
if (this.useRedis()) {
|
||||||
logger.warn("keys() called but Redis keys are not included, only local cache keys returned");
|
logger.warn(
|
||||||
|
"keys() called but Redis keys are not included, only local cache keys returned"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return localCache.keys();
|
return localCache.keys();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,10 @@ export const privateConfigSchema = z.object({
|
|||||||
.object({
|
.object({
|
||||||
host: z.string(),
|
host: z.string(),
|
||||||
port: portSchema,
|
port: portSchema,
|
||||||
password: z.string().optional(),
|
password: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform(getEnvOrYaml("REDIS_PASSWORD")),
|
||||||
db: z.int().nonnegative().optional().default(0),
|
db: z.int().nonnegative().optional().default(0),
|
||||||
replicas: z
|
replicas: z
|
||||||
.array(
|
.array(
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import { verifySessionRemoteExitNodeMiddleware } from "#private/middlewares/veri
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import {
|
import {
|
||||||
db,
|
db,
|
||||||
|
logsDb,
|
||||||
exitNodes,
|
exitNodes,
|
||||||
Resource,
|
Resource,
|
||||||
ResourcePassword,
|
ResourcePassword,
|
||||||
@@ -81,6 +82,7 @@ import { verifyResourceAccessToken } from "@server/auth/verifyResourceAccessToke
|
|||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import { maxmindAsnLookup } from "@server/db/maxmindAsn";
|
import { maxmindAsnLookup } from "@server/db/maxmindAsn";
|
||||||
import { checkOrgAccessPolicy } from "@server/lib/checkOrgAccessPolicy";
|
import { checkOrgAccessPolicy } from "@server/lib/checkOrgAccessPolicy";
|
||||||
|
import { sanitizeString } from "@server/lib/sanitize";
|
||||||
|
|
||||||
// Zod schemas for request validation
|
// Zod schemas for request validation
|
||||||
const getResourceByDomainParamsSchema = z.strictObject({
|
const getResourceByDomainParamsSchema = z.strictObject({
|
||||||
@@ -1859,24 +1861,24 @@ hybridRouter.post(
|
|||||||
})
|
})
|
||||||
.map((logEntry) => ({
|
.map((logEntry) => ({
|
||||||
timestamp: logEntry.timestamp,
|
timestamp: logEntry.timestamp,
|
||||||
orgId: logEntry.orgId,
|
orgId: sanitizeString(logEntry.orgId),
|
||||||
actorType: logEntry.actorType,
|
actorType: sanitizeString(logEntry.actorType),
|
||||||
actor: logEntry.actor,
|
actor: sanitizeString(logEntry.actor),
|
||||||
actorId: logEntry.actorId,
|
actorId: sanitizeString(logEntry.actorId),
|
||||||
metadata: logEntry.metadata,
|
metadata: sanitizeString(logEntry.metadata),
|
||||||
action: logEntry.action,
|
action: logEntry.action,
|
||||||
resourceId: logEntry.resourceId,
|
resourceId: logEntry.resourceId,
|
||||||
reason: logEntry.reason,
|
reason: logEntry.reason,
|
||||||
location: logEntry.location,
|
location: sanitizeString(logEntry.location),
|
||||||
// userAgent: data.userAgent, // TODO: add this
|
// userAgent: data.userAgent, // TODO: add this
|
||||||
// headers: data.body.headers,
|
// headers: data.body.headers,
|
||||||
// query: data.body.query,
|
// query: data.body.query,
|
||||||
originalRequestURL: logEntry.originalRequestURL,
|
originalRequestURL: sanitizeString(logEntry.originalRequestURL) ?? "",
|
||||||
scheme: logEntry.scheme,
|
scheme: sanitizeString(logEntry.scheme) ?? "",
|
||||||
host: logEntry.host,
|
host: sanitizeString(logEntry.host) ?? "",
|
||||||
path: logEntry.path,
|
path: sanitizeString(logEntry.path) ?? "",
|
||||||
method: logEntry.method,
|
method: sanitizeString(logEntry.method) ?? "",
|
||||||
ip: logEntry.ip,
|
ip: sanitizeString(logEntry.ip),
|
||||||
tls: logEntry.tls
|
tls: logEntry.tls
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -1884,7 +1886,7 @@ hybridRouter.post(
|
|||||||
const batchSize = 100;
|
const batchSize = 100;
|
||||||
for (let i = 0; i < logEntries.length; i += batchSize) {
|
for (let i = 0; i < logEntries.length; i += batchSize) {
|
||||||
const batch = logEntries.slice(i, i + batchSize);
|
const batch = logEntries.slice(i, i + batchSize);
|
||||||
await db.insert(requestAuditLog).values(batch);
|
await logsDb.insert(requestAuditLog).values(batch);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response(res, {
|
return response(res, {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export const startRemoteExitNodeOfflineChecker = (): void => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Find clients that haven't pinged in the last 2 minutes and mark them as offline
|
// Find clients that haven't pinged in the last 2 minutes and mark them as offline
|
||||||
const newlyOfflineNodes = await db
|
const offlineNodes = await db
|
||||||
.update(exitNodes)
|
.update(exitNodes)
|
||||||
.set({ online: false })
|
.set({ online: false })
|
||||||
.where(
|
.where(
|
||||||
@@ -53,32 +53,15 @@ export const startRemoteExitNodeOfflineChecker = (): void => {
|
|||||||
)
|
)
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
// Update the sites to offline if they have not pinged either
|
if (offlineNodes.length > 0) {
|
||||||
const exitNodeIds = newlyOfflineNodes.map(
|
logger.info(
|
||||||
(node) => node.exitNodeId
|
`checkRemoteExitNodeOffline: Marked ${offlineNodes.length} remoteExitNode client(s) offline due to inactivity`
|
||||||
);
|
|
||||||
|
|
||||||
const sitesOnNode = await db
|
|
||||||
.select()
|
|
||||||
.from(sites)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(sites.online, true),
|
|
||||||
inArray(sites.exitNodeId, exitNodeIds)
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// loop through the sites and process their lastBandwidthUpdate as an iso string and if its more than 1 minute old then mark the site offline
|
for (const offlineClient of offlineNodes) {
|
||||||
for (const site of sitesOnNode) {
|
logger.debug(
|
||||||
if (!site.lastBandwidthUpdate) {
|
`checkRemoteExitNodeOffline: Client ${offlineClient.exitNodeId} marked offline (lastPing: ${offlineClient.lastPing})`
|
||||||
continue;
|
);
|
||||||
}
|
|
||||||
const lastBandwidthUpdate = new Date(site.lastBandwidthUpdate);
|
|
||||||
if (Date.now() - lastBandwidthUpdate.getTime() > 60 * 1000) {
|
|
||||||
await db
|
|
||||||
.update(sites)
|
|
||||||
.set({ online: false })
|
|
||||||
.where(eq(sites.siteId, site.siteId));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -5,25 +5,7 @@ import cache from "#dynamic/lib/cache";
|
|||||||
import { calculateCutoffTimestamp } from "@server/lib/cleanupLogs";
|
import { calculateCutoffTimestamp } from "@server/lib/cleanupLogs";
|
||||||
import { stripPortFromHost } from "@server/lib/ip";
|
import { stripPortFromHost } from "@server/lib/ip";
|
||||||
|
|
||||||
/**
|
import { sanitizeString } from "@server/lib/sanitize";
|
||||||
* Sanitize a string field by replacing lone UTF-16 surrogates (which cannot
|
|
||||||
* be encoded as valid UTF-8) with the Unicode replacement character, and
|
|
||||||
* stripping ASCII control characters that are invalid in most text columns.
|
|
||||||
*/
|
|
||||||
function sanitizeString(value: string | undefined | null): string | undefined {
|
|
||||||
if (value == null) return undefined;
|
|
||||||
return (
|
|
||||||
value
|
|
||||||
// Replace lone high surrogates (not followed by a low surrogate)
|
|
||||||
// and lone low surrogates (not preceded by a high surrogate)
|
|
||||||
.replace(
|
|
||||||
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g,
|
|
||||||
"\uFFFD"
|
|
||||||
)
|
|
||||||
// Strip C0 control characters except HT (\x09), LF (\x0A), CR (\x0D)
|
|
||||||
.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, "")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ async function getLatestOlmVersion(): Promise<string | null> {
|
|||||||
tags = tags.filter((version) => !version.name.includes("rc"));
|
tags = tags.filter((version) => !version.name.includes("rc"));
|
||||||
const latestVersion = tags[0].name;
|
const latestVersion = tags[0].name;
|
||||||
|
|
||||||
olmVersionCache.set("latestOlmVersion", latestVersion);
|
olmVersionCache.set("latestOlmVersion", latestVersion, 3600);
|
||||||
|
|
||||||
return latestVersion;
|
return latestVersion;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ async function getLatestOlmVersion(): Promise<string | null> {
|
|||||||
tags = tags.filter((version) => !version.name.includes("rc"));
|
tags = tags.filter((version) => !version.name.includes("rc"));
|
||||||
const latestVersion = tags[0].name;
|
const latestVersion = tags[0].name;
|
||||||
|
|
||||||
olmVersionCache.set("latestOlmVersion", latestVersion);
|
olmVersionCache.set("latestOlmVersion", latestVersion, 3600);
|
||||||
|
|
||||||
return latestVersion;
|
return latestVersion;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@@ -1,15 +1,54 @@
|
|||||||
import { sendToClient } from "#dynamic/routers/ws";
|
import { sendToClient } from "#dynamic/routers/ws";
|
||||||
import { db, olms, Transaction } from "@server/db";
|
import { db, newts, olms } from "@server/db";
|
||||||
|
import {
|
||||||
|
Alias,
|
||||||
|
convertSubnetProxyTargetsV2ToV1,
|
||||||
|
SubnetProxyTarget,
|
||||||
|
SubnetProxyTargetV2
|
||||||
|
} from "@server/lib/ip";
|
||||||
import { canCompress } from "@server/lib/clientVersionChecks";
|
import { canCompress } from "@server/lib/clientVersionChecks";
|
||||||
import { Alias, SubnetProxyTarget } from "@server/lib/ip";
|
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
import semver from "semver";
|
||||||
|
|
||||||
|
const NEWT_V2_TARGETS_VERSION = ">=1.10.3";
|
||||||
|
|
||||||
|
export async function convertTargetsIfNessicary(
|
||||||
|
newtId: string,
|
||||||
|
targets: SubnetProxyTarget[] | SubnetProxyTargetV2[]
|
||||||
|
) {
|
||||||
|
// get the newt
|
||||||
|
const [newt] = await db
|
||||||
|
.select()
|
||||||
|
.from(newts)
|
||||||
|
.where(eq(newts.newtId, newtId));
|
||||||
|
if (!newt) {
|
||||||
|
throw new Error(`No newt found for id: ${newtId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the semver
|
||||||
|
if (
|
||||||
|
newt.version &&
|
||||||
|
!semver.satisfies(newt.version, NEWT_V2_TARGETS_VERSION)
|
||||||
|
) {
|
||||||
|
logger.debug(
|
||||||
|
`addTargets Newt version ${newt.version} does not support targets v2 falling back`
|
||||||
|
);
|
||||||
|
targets = convertSubnetProxyTargetsV2ToV1(
|
||||||
|
targets as SubnetProxyTargetV2[]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return targets;
|
||||||
|
}
|
||||||
|
|
||||||
export async function addTargets(
|
export async function addTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: SubnetProxyTarget[],
|
targets: SubnetProxyTarget[] | SubnetProxyTargetV2[],
|
||||||
version?: string | null
|
version?: string | null
|
||||||
) {
|
) {
|
||||||
|
targets = await convertTargetsIfNessicary(newtId, targets);
|
||||||
|
|
||||||
await sendToClient(
|
await sendToClient(
|
||||||
newtId,
|
newtId,
|
||||||
{
|
{
|
||||||
@@ -22,9 +61,11 @@ export async function addTargets(
|
|||||||
|
|
||||||
export async function removeTargets(
|
export async function removeTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: SubnetProxyTarget[],
|
targets: SubnetProxyTarget[] | SubnetProxyTargetV2[],
|
||||||
version?: string | null
|
version?: string | null
|
||||||
) {
|
) {
|
||||||
|
targets = await convertTargetsIfNessicary(newtId, targets);
|
||||||
|
|
||||||
await sendToClient(
|
await sendToClient(
|
||||||
newtId,
|
newtId,
|
||||||
{
|
{
|
||||||
@@ -38,11 +79,39 @@ export async function removeTargets(
|
|||||||
export async function updateTargets(
|
export async function updateTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: {
|
targets: {
|
||||||
oldTargets: SubnetProxyTarget[];
|
oldTargets: SubnetProxyTarget[] | SubnetProxyTargetV2[];
|
||||||
newTargets: SubnetProxyTarget[];
|
newTargets: SubnetProxyTarget[] | SubnetProxyTargetV2[];
|
||||||
},
|
},
|
||||||
version?: string | null
|
version?: string | null
|
||||||
) {
|
) {
|
||||||
|
// get the newt
|
||||||
|
const [newt] = await db
|
||||||
|
.select()
|
||||||
|
.from(newts)
|
||||||
|
.where(eq(newts.newtId, newtId));
|
||||||
|
if (!newt) {
|
||||||
|
logger.error(`addTargetsL No newt found for id: ${newtId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the semver
|
||||||
|
if (
|
||||||
|
newt.version &&
|
||||||
|
!semver.satisfies(newt.version, NEWT_V2_TARGETS_VERSION)
|
||||||
|
) {
|
||||||
|
logger.debug(
|
||||||
|
`addTargets Newt version ${newt.version} does not support targets v2 falling back`
|
||||||
|
);
|
||||||
|
targets = {
|
||||||
|
oldTargets: convertSubnetProxyTargetsV2ToV1(
|
||||||
|
targets.oldTargets as SubnetProxyTargetV2[]
|
||||||
|
),
|
||||||
|
newTargets: convertSubnetProxyTargetsV2ToV1(
|
||||||
|
targets.newTargets as SubnetProxyTargetV2[]
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
await sendToClient(
|
await sendToClient(
|
||||||
newtId,
|
newtId,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import { eq, and } from "drizzle-orm";
|
|||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import {
|
import {
|
||||||
formatEndpoint,
|
formatEndpoint,
|
||||||
generateSubnetProxyTargets,
|
generateSubnetProxyTargetV2,
|
||||||
SubnetProxyTarget
|
SubnetProxyTargetV2
|
||||||
} from "@server/lib/ip";
|
} from "@server/lib/ip";
|
||||||
|
|
||||||
export async function buildClientConfigurationForNewtClient(
|
export async function buildClientConfigurationForNewtClient(
|
||||||
@@ -143,7 +143,7 @@ export async function buildClientConfigurationForNewtClient(
|
|||||||
.from(siteResources)
|
.from(siteResources)
|
||||||
.where(eq(siteResources.siteId, siteId));
|
.where(eq(siteResources.siteId, siteId));
|
||||||
|
|
||||||
const targetsToSend: SubnetProxyTarget[] = [];
|
const targetsToSend: SubnetProxyTargetV2[] = [];
|
||||||
|
|
||||||
for (const resource of allSiteResources) {
|
for (const resource of allSiteResources) {
|
||||||
// Get clients associated with this specific resource
|
// Get clients associated with this specific resource
|
||||||
@@ -168,12 +168,14 @@ export async function buildClientConfigurationForNewtClient(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
const resourceTargets = generateSubnetProxyTargets(
|
const resourceTarget = generateSubnetProxyTargetV2(
|
||||||
resource,
|
resource,
|
||||||
resourceClients
|
resourceClients
|
||||||
);
|
);
|
||||||
|
|
||||||
targetsToSend.push(...resourceTargets);
|
if (resourceTarget) {
|
||||||
|
targetsToSend.push(resourceTarget);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { db, ExitNode, exitNodes, Newt, sites } from "@server/db";
|
|||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { sendToExitNode } from "#dynamic/lib/exitNodes";
|
import { sendToExitNode } from "#dynamic/lib/exitNodes";
|
||||||
import { buildClientConfigurationForNewtClient } from "./buildConfiguration";
|
import { buildClientConfigurationForNewtClient } from "./buildConfiguration";
|
||||||
|
import { convertTargetsIfNessicary } from "../client/targets";
|
||||||
import { canCompress } from "@server/lib/clientVersionChecks";
|
import { canCompress } from "@server/lib/clientVersionChecks";
|
||||||
|
|
||||||
const inputSchema = z.object({
|
const inputSchema = z.object({
|
||||||
@@ -127,13 +128,15 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
|
|||||||
exitNode
|
exitNode
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const targetsToSend = await convertTargetsIfNessicary(newt.newtId, targets);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
message: {
|
message: {
|
||||||
type: "newt/wg/receive-config",
|
type: "newt/wg/receive-config",
|
||||||
data: {
|
data: {
|
||||||
ipAddress: site.address,
|
ipAddress: site.address,
|
||||||
peers,
|
peers,
|
||||||
targets
|
targets: targetsToSend
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import logger from "@server/logger";
|
|||||||
/**
|
/**
|
||||||
* Handles disconnecting messages from sites to show disconnected in the ui
|
* Handles disconnecting messages from sites to show disconnected in the ui
|
||||||
*/
|
*/
|
||||||
export const handleNewtDisconnectingMessage: MessageHandler = async (context) => {
|
export const handleNewtDisconnectingMessage: MessageHandler = async (
|
||||||
|
context
|
||||||
|
) => {
|
||||||
const { message, client: c, sendToClient } = context;
|
const { message, client: c, sendToClient } = context;
|
||||||
const newt = c as Newt;
|
const newt = c as Newt;
|
||||||
|
|
||||||
@@ -27,7 +29,7 @@ export const handleNewtDisconnectingMessage: MessageHandler = async (context) =>
|
|||||||
.set({
|
.set({
|
||||||
online: false
|
online: false
|
||||||
})
|
})
|
||||||
.where(eq(sites.siteId, sites.siteId));
|
.where(eq(sites.siteId, newt.siteId));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error handling disconnecting message", { error });
|
logger.error("Error handling disconnecting message", { error });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ async function getLatestNewtVersion(): Promise<string | null> {
|
|||||||
tags = tags.filter((version) => !version.name.includes("rc"));
|
tags = tags.filter((version) => !version.name.includes("rc"));
|
||||||
const latestVersion = tags[0].name;
|
const latestVersion = tags[0].name;
|
||||||
|
|
||||||
await cache.set("latestNewtVersion", latestVersion);
|
await cache.set("latestNewtVersion", latestVersion, 3600);
|
||||||
|
|
||||||
return latestVersion;
|
return latestVersion;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -180,7 +180,7 @@ registry.registerPath({
|
|||||||
method: "get",
|
method: "get",
|
||||||
path: "/org/{orgId}/sites",
|
path: "/org/{orgId}/sites",
|
||||||
description: "List all sites in an organization",
|
description: "List all sites in an organization",
|
||||||
tags: [OpenAPITags.Site],
|
tags: [OpenAPITags.Org, OpenAPITags.Site],
|
||||||
request: {
|
request: {
|
||||||
params: listSitesParamsSchema,
|
params: listSitesParamsSchema,
|
||||||
query: listSitesSchema
|
query: listSitesSchema
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ const createSiteResourceSchema = z
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
message:
|
message:
|
||||||
"Destination must be a valid IP address or valid domain AND alias is required"
|
"Destination must be a valid IPV4 address or valid domain AND alias is required"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.refine(
|
.refine(
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import { updatePeerData, updateTargets } from "@server/routers/client/targets";
|
|||||||
import {
|
import {
|
||||||
generateAliasConfig,
|
generateAliasConfig,
|
||||||
generateRemoteSubnets,
|
generateRemoteSubnets,
|
||||||
generateSubnetProxyTargets,
|
generateSubnetProxyTargetV2,
|
||||||
isIpInCidr,
|
isIpInCidr,
|
||||||
portRangeStringSchema
|
portRangeStringSchema
|
||||||
} from "@server/lib/ip";
|
} from "@server/lib/ip";
|
||||||
@@ -608,18 +608,18 @@ export async function handleMessagingForUpdatedSiteResource(
|
|||||||
|
|
||||||
// Only update targets on newt if destination changed
|
// Only update targets on newt if destination changed
|
||||||
if (destinationChanged || portRangesChanged) {
|
if (destinationChanged || portRangesChanged) {
|
||||||
const oldTargets = generateSubnetProxyTargets(
|
const oldTarget = generateSubnetProxyTargetV2(
|
||||||
existingSiteResource,
|
existingSiteResource,
|
||||||
mergedAllClients
|
mergedAllClients
|
||||||
);
|
);
|
||||||
const newTargets = generateSubnetProxyTargets(
|
const newTarget = generateSubnetProxyTargetV2(
|
||||||
updatedSiteResource,
|
updatedSiteResource,
|
||||||
mergedAllClients
|
mergedAllClients
|
||||||
);
|
);
|
||||||
|
|
||||||
await updateTargets(newt.newtId, {
|
await updateTargets(newt.newtId, {
|
||||||
oldTargets: oldTargets,
|
oldTargets: oldTarget ? [oldTarget] : [],
|
||||||
newTargets: newTargets
|
newTargets: newTarget ? [newTarget] : []
|
||||||
}, newt.version);
|
}, newt.version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ export async function inviteUser(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await cache.set(email, attempts + 1);
|
await cache.set("regenerateInvite:" + email, attempts + 1, 3600);
|
||||||
|
|
||||||
const inviteId = existingInvite[0].inviteId; // Retrieve the original inviteId
|
const inviteId = existingInvite[0].inviteId; // Retrieve the original inviteId
|
||||||
const token = generateRandomString(
|
const token = generateRandomString(
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import { usePathname, useRouter } from "next/navigation";
|
|||||||
import { useMemo, useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import { useUserContext } from "@app/hooks/useUserContext";
|
import { useUserContext } from "@app/hooks/useUserContext";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
|
import { build } from "@server/build";
|
||||||
|
|
||||||
interface OrgSelectorProps {
|
interface OrgSelectorProps {
|
||||||
orgId?: string;
|
orgId?: string;
|
||||||
@@ -50,6 +51,11 @@ export function OrgSelector({
|
|||||||
|
|
||||||
const selectedOrg = orgs?.find((org) => org.orgId === orgId);
|
const selectedOrg = orgs?.find((org) => org.orgId === orgId);
|
||||||
|
|
||||||
|
let canCreateOrg = !env.flags.disableUserCreateOrg || user.serverAdmin;
|
||||||
|
if (build === "saas" && user.type !== "internal") {
|
||||||
|
canCreateOrg = false;
|
||||||
|
}
|
||||||
|
|
||||||
const sortedOrgs = useMemo(() => {
|
const sortedOrgs = useMemo(() => {
|
||||||
if (!orgs?.length) return orgs ?? [];
|
if (!orgs?.length) return orgs ?? [];
|
||||||
return [...orgs].sort((a, b) => {
|
return [...orgs].sort((a, b) => {
|
||||||
@@ -161,7 +167,7 @@ export function OrgSelector({
|
|||||||
</CommandGroup>
|
</CommandGroup>
|
||||||
</CommandList>
|
</CommandList>
|
||||||
</Command>
|
</Command>
|
||||||
{(!env.flags.disableUserCreateOrg || user.serverAdmin) && (
|
{canCreateOrg && (
|
||||||
<div className="p-2 border-t border-border">
|
<div className="p-2 border-t border-border">
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
|
|||||||
Reference in New Issue
Block a user