Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dyndns-server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Schulcz Ferenc
dyndns-server
Commits
10f94c52
Commit
10f94c52
authored
5 months ago
by
Ferenc Schulcz
Browse files
Options
Downloads
Patches
Plain Diff
Load zone from master on startup
parent
8d1fbabb
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
config.json.defaults
+6
-1
6 additions, 1 deletion
config.json.defaults
database.js
+25
-2
25 additions, 2 deletions
database.js
index.js
+61
-0
61 additions, 0 deletions
index.js
knotdns.js
+36
-1
36 additions, 1 deletion
knotdns.js
package.json
+1
-1
1 addition, 1 deletion
package.json
with
129 additions
and
5 deletions
config.json.defaults
+
6
−
1
View file @
10f94c52
...
...
@@ -3,5 +3,10 @@
"knotLocation": "/usr/sbin/knotc",
"dbHost": "localhost",
"listenAddress": "127.0.0.1",
"listenPort": 3000
"listenPort": 3000,
"master": {
"address": "https://dyndns.example.com",
"myId": "ns-test",
"key": "verysecret"
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
database.js
+
25
−
2
View file @
10f94c52
...
...
@@ -48,7 +48,28 @@ function updateDomain(domain, token, ip) {
Domain
.
updateOne
({
token
:
token
},
{
name
:
domain
,
token
:
token
,
ip
:
ip
})
.
catch
((
err
)
=>
reject
())
.
then
((
d
)
=>
resolve
());
})
});
}
function
insertDomain
(
domain
,
token
,
ip
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
record
=
new
Domain
({
name
:
domain
,
token
:
token
,
ip
:
ip
});
record
.
save
()
.
catch
((
err
)
=>
reject
(
err
))
.
then
(()
=>
resolve
());
});
}
function
clearDomains
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
Domain
.
deleteMany
({})
.
catch
(
err
=>
reject
(
err
))
.
then
(()
=>
resolve
());
});
}
module
.
exports
=
{
...
...
@@ -56,5 +77,7 @@ module.exports = {
domainExists
:
domainExists
,
registerDomain
:
registerDomain
,
getDomain
:
getDomain
,
updateDomain
:
updateDomain
updateDomain
:
updateDomain
,
insertDomain
:
insertDomain
,
clearDomains
:
clearDomains
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
index.js
+
61
−
0
View file @
10f94c52
const
https
=
require
(
'
https
'
);
const
express
=
require
(
'
express
'
);
// Initialize
// --------------------------------------------------------
const
app
=
express
();
const
config
=
require
(
'
./config
'
);
...
...
@@ -7,9 +12,65 @@ config.init();
const
db
=
require
(
'
./database
'
);
db
.
init
();
// Sync records from master
// --------------------------------------------------------
console
.
log
(
"
Syncing records from
"
+
config
.
config
().
master
.
address
+
"
/ ...
"
);
https
.
get
(
config
.
config
().
master
.
address
+
'
/dyndns-list?id=
'
+
config
.
config
().
master
.
myId
+
'
&key=
'
+
config
.
config
().
master
.
key
,
(
res
)
=>
{
if
(
res
.
statusCode
>
299
)
{
process
.
exit
(
2
);
return
;
}
let
response
=
''
;
res
.
on
(
'
data
'
,
(
chunk
)
=>
response
+=
chunk
);
res
.
on
(
'
end
'
,
()
=>
{
db
.
clearDomains
().
then
(()
=>
{
knotdns
.
clearZone
().
then
(()
=>
{
console
.
log
(
"
Purged existing domains
"
);
try
{
records
=
JSON
.
parse
(
response
);
async
function
loadRecords
(
records
)
{
for
(
const
r
of
records
)
{
const
domain
=
r
.
domain
.
split
(
"
.
"
)[
0
];
const
token
=
r
.
token
;
const
ip
=
r
.
ip
;
if
(
ip
==
"
null
"
)
{
await
db
.
registerDomain
(
domain
,
token
);
console
.
log
(
"
Registering record without IP:
"
+
domain
);
}
else
{
await
db
.
insertDomain
(
domain
,
token
,
ip
);
await
knotdns
.
updateRecord
(
domain
,
ip
);
console
.
log
(
"
Registering domain
"
+
domain
+
"
to
"
+
ip
);
}
};
}
loadRecords
(
records
);
}
catch
(
error
)
{
console
.
error
(
"
Error parsing records from master:
"
,
error
);
process
.
exit
(
1
);
}
})
})
});
}).
on
(
'
error
'
,
(
error
)
=>
{
console
.
error
(
"
Could not sync records from master:
"
,
error
);
process
.
exit
(
1
);
});
// Register and serve endpoints
// --------------------------------------------------------
const
registerMW
=
require
(
'
./middleware/registerMW
'
);
const
replyMW
=
require
(
'
./middleware/replyMW
'
);
const
updateMW
=
require
(
'
./middleware/updateMW
'
);
const
{
error
}
=
require
(
'
console
'
);
const
knotdns
=
require
(
'
./knotdns
'
);
// register a new dyndns record
app
.
get
(
'
/register
'
,
registerMW
(),
replyMW
());
...
...
This diff is collapsed.
Click to expand it.
knotdns.js
+
36
−
1
View file @
10f94c52
...
...
@@ -3,6 +3,37 @@ const exec = util.promisify(require('child_process').exec);
const
config
=
require
(
'
./config
'
);
async
function
clearZone
()
{
const
{
stdout
,
stderr
}
=
await
exec
(
config
.
config
().
knotLocation
+
"
zone-read
"
+
config
.
config
().
higherLevelDomain
);
const
domains
=
stdout
.
split
(
'
\n
'
)
.
filter
(
line
=>
line
.
includes
(
'
60 A
'
))
//filter for A records
.
map
(
line
=>
line
.
match
(
'
\
] (.*) 60 A
'
)[
1
])
// regex match for full domains
.
map
(
domain
=>
domain
.
split
(
'
.
'
)[
0
]);
// cut subdomain
await
exec
(
config
.
config
().
knotLocation
+
"
zone-begin
"
+
config
.
config
().
higherLevelDomain
);
for
(
domain
of
domains
)
{
await
exec
(
config
.
config
().
knotLocation
+
"
zone-unset
"
+
config
.
config
().
higherLevelDomain
+
"
"
+
domain
);
}
await
exec
(
config
.
config
().
knotLocation
+
"
zone-commit
"
+
config
.
config
().
higherLevelDomain
);
}
async
function
insertRecord
(
domain
,
ip
)
{
try
{
await
exec
(
config
.
config
().
knotLocation
+
"
zone-begin
"
+
config
.
config
().
higherLevelDomain
);
await
exec
(
config
.
config
().
knotLocation
+
"
zone-set
"
+
config
.
config
().
higherLevelDomain
+
"
"
+
domain
+
"
60 A
"
+
ip
);
await
exec
(
config
.
config
().
knotLocation
+
"
zone-commit
"
+
config
.
config
().
higherLevelDomain
);
}
catch
(
error
)
{
try
{
await
exec
(
config
.
config
().
knotLocation
+
"
zone-abort
"
+
config
.
config
().
higherLevelDomain
);
console
.
log
(
"
Problem with manipulating DNS zone
"
+
config
.
config
().
higherLevelDomain
+
"
. Aborted transaction.
"
+
error
);
}
catch
{
console
.
log
(
"
Problem with manipulating DNS zone
"
+
config
.
config
().
higherLevelDomain
+
"
. Could not even abort transaction.
"
+
error
);
}
}
}
async
function
updateRecord
(
domain
,
ip
)
{
try
{
await
exec
(
config
.
config
().
knotLocation
+
"
zone-begin
"
+
config
.
config
().
higherLevelDomain
);
...
...
@@ -23,6 +54,10 @@ async function updateRecord(domain, ip) {
}
}
module
.
exports
=
{
updateRecord
:
updateRecord
updateRecord
:
updateRecord
,
insertRecord
:
insertRecord
,
clearZone
:
clearZone
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
package.json
+
1
−
1
View file @
10f94c52
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment