Skip to content

Commit

Permalink
dns management
Browse files Browse the repository at this point in the history
  • Loading branch information
whalechoi committed Nov 8, 2024
1 parent b3da834 commit 7fab665
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 4 deletions.
115 changes: 113 additions & 2 deletions src/Manage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@
:model-value="value" @update:model-value="v => rulesetResult[currentRulesetResult][key] = v"/>
</van-list>
</van-popup>
<!-- dns manage -->
<van-popup v-model:show="dnsManage" round :style="{ width: '90%' ,maxHeight:'85%'}">
<van-cell :title="$t('manage.dns-manage')" title-style="max-width:100%;">
<template #right-icon>
<van-icon size="1.2rem" name="plus" @click="addDns"/>
</template>
</van-cell>
<van-cell-group>
<van-cell v-for="(item, index) in dnsResult" :key="index" center>
<template #title>
<span class="custom-title">{{ coreType==='xray'?item.address:item.tag }}</span>
</template>
<template #value>
<van-space>
<van-button plain hairline type="default" size="small" icon="edit" @click="editDns(index)"/>
<van-button plain hairline type="default" size="small" icon="cross" @click="deleteDns(index)"/>
</van-space>
</template>
</van-cell>
</van-cell-group>
</van-popup>
<!-- dns editor -->
<van-popup v-model:show="dnsEditor" round :style="{ width: '90%' ,maxHeight:'85%'}" @closed="saveDns">
<van-list>
<van-field v-for="(value, key) in dnsField"
type="textarea" autosize :label="key + ':'" labelWidth="7em"
:model-value="value" @update:model-value="v => dnsResult[currentDnsResult][key] = v"/>
</van-list>
</van-popup>
<!-- 负载均衡的时候出现 -->
<!-- <van-floating-bubble icon="checked" @click="onClick" /> -->
</div>
Expand All @@ -159,15 +188,21 @@ import {callApi, readFile, saveFile, XRAYHELPER_CONFIG} from "./tools.js"
import {
newRuleObject as newRuleObjectXray,
parseRuleObject as parseRuleObjectXray,
standardizeRuleObject as standardizeRuleObjectXray
standardizeRuleObject as standardizeRuleObjectXray,
newDnsObject as newDnsObjectXray,
parseDnsObject as parseDnsObjectXray,
standardizeDnsObject as standardizeDnsObjectXray,
} from "./xray.js";
import {
newRuleObject as newRuleObjectSingbox,
parseRuleObject as parseRuleObjectSingbox,
standardizeRuleObject as standardizeRuleObjectSingbox,
newRulesetObject as newRulesetObjectSingbox,
parseRulesetObject as parseRulesetObjectSingbox,
standardizeRulesetObject as standardizeRulesetObjectSingbox
standardizeRulesetObject as standardizeRulesetObjectSingbox,
newDnsObject as newDnsObjectSingbox,
parseDnsObject as parseDnsObjectSingbox,
standardizeDnsObject as standardizeDnsObjectSingbox,
} from "./sing-box.js";
import {Buffer} from "buffer";

