improve randomShortId , format document

This commit is contained in:
mhsanaei 2024-08-06 17:06:39 +02:00
parent 2d2b30daf1
commit b8c3555b09
4 changed files with 468 additions and 379 deletions

View file

@ -72,10 +72,12 @@ const WireguardDomainStrategy = [
Object.freeze(Protocols); Object.freeze(Protocols);
Object.freeze(SSMethods); Object.freeze(SSMethods);
Object.freeze(TLS_FLOW_CONTROL); Object.freeze(TLS_FLOW_CONTROL);
Object.freeze(UTLS_FINGERPRINT);
Object.freeze(ALPN_OPTION); Object.freeze(ALPN_OPTION);
Object.freeze(OutboundDomainStrategies); Object.freeze(OutboundDomainStrategies);
Object.freeze(WireguardDomainStrategy); Object.freeze(WireguardDomainStrategy);
class CommonClass { class CommonClass {
static toJsonArray(arr) { static toJsonArray(arr) {
@ -132,7 +134,9 @@ class TcpStreamSettings extends CommonClass {
} }
class KcpStreamSettings extends CommonClass { class KcpStreamSettings extends CommonClass {
constructor(mtu=1350, tti=20, constructor(
mtu = 1350,
tti = 50,
uplinkCapacity = 5, uplinkCapacity = 5,
downlinkCapacity = 20, downlinkCapacity = 20,
congestion = false, congestion = false,
@ -229,8 +233,11 @@ class HttpStreamSettings extends CommonClass {
} }
class QuicStreamSettings extends CommonClass { class QuicStreamSettings extends CommonClass {
constructor(security='none', constructor(
key='', type='none') { security = 'none',
key = '',
type = 'none'
) {
super(); super();
this.security = security; this.security = security;
this.key = key; this.key = key;
@ -257,7 +264,11 @@ class QuicStreamSettings extends CommonClass {
} }
class GrpcStreamSettings extends CommonClass { class GrpcStreamSettings extends CommonClass {
constructor(serviceName="", authority="", multiMode=false) { constructor(
serviceName = "",
authority = "",
multiMode = false
) {
super(); super();
this.serviceName = serviceName; this.serviceName = serviceName;
this.authority = authority; this.authority = authority;
@ -322,10 +333,12 @@ class SplitHTTPStreamSettings extends CommonClass {
} }
class TlsStreamSettings extends CommonClass { class TlsStreamSettings extends CommonClass {
constructor(serverName='', constructor(
serverName = '',
alpn = [], alpn = [],
fingerprint = '', fingerprint = '',
allowInsecure = false) { allowInsecure = false
) {
super(); super();
this.serverName = serverName; this.serverName = serverName;
this.alpn = alpn; this.alpn = alpn;
@ -353,7 +366,13 @@ class TlsStreamSettings extends CommonClass {
} }
class RealityStreamSettings extends CommonClass { class RealityStreamSettings extends CommonClass {
constructor(publicKey = '', fingerprint = '', serverName = '', shortId = '', spiderX = '/') { constructor(
publicKey = '',
fingerprint = '',
serverName = '',
shortId = '',
spiderX = '/'
) {
super(); super();
this.publicKey = publicKey; this.publicKey = publicKey;
this.fingerprint = fingerprint; this.fingerprint = fingerprint;
@ -381,7 +400,13 @@ class RealityStreamSettings extends CommonClass {
} }
}; };
class SockoptStreamSettings extends CommonClass { class SockoptStreamSettings extends CommonClass {
constructor(dialerProxy = "", tcpFastOpen = false, tcpKeepAliveInterval = 0, tcpMptcp = false, tcpNoDelay = false) { constructor(
dialerProxy = "",
tcpFastOpen = false,
tcpKeepAliveInterval = 0,
tcpMptcp = false,
tcpNoDelay = false
) {
super(); super();
this.dialerProxy = dialerProxy; this.dialerProxy = dialerProxy;
this.tcpFastOpen = tcpFastOpen; this.tcpFastOpen = tcpFastOpen;
@ -413,7 +438,8 @@ class SockoptStreamSettings extends CommonClass {
} }
class StreamSettings extends CommonClass { class StreamSettings extends CommonClass {
constructor(network='tcp', constructor(
network = 'tcp',
security = 'none', security = 'none',
tlsSettings = new TlsStreamSettings(), tlsSettings = new TlsStreamSettings(),
realitySettings = new RealityStreamSettings(), realitySettings = new RealityStreamSettings(),
@ -1082,17 +1108,23 @@ Outbound.HttpSettings = class extends CommonClass {
Outbound.WireguardSettings = class extends CommonClass { Outbound.WireguardSettings = class extends CommonClass {
constructor( constructor(
mtu=1420, secretKey='', mtu = 1420,
address=[''], workers=2, domainStrategy='', reserved='', secretKey = '',
peers=[new Outbound.WireguardSettings.Peer()], kernelMode=false) { address = [''],
workers = 2,
domainStrategy = '',
reserved = '',
peers = [new Outbound.WireguardSettings.Peer()],
kernelMode = false
) {
super(); super();
this.mtu = mtu; this.mtu = mtu;
this.secretKey = secretKey; this.secretKey = secretKey;
this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : ''; this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
this.address = address instanceof Array ? address.join(',') : address; this.address = Array.isArray(address) ? address.join(',') : address;
this.workers = workers; this.workers = workers;
this.domainStrategy = domainStrategy; this.domainStrategy = domainStrategy;
this.reserved = reserved instanceof Array ? reserved.join(',') : reserved; this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
this.peers = peers; this.peers = peers;
this.kernelMode = kernelMode; this.kernelMode = kernelMode;
} }
@ -1133,7 +1165,13 @@ Outbound.WireguardSettings = class extends CommonClass {
}; };
Outbound.WireguardSettings.Peer = class extends CommonClass { Outbound.WireguardSettings.Peer = class extends CommonClass {
constructor(publicKey='', psk='', allowedIPs=['0.0.0.0/0','::/0'], endpoint='', keepAlive=0) { constructor(
publicKey = '',
psk = '',
allowedIPs = ['0.0.0.0/0', '::/0'],
endpoint = '',
keepAlive = 0
) {
super(); super();
this.publicKey = publicKey; this.publicKey = publicKey;
this.psk = psk; this.psk = psk;

View file

@ -26,7 +26,7 @@ class AllSetting {
this.secretEnable = false; this.secretEnable = false;
this.subEnable = false; this.subEnable = false;
this.subListen = ""; this.subListen = "";
this.subPort = "2096"; this.subPort = 2096;
this.subPath = "/sub/"; this.subPath = "/sub/";
this.subJsonPath = "/json/"; this.subJsonPath = "/json/";
this.subDomain = ""; this.subDomain = "";

View file

@ -103,6 +103,7 @@ const DOMAIN_STRATEGY_OPTION = {
FORCE_IPV4V6: "ForceIPv4v6", FORCE_IPV4V6: "ForceIPv4v6",
FORCE_IPV4: "ForceIPv4", FORCE_IPV4: "ForceIPv4",
}; };
const TCP_CONGESTION_OPTION = { const TCP_CONGESTION_OPTION = {
BBR: "bbr", BBR: "bbr",
CUBIC: "cubic", CUBIC: "cubic",
@ -180,7 +181,8 @@ class XrayCommonClass {
} }
class TcpStreamSettings extends XrayCommonClass { class TcpStreamSettings extends XrayCommonClass {
constructor(acceptProxyProtocol=false, constructor(
acceptProxyProtocol = false,
type = 'none', type = 'none',
request = new TcpStreamSettings.TcpRequest(), request = new TcpStreamSettings.TcpRequest(),
response = new TcpStreamSettings.TcpResponse(), response = new TcpStreamSettings.TcpResponse(),
@ -217,7 +219,8 @@ class TcpStreamSettings extends XrayCommonClass {
} }
TcpStreamSettings.TcpRequest = class extends XrayCommonClass { TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
constructor(version='1.1', constructor(
version = '1.1',
method = 'GET', method = 'GET',
path = ['/'], path = ['/'],
headers = [], headers = [],
@ -265,7 +268,8 @@ TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
}; };
TcpStreamSettings.TcpResponse = class extends XrayCommonClass { TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
constructor(version='1.1', constructor(
version = '1.1',
status = '200', status = '200',
reason = 'OK', reason = 'OK',
headers = [], headers = [],
@ -305,7 +309,9 @@ TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
}; };
class KcpStreamSettings extends XrayCommonClass { class KcpStreamSettings extends XrayCommonClass {
constructor(mtu=1350, tti=20, constructor(
mtu = 1350,
tti = 50,
uplinkCapacity = 5, uplinkCapacity = 5,
downlinkCapacity = 20, downlinkCapacity = 20,
congestion = false, congestion = false,
@ -358,7 +364,12 @@ class KcpStreamSettings extends XrayCommonClass {
} }
class WsStreamSettings extends XrayCommonClass { class WsStreamSettings extends XrayCommonClass {
constructor(acceptProxyProtocol=false, path='/', host='', headers=[]) { constructor(
acceptProxyProtocol = false,
path = '/',
host = '',
headers = []
) {
super(); super();
this.acceptProxyProtocol = acceptProxyProtocol; this.acceptProxyProtocol = acceptProxyProtocol;
this.path = path; this.path = path;
@ -430,8 +441,11 @@ class HttpStreamSettings extends XrayCommonClass {
} }
class QuicStreamSettings extends XrayCommonClass { class QuicStreamSettings extends XrayCommonClass {
constructor(security='none', constructor(
key=RandomUtil.randomSeq(10), type='none') { security = 'none',
key = RandomUtil.randomSeq(10),
type = 'none'
) {
super(); super();
this.security = security; this.security = security;
this.key = key; this.key = key;
@ -487,7 +501,12 @@ class GrpcStreamSettings extends XrayCommonClass {
} }
class HTTPUpgradeStreamSettings extends XrayCommonClass { class HTTPUpgradeStreamSettings extends XrayCommonClass {
constructor(acceptProxyProtocol=false, path='/', host='', headers=[]) { constructor(
acceptProxyProtocol = false,
path = '/',
host = '',
headers = []
) {
super(); super();
this.acceptProxyProtocol = acceptProxyProtocol; this.acceptProxyProtocol = acceptProxyProtocol;
this.path = path; this.path = path;
@ -576,7 +595,8 @@ class SplitHTTPStreamSettings extends XrayCommonClass {
} }
class TlsStreamSettings extends XrayCommonClass { class TlsStreamSettings extends XrayCommonClass {
constructor(serverName='', constructor(
serverName = '',
minVersion = TLS_VERSION_OPTION.TLS12, minVersion = TLS_VERSION_OPTION.TLS12,
maxVersion = TLS_VERSION_OPTION.TLS13, maxVersion = TLS_VERSION_OPTION.TLS13,
cipherSuites = '', cipherSuites = '',
@ -585,7 +605,8 @@ class TlsStreamSettings extends XrayCommonClass {
enableSessionResumption = false, enableSessionResumption = false,
certificates = [new TlsStreamSettings.Cert()], certificates = [new TlsStreamSettings.Cert()],
alpn = [ALPN_OPTION.H3, ALPN_OPTION.H2, ALPN_OPTION.HTTP1], alpn = [ALPN_OPTION.H3, ALPN_OPTION.H2, ALPN_OPTION.HTTP1],
settings=new TlsStreamSettings.Settings()) { settings = new TlsStreamSettings.Settings()
) {
super(); super();
this.sni = serverName; this.sni = serverName;
this.minVersion = minVersion; this.minVersion = minVersion;
@ -663,8 +684,8 @@ TlsStreamSettings.Cert = class extends XrayCommonClass {
this.useFile = useFile; this.useFile = useFile;
this.certFile = certificateFile; this.certFile = certificateFile;
this.keyFile = keyFile; this.keyFile = keyFile;
this.cert = certificate instanceof Array ? certificate.join('\n') : certificate; this.cert = Array.isArray(certificate) ? certificate.join('\n') : certificate;
this.key = key instanceof Array ? key.join('\n') : key; this.key = Array.isArray(key) ? key.join('\n') : key;
this.ocspStapling = ocspStapling; this.ocspStapling = ocspStapling;
this.oneTimeLoading = oneTimeLoading; this.oneTimeLoading = oneTimeLoading;
this.usage = usage; this.usage = usage;
@ -739,10 +760,12 @@ TlsStreamSettings.Settings = class extends XrayCommonClass {
}; };
class XtlsStreamSettings extends XrayCommonClass { class XtlsStreamSettings extends XrayCommonClass {
constructor(serverName='', constructor(
serverName = '',
certificates = [new XtlsStreamSettings.Cert()], certificates = [new XtlsStreamSettings.Cert()],
alpn = [ALPN_OPTION.H3, ALPN_OPTION.H2, ALPN_OPTION.HTTP1], alpn = [ALPN_OPTION.H3, ALPN_OPTION.H2, ALPN_OPTION.HTTP1],
settings=new XtlsStreamSettings.Settings()) { settings = new XtlsStreamSettings.Settings()
) {
super(); super();
this.sni = serverName; this.sni = serverName;
this.certs = certificates; this.certs = certificates;
@ -787,13 +810,22 @@ class XtlsStreamSettings extends XrayCommonClass {
} }
XtlsStreamSettings.Cert = class extends XrayCommonClass { XtlsStreamSettings.Cert = class extends XrayCommonClass {
constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='', ocspStapling=3600, oneTimeLoading=false, usage=USAGE_OPTION.ENCIPHERMENT) { constructor(
useFile = true,
certificateFile = '',
keyFile = '',
certificate = '',
key = '',
ocspStapling = 3600,
oneTimeLoading = false,
usage = USAGE_OPTION.ENCIPHERMENT
) {
super(); super();
this.useFile = useFile; this.useFile = useFile;
this.certFile = certificateFile; this.certFile = certificateFile;
this.keyFile = keyFile; this.keyFile = keyFile;
this.cert = certificate instanceof Array ? certificate.join('\n') : certificate; this.cert = Array.isArray(certificate) ? certificate.join('\n') : certificate;
this.key = key instanceof Array ? key.join('\n') : key; this.key = Array.isArray(key) ? key.join('\n') : key;
this.ocspStapling = ocspStapling; this.ocspStapling = ocspStapling;
this.oneTimeLoading = oneTimeLoading; this.oneTimeLoading = oneTimeLoading;
this.usage = usage; this.usage = usage;
@ -860,9 +892,9 @@ XtlsStreamSettings.Settings = class extends XrayCommonClass {
}; };
class RealityStreamSettings extends XrayCommonClass { class RealityStreamSettings extends XrayCommonClass {
constructor( constructor(
show = false,xver = 0, show = false,
xver = 0,
dest = 'yahoo.com:443', dest = 'yahoo.com:443',
serverNames = 'yahoo.com,www.yahoo.com', serverNames = 'yahoo.com,www.yahoo.com',
privateKey = '', privateKey = '',
@ -876,20 +908,23 @@ class RealityStreamSettings extends XrayCommonClass {
this.show = show; this.show = show;
this.xver = xver; this.xver = xver;
this.dest = dest; this.dest = dest;
this.serverNames = serverNames instanceof Array ? serverNames.join(",") : serverNames; this.serverNames = Array.isArray(serverNames) ? serverNames.join(",") : serverNames;
this.privateKey = privateKey; this.privateKey = privateKey;
this.minClient = minClient; this.minClient = minClient;
this.maxClient = maxClient; this.maxClient = maxClient;
this.maxTimediff = maxTimediff; this.maxTimediff = maxTimediff;
this.shortIds = shortIds instanceof Array ? shortIds.join(",") : shortIds; this.shortIds = Array.isArray(shortIds) ? shortIds.join(",") : shortIds;
this.settings = settings; this.settings = settings;
} }
static fromJson(json = {}) { static fromJson(json = {}) {
let settings; let settings;
if (!ObjectUtil.isEmpty(json.settings)) { if (!ObjectUtil.isEmpty(json.settings)) {
settings = new RealityStreamSettings.Settings(json.settings.publicKey , json.settings.fingerprint, json.settings.serverName, json.settings.spiderX); settings = new RealityStreamSettings.Settings(
} json.settings.publicKey,
json.settings.fingerprint,
json.settings.serverName,
json.settings.spiderX);}
return new RealityStreamSettings( return new RealityStreamSettings(
json.show, json.show,
json.xver, json.xver,
@ -902,8 +937,8 @@ class RealityStreamSettings extends XrayCommonClass {
json.shortIds, json.shortIds,
json.settings, json.settings,
); );
} }
toJson() { toJson() {
return { return {
show: this.show, show: this.show,
@ -921,7 +956,12 @@ class RealityStreamSettings extends XrayCommonClass {
} }
RealityStreamSettings.Settings = class extends XrayCommonClass { RealityStreamSettings.Settings = class extends XrayCommonClass {
constructor(publicKey = '', fingerprint = UTLS_FINGERPRINT.UTLS_RANDOM, serverName = '', spiderX= '/') { constructor(
publicKey = '',
fingerprint = UTLS_FINGERPRINT.UTLS_RANDOM,
serverName = '',
spiderX = '/'
) {
super(); super();
this.publicKey = publicKey; this.publicKey = publicKey;
this.fingerprint = fingerprint; this.fingerprint = fingerprint;
@ -1180,7 +1220,8 @@ class Sniffing extends XrayCommonClass {
} }
class Inbound extends XrayCommonClass { class Inbound extends XrayCommonClass {
constructor(port=RandomUtil.randomIntRange(10000, 60000), constructor(
port = RandomUtil.randomIntRange(10000, 60000),
listen = '', listen = '',
protocol = Protocols.VLESS, protocol = Protocols.VLESS,
settings = null, settings = null,

View file

@ -100,11 +100,21 @@ class RandomUtil {
} }
static randomShortId() { static randomShortId() {
let str = ''; const lengths = [2, 4, 6, 8, 10, 12, 14, 16];
for (let i = 0; i < 8; ++i) { for (let i = lengths.length - 1; i > 0; i--) {
str += seq[this.randomInt(16)]; const j = Math.floor(Math.random() * (i + 1));
[lengths[i], lengths[j]] = [lengths[j], lengths[i]];
} }
return str;
let shortIds = [];
for (let length of lengths) {
let shortId = '';
for (let i = 0; i < length; i++) {
shortId += seq[this.randomInt(16)];
}
shortIds.push(shortId);
}
return shortIds;
} }
static randomLowerAndNum(len) { static randomLowerAndNum(len) {