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

View file

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

View file

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

View file

@ -100,11 +100,21 @@ class RandomUtil {
}
static randomShortId() {
let str = '';
for (let i = 0; i < 8; ++i) {
str += seq[this.randomInt(16)];
const lengths = [2, 4, 6, 8, 10, 12, 14, 16];
for (let i = lengths.length - 1; i > 0; i--) {
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) {