Skip to content

Commit

Permalink
Update xray.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rkygogo committed Jul 4, 2022
1 parent 8d9285c commit 805fb8e
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions web/assets/js/model/xray.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,75 @@ class Inbound extends XrayCommonClass {

genTrojanLink(address = '', remark = '') {
let settings = this.settings;
return `trojan:https://${settings.clients[0].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
const port = this.port;
const type = this.stream.network;
const params = new Map();
params.set("type", this.stream.network);
if (this.xtls) {
params.set("security", "xtls");
} else {
params.set("security", this.stream.security);
}
switch (type) {
case "tcp":
const tcp = this.stream.tcp;
if (tcp.type === 'http') {
const request = tcp.request;
params.set("path", request.path.join(','));
const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
if (index >= 0) {
const host = request.headers[index].value;
params.set("host", host);
}
}
break;
case "kcp":
const kcp = this.stream.kcp;
params.set("headerType", kcp.type);
params.set("seed", kcp.seed);
break;
case "ws":
const ws = this.stream.ws;
params.set("path", ws.path);
const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
if (index >= 0) {
const host = ws.headers[index].value;
params.set("host", host);
}
break;
case "http":
const http = this.stream.http;
params.set("path", http.path);
params.set("host", http.host);
break;
case "quic":
const quic = this.stream.quic;
params.set("quicSecurity", quic.security);
params.set("key", quic.key);
params.set("headerType", quic.type);
break;
case "grpc":
const grpc = this.stream.grpc;
params.set("serviceName", grpc.serviceName);
break;
}

if (this.stream.security === 'tls') {
if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
address = this.stream.tls.server;
params.set("sni", address);
}
}
if (this.xtls) {
params.set("flow", this.settings.clients[0].flow);
}
const link = `trojan:https://${settings.clients[0].password}@${address}:${port}`;
const url = new URL(link);
for (const [key, value] of params) {
url.searchParams.set(key, value)
}
url.hash = encodeURIComponent(remark);
return url.toString();
}

genLink(address = '', remark = '') {
Expand Down Expand Up @@ -1541,4 +1609,4 @@ Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
static fromJson(json = {}) {
return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
}
};
};

0 comments on commit 805fb8e

Please sign in to comment.