wireguard optional fields saniteze fix

This commit is contained in:
Сергей Павлюк 2023-12-25 22:48:09 +03:00
parent b7fb49fe8f
commit b2e1c46ffe

View file

@ -854,9 +854,12 @@ Outbound.ShadowsocksSettings = class extends CommonClass {
Outbound.WireguardSettings = class extends CommonClass { Outbound.WireguardSettings = class extends CommonClass {
constructor(secretKey, address, peers, mtu, workers, domainStrategy) { constructor(secretKey, address, peers, mtu, workers, domainStrategy) {
super(); super();
this.secretKey = secretKey; this.secretKey = secretKey || '';
this.address = address ? [...address] : ['']; this.address = address ? [...address] : [];
this.peers = peers ? peers.map((p) => ({ ...p })) : []; this.peers = peers ? peers.map((p) => ({
...p,
allowedIPs: p.allowedIPs ? [...p.allowedIPs] : [],
})) : [];
this.mtu = mtu; this.mtu = mtu;
this.workers = workers; this.workers = workers;
this.domainStrategy = domainStrategy; this.domainStrategy = domainStrategy;
@ -882,7 +885,9 @@ Outbound.WireguardSettings = class extends CommonClass {
addPeer() { addPeer() {
this.peers.push({ this.peers.push({
allowedIPs: [''], endpoint: '',
publicKey: '',
allowedIPs: [],
}); });
} }
@ -902,27 +907,30 @@ Outbound.WireguardSettings = class extends CommonClass {
this.peers[index].allowedIPs.splice(ipIndex, 1); this.peers[index].allowedIPs.splice(ipIndex, 1);
} }
optionalFields = ['mtu', 'workers', 'domainStrategy']; optionalFields = ['mtu', 'workers', 'domainStrategy', 'address'];
optionalPeerFields = ['allowedIPs', 'keepAlive', 'preSharedKey']; optionalPeerFields = ['allowedIPs', 'keepAlive', 'preSharedKey'];
cleanUpOptionalFields(obj) { cleanUpOptionalFields(obj) {
const isEmpty = (v) => ObjectUtil.isEmpty(v) || ObjectUtil.isArrEmpty(v);
return Object.entries(obj).reduce((memo, [key, value]) => { return Object.entries(obj).reduce((memo, [key, value]) => {
if (key === 'peers') { if (key === 'peers') {
memo[key] = value.map((peer) => { memo[key] = value.map((peer) => {
return Object.entries(peer).reduce((pMemo, [pKey, pValue]) => { return Object.entries(peer).reduce((pMemo, [pKey, pValue]) => {
if (this.optionalPeerFields.includes(pKey) && ObjectUtil.isEmpty(pValue)) { if (this.optionalPeerFields.includes(pKey) && isEmpty(pValue)) {
return pMemo; return pMemo;
} }
pMemo[pKey] = pValue; pMemo[pKey] = pValue;
return pMemo; return pMemo;
}); }, {});
}); });
} else if (this.optionalFields.includes(key) && ObjectUtil.isEmpty(value)) { } else if (this.optionalFields.includes(key) && isEmpty(value)) {
return memo; return memo;
} else {
memo[key] = value;
} }
memo[key] = value;
return memo; return memo;
}, {}); }, {});
} }