Expand All @@ -178,6 +213,7 @@ const coreType = ref("")
const showMenu = ref(false);
const actions = [
{text: i18n.global.t('manage.edit-custom'), value: 'edit-custom', disabled: false},
{text: i18n.global.t('manage.dns-manage'), value: 'dns', disabled: false},
{text: i18n.global.t('manage.rule-manage'), value: 'rule', disabled: false},
//{text: i18n.global.t('manage.load-balancing'), value: 'balancing', disabled: true},
//{text: i18n.global.t('manage.more-setting'), value: 'setting', disabled: true},
Expand All @@ -190,6 +226,8 @@ const onSelect = (action) => {
showRuleManage()
} else if (action.value === 'ruleset') {
showRulesetManage()
} else if (action.value === 'dns') {
showDnsManage()
}
}
const loading = ref(false);
Expand Down Expand Up @@ -413,6 +451,78 @@ const saveRuleset = async () => {
currentRulesetResult.value = 0
showRulesetManage()
}
// dns
const dnsResult = ref([])
const currentDnsResult = ref(0)
const dnsField = computed(() => {
let result = {}
Object.assign(result, dnsResult.value[currentDnsResult.value])
delete result["index"]
delete result["newDns"]
return result
})
const dnsManage = ref(false)
const dnsEditor = ref(false)
const showDnsManage = () => {
dnsManage.value = true
callApi(`get dns`).then(value => {
dnsResult.value = value.result
for (let i = 0; i < dnsResult.value.length; i++) {
if (coreType.value === "xray") {
dnsResult.value[i] = parseDnsObjectXray(dnsResult.value[i])
} else if (coreType.value === "sing-box") {
dnsResult.value[i] = parseDnsObjectSingbox(dnsResult.value[i])
}
dnsResult.value[i].index = i
dnsResult.value[i].newDns = false
}
})
}
const addDns = () => {
let dns
if (coreType.value === "xray") {
dns = newDnsObjectXray()
} else if (coreType.value === "sing-box") {
dns = newDnsObjectSingbox()
}
dns.index = dnsResult.value.length
dns.newDns = true
dnsResult.value.push(dns)
editDns(dnsResult.value.length - 1)
}
const editDns = (index) => {
currentDnsResult.value = index
dnsEditor.value = true
}
const deleteDns = async (index) => {
await callApi(`delete dns ${index}`)
showDnsManage()
}
const saveDns = async () => {
let params = []
if (dnsResult.value[currentDnsResult.value].newDns) {
params.push("add")
params.push("dns")
} else {
params.push("set")
params.push("dns")
params.push(`${dnsResult.value[currentDnsResult.value].index}`)
}
// standardize
if (coreType.value === "xray") {
standardizeDnsObjectXray(dnsResult.value[currentDnsResult.value])
let k = Object.keys(dnsResult.value[currentDnsResult.value])
if (k.length === 1 && k[0] === "address") {
dnsResult.value[currentDnsResult.value] = dnsResult.value[currentDnsResult.value]["address"]
}
} else if (coreType.value === "sing-box") {
standardizeDnsObjectSingbox(dnsResult.value[currentDnsResult.value])
}
params.push(`${Buffer.from(JSON.stringify(dnsResult.value[currentDnsResult.value])).toString("base64")}`)
await callApi(params)
currentDnsResult.value = 0
showDnsManage()
}
// 点击切换节点按钮
const switchChecked = (item) => {
item.switchLoading = true;
Expand Down Expand Up @@ -647,6 +757,7 @@ const initStatus = () => {
} else if (core_type) {
coreType.value = core_type
if (core_type === 'sing-box') {
actions.push({text: i18n.global.t('manage.dnsrule-manage'), value: 'dnsrule', disabled: false})
actions.push({text: i18n.global.t('manage.ruleset-manage'), value: 'ruleset', disabled: false})
}
if (core_type === 'xray' || core_type === 'sing-box') {
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const manage = {
'edit-custom': 'Custom Node',
'rule-manage': 'Route Rule',
'ruleset-manage': 'Rule Set',
'dns-manage': 'DNS Server',
'dnsrule-manage': 'DNS Rule',
'more-setting': 'Settings',
'load-balancing': 'Balancer',
'load-switch-data-failed': 'Load switch data failed, an exception occurred.',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const manage = {
'edit-custom': '自定义节点',
'rule-manage': '路由规则',
'ruleset-manage': '规则集',
'dns-manage': 'DNS服务器',
'dnsrule-manage': 'DNS规则',
'more-setting': '更多设置',
'load-balancing': '负载均衡',
'load-switch-data-failed': '加载节点信息失败,发生异常。',
Expand Down
31 changes: 30 additions & 1 deletion src/sing-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ export const newRulesetObject = () => {
update_interval: "",
}
}
export const newDnsObject = () => {
return {
tag: "",
address: "",
address_resolver: "",
address_strategy: "",
strategy: "",
detour: "",
client_subnet: ""
}
}
export const parseRuleObject = (rule) => {
let result = newRuleObject()
Object.keys(rule).forEach(key => {
Expand All @@ -65,6 +76,17 @@ export const parseRulesetObject = (ruleset) => {
})
return result
}
export const parseDnsObject = (dns) => {
let result = newDnsObject()
Object.keys(dns).forEach(key => {
if (key === "index" || key === "newDns" || dns[key].length === 0) {
result[key] = dns[key]
} else {
result[key] = dns[key].toString()
}
})
return result
}
export const standardizeRuleObject = (rule) => {
Object.keys(rule).forEach(key => {
if (key === "remarks" || key === "index" || key === "newRule" || rule[key].length === 0) {
Expand Down Expand Up @@ -291,4 +313,11 @@ export const standardizeRulesetObject = (ruleset) => {
delete ruleset[key]
}
})
}
}
export const standardizeDnsObject = (dns) => {
Object.keys(dns).forEach(key => {
if (key === "index" || key === "newDns" || dns[key].length === 0) {
delete dns[key]
}
})
}
61 changes: 60 additions & 1 deletion src/xray.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {newRulesetObject} from "./sing-box.js";

export const newRuleObject = () => {
return {
domainMatcher: "",
Expand All @@ -16,6 +18,16 @@ export const newRuleObject = () => {
ruleTag: ""
}
}
export const newDnsObject = () => {
return {
address: "",
port: "",
domains: "",
expectIPs: "",
skipFallback: "",
clientIP: ""
}
}
export const parseRuleObject = (rule) => {
let result = newRuleObject()
Object.keys(rule).forEach(key => {
Expand All @@ -27,6 +39,21 @@ export const parseRuleObject = (rule) => {
})
return result
}
export const parseDnsObject = (dns) => {
let result = newDnsObject()
if (typeof dns === "string") {
result.address = dns
}else{
Object.keys(dns).forEach(key => {
if (key === "index" || key === "newDns" || dns[key].length === 0) {
result[key] = dns[key]
} else {
result[key] = dns[key].toString()
}
})
}
return result
}
export const standardizeRuleObject = (rule) => {
Object.keys(rule).forEach((key) => {
if (key === "remarks" || key === "index" || key === "newRule" || rule[key].length === 0) {
Expand Down Expand Up @@ -82,4 +109,36 @@ export const standardizeRuleObject = (rule) => {
return
}
})
}
}
export const standardizeDnsObject = (dns) => {
Object.keys(dns).forEach(key => {
if (key === "index" || key === "newDns" || dns[key].length === 0) {
delete dns[key]
return
}
if (key === "port") {
dns[key] = parseInt(dns[key])
return
}
if (key === "domains") {
let arr = dns[key].split(",")
for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i].trim()
}
dns[key] = arr
return
}
if (key === "expectIPs") {
let arr = dns[key].split(",")
for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i].trim()
}
dns[key] = arr
return
}
if (key === "skipFallback") {
dns[key] = JSON.parse(dns[key])
return
}
})
}

0 comments on commit 7fab665

Please sign in to comment.