Skip to content

Commit

Permalink
Merge pull request #64 from GSWXXN/main
Browse files Browse the repository at this point in the history
add fallbacks feature & flow control mode for trojan
  • Loading branch information
vaxilu committed Aug 25, 2021
2 parents 698bf1d + 100bd29 commit 581e3b8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 9 deletions.
77 changes: 69 additions & 8 deletions web/assets/js/model/xray.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const RULE_DOMAIN = {
SPEEDTEST: 'geosite:speedtest',
};

const VLESS_FLOW = {
const FLOW_CONTROL = {
ORIGIN: "xtls-rprx-origin",
DIRECT: "xtls-rprx-direct",
};
Expand All @@ -50,7 +50,7 @@ Object.freeze(VmessMethods);
Object.freeze(SSMethods);
Object.freeze(RULE_IP);
Object.freeze(RULE_DOMAIN);
Object.freeze(VLESS_FLOW);
Object.freeze(FLOW_CONTROL);

class XrayCommonClass {

Expand Down Expand Up @@ -697,11 +697,13 @@ class Inbound extends XrayCommonClass {
}
}

// VLess
// VLess & Trojan
get flow() {
switch (this.protocol) {
case Protocols.VLESS:
return this.settings.vlesses[0].flow;
case Protocols.TROJAN:
return this.settings.clients[0].flow;
default:
return "";
}
Expand Down Expand Up @@ -1217,7 +1219,7 @@ Inbound.VLESSSettings = class extends Inbound.Settings {
};
Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {

constructor(id=RandomUtil.randomUUID(), flow=VLESS_FLOW.DIRECT) {
constructor(id=RandomUtil.randomUUID(), flow=FLOW_CONTROL.DIRECT) {
super();
this.id = id;
this.flow = flow;
Expand Down Expand Up @@ -1270,14 +1272,26 @@ Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
};

Inbound.TrojanSettings = class extends Inbound.Settings {
constructor(protocol, clients=[new Inbound.TrojanSettings.Client()]) {
constructor(protocol,
clients=[new Inbound.TrojanSettings.Client()],
fallbacks=[],) {
super(protocol);
this.clients = clients;
this.fallbacks = fallbacks;
}

addTrojanFallback() {
this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
}

delTrojanFallback(index) {
this.fallbacks.splice(index, 1);
}

toJson() {
return {
clients: Inbound.TrojanSettings.toJsonArray(this.clients),
fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
};
}

Expand All @@ -1286,25 +1300,72 @@ Inbound.TrojanSettings = class extends Inbound.Settings {
for (const c of json.clients) {
clients.push(Inbound.TrojanSettings.Client.fromJson(c));
}
return new Inbound.TrojanSettings(Protocols.TROJAN, clients);
return new Inbound.TrojanSettings(
Protocols.TROJAN,
clients,
Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
}
};
Inbound.TrojanSettings.Client = class extends XrayCommonClass {
constructor(password=RandomUtil.randomSeq(10)) {
constructor(password=RandomUtil.randomSeq(10), flow=FLOW_CONTROL.DIRECT) {
super();
this.password = password;
this.flow = flow;
}

toJson() {
return {
password: this.password,
flow: this.flow,
};
}

static fromJson(json={}) {
return new Inbound.TrojanSettings.Client(json.password);
return new Inbound.TrojanSettings.Client(
json.password,
json.flow,
);
}

};

Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
constructor(name="", alpn='', path='', dest='', xver=0) {
super();
this.name = name;
this.alpn = alpn;
this.path = path;
this.dest = dest;
this.xver = xver;
}

toJson() {
let xver = this.xver;
if (!Number.isInteger(xver)) {
xver = 0;
}
return {
name: this.name,
alpn: this.alpn,
path: this.path,
dest: this.dest,
xver: xver,
}
}

static fromJson(json=[]) {
const fallbacks = [];
for (let fallback of json) {
fallbacks.push(new Inbound.TrojanSettings.Fallback(
fallback.name,
fallback.alpn,
fallback.path,
fallback.dest,
fallback.xver,
))
}
return fallbacks;
}
};

Inbound.ShadowsocksSettings = class extends Inbound.Settings {
Expand Down
42 changes: 42 additions & 0 deletions web/html/xui/form/protocol/trojan.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,47 @@
<a-form-item label="密码">
<a-input v-model.trim="inbound.settings.clients[0].password"></a-input>
</a-form-item>
<a-form-item v-if="inbound.xtls" label="flow">
<a-select v-model="inbound.settings.clients[0].flow" style="width: 150px">
<a-select-option value=""></a-select-option>
<a-select-option v-for="key in FLOW_CONTROL" :value="key">[[ key ]]</a-select-option>
</a-select>
</a-form-item>
</a-form>

<a-form layout="inline">
<a-form-item label="fallbacks">
<a-row>
<a-button type="primary" size="small"
@click="inbound.settings.addTrojanFallback()">
+
</a-button>
</a-row>
</a-form-item>
</a-form>

<!-- trojan fallbacks -->
<a-form v-for="(fallback, index) in inbound.settings.fallbacks" layout="inline">
<a-divider>
fallback[[ index + 1 ]]
<a-icon type="delete" @click="() => inbound.settings.delTrojanFallback(index)"
style="color: rgb(255, 77, 79);cursor: pointer;"/>
</a-divider>
<a-form-item label="name">
<a-input v-model="fallback.name"></a-input>
</a-form-item>
<a-form-item label="alpn">
<a-input v-model="fallback.alpn"></a-input>
</a-form-item>
<a-form-item label="path">
<a-input v-model="fallback.path"></a-input>
</a-form-item>
<a-form-item label="dest">
<a-input v-model="fallback.dest"></a-input>
</a-form-item>
<a-form-item label="xver">
<a-input type="number" v-model.number="fallback.xver"></a-input>
</a-form-item>
<a-divider v-if="inbound.settings.fallbacks.length - 1 === index"/>
</a-form>
{{end}}
2 changes: 1 addition & 1 deletion web/html/xui/form/protocol/vless.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<a-form-item v-if="inbound.xtls" label="flow">
<a-select v-model="inbound.settings.vlesses[0].flow" style="width: 150px">
<a-select-option value=""></a-select-option>
<a-select-option v-for="key in VLESS_FLOW" :value="key">[[ key ]]</a-select-option>
<a-select-option v-for="key in FLOW_CONTROL" :value="key">[[ key ]]</a-select-option>
</a-select>
</a-form-item>
</a-form>
Expand Down

0 comments on commit 581e3b8

Please sign in to comment.