Skip to content

Commit

Permalink
Merge pull request #4 from insectos:credentialtest
Browse files Browse the repository at this point in the history
moved token config to credentials
  • Loading branch information
Stwissel committed Dec 27, 2023
2 parents 203a985 + c779e62 commit 0975d8b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
41 changes: 20 additions & 21 deletions cloudflare-ddns/cloudflare-ddns-hub.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
name: {
value: '',
required: false
},
spokes: [{ host: '', token: '' }]
}
},
credentials: {
token: { type: 'text' },
cfKey: { type: 'password', required: true },
zoneID: { type: 'password', required: true }
},
Expand Down Expand Up @@ -43,34 +43,29 @@
addButton: 'Add Spoke',
addItem: addSpoke
});
if (!Array.isArray(this.spokes)) {
// Convert legacy format
try {
const replacement = [];
const candidate = JSON.parse(this.spokes);
Object.keys(candidate).forEach((k) => {
let spoke = { host: k, token: candidate[k] };
replacement.push(spoke);
});
this.spokes = replacement;
} catch (e) {
console.log(e);
}
let candidate;
try {
candidate = JSON.parse(this.credentials.token);
} catch (e) {
console.error(e);
candidate = [];
}
this.spokes.forEach((spoke) =>
candidate.forEach((spoke) =>
$('#node-input-spokes-container').editableList('addItem', spoke)
);
}

function saveList() {
const items = $('#node-input-spokes-container').editableList('items');
this.spokes = [];
for (let i = 0; i < items.length; i++) {
const El = $(items[i]);
const newSpokes = [];
for (const element of items) {
const El = $(element);
const curHost = El.find('.datahost')[0].value;
const curToken = El.find('.datatoken')[0].value;
this.spokes.push({ host: curHost, token: curToken });
newSpokes.push({ host: curHost, token: curToken });
}
this.credentials.token = JSON.stringify(newSpokes);
$('#node-input-token')[0].value = JSON.stringify(newSpokes);
}

function addSpoke(container, index, data) {
Expand All @@ -87,7 +82,7 @@
</div>
<div class="form-row">
<label for="token${index}">Token</label>
<input type="text" id="token${index}" class="datatoken" value="${data.token}">
<input type="password" id="token${index}" class="datatoken" value="${data.token}">
</div>
`;
$(container).html(HTML);
Expand All @@ -107,6 +102,10 @@
<label for="node-input-zoneID"><i class="fa fa-globe"></i> Cloudflare ZoneID</label>
<input type="password" id="node-input-zoneID">
</div>
<div class="form-row" style="display: none">
<label for="node-input-token">Token</label>
<input type="text" id="node-input-token">
</div>
<div class="form-row node-input-rule-spokes-row">
<ol id="node-input-spokes-container"></ol>
</div>
Expand Down
20 changes: 16 additions & 4 deletions cloudflare-ddns/cloudflare-ddns-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);
let node = this;
node.spokes = {};
config.spokes.forEach((spoke) => {
node.spokes[spoke.host] = spoke.token;
});
if (this.credentials.token) {
try {
const parsed = JSON.parse(this.credentials.token);
parsed.forEach((spoke) => {
node.spokes[spoke.host] = spoke.token;
});
} catch (e) {
console.error(e);
}
} else if (config.spokes) {
config.spokes.forEach((spoke) => {
node.spokes[spoke.host] = spoke.token;
});
}
node.cfKey = this.credentials.cfKey;
node.zoneID = this.credentials.zoneID;

Expand All @@ -30,7 +41,8 @@ module.exports = function (RED) {
RED.nodes.registerType('cloudflare-ddns-hub', CloudFlareDdnsHub, {
credentials: {
cfKey: { type: 'password' },
zoneID: { type: 'password' }
zoneID: { type: 'password' },
token: { type: 'text' }
}
});
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@insectos/node-red-cloudflare-ddns",
"version": "0.3.0",
"version": "0.4.0",
"description": "A DDNS client for Cloudflare with optional distributed operation",
"main": "index.js",
"engines": {
Expand Down

0 comments on commit 0975d8b

Please sign in to comment.