@alltiptop/geoip-3xui-rules
Version:
Middleware server to set routing rules by countries for XRAY
66 lines (65 loc) • 2.42 kB
TypeScript
export type JsonValue = string | number | boolean | JsonValue[] | {
[key: string]: JsonValue;
};
export type JsonOptions = {
[key: string]: JsonValue;
};
export interface XrayRule {
type: 'field';
ip?: string[];
domain?: string[];
outboundTag: string;
remarks?: string;
}
export interface XuiOptions {
/** URL of the upstream 3x-ui endpoint (without trailing slash). */
panelAddress: string;
/** API Token of the 3x-ui panel. */
token: string;
/** Inbound IDs of the 3x-ui panel. */
inboundIds: Array<number | string>;
/** Debug mode. */
debug?: boolean;
}
export type QueryType = Record<string, string | string[] | undefined>;
export interface TransformProps {
json: JsonOptions;
iso: string;
subId: string;
isEU: boolean;
query?: QueryType;
requestHeaders?: Record<string, string | string[] | undefined>;
}
export interface TransformResponse {
transformed: JsonOptions;
headers: Record<string, string>;
}
export interface CreateServerProps {
/** URL of the upstream 3x-ui endpoint (without trailing slash). */
upstreamUrl: string;
/** Secret path segment protecting this proxy, e.g. `abc123` → `/abc123/json/:id` */
secretUrl: string;
/** Directory with JSON rule presets (`RU.json`, `EU.json`, `BASE.json` …). */
rulesDir: string;
/** Directory with JSON overrides presets (`RU.json`, `EU.json`, `BASE.json` …). */
overridesDir?: string;
/** Inject direct‑route rules for the requester’s own country. */
directSameCountry?: boolean;
/** Enable Fastify logger. */
logger?: boolean;
/** Public Domain URL of the service. */
publicURL?: string;
/** Options for the 3x-ui panel. */
xuiOptions?: XuiOptions;
/**
* Transform the JSON before sending it to the client.
* @param {Object} props.json - The JSON object to transform.
* @param {String} props.iso - The ISO code of the country of the requester.
* @param {String} props.subId - The subscription ID of the requester.
* @param {Boolean} props.isEU - Whether the requester is in the Europe Union.
* @param {Object} props.query - Query params for subscription
* @param {Object} props.requestHeaders - Original request headers params
* @returns The transformed JSON object.
*/
transform?: (props: TransformProps) => Promise<TransformResponse>;
}