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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Schulcz Ferenc
dyndns-server
Commits
a75659ea
Commit
a75659ea
authored
3 months ago
by
Ferenc Schulcz
Browse files
Options
Downloads
Patches
Plain Diff
Make initialization more resilient
parent
10f94c52
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
index.js
+113
-71
113 additions, 71 deletions
index.js
with
113 additions
and
71 deletions
index.js
+
113
−
71
View file @
a75659ea
const
https
=
require
(
'
https
'
);
const
express
=
require
(
'
express
'
);
// Initialize
// --------------------------------------------------------
const
app
=
express
();
const
{
error
}
=
require
(
'
console
'
);
const
config
=
require
(
'
./config
'
);
config
.
init
();
const
db
=
require
(
'
./database
'
);
db
.
init
(
);
const
knotdns
=
require
(
'
./knotdns
'
);
//
Sync
records from master
// --------------------------------------------------------
//
number of times to try syncing DNS
records from master
before giving up
const
retryMax
=
5
;
console
.
log
(
"
Syncing records from
"
+
config
.
config
().
master
.
address
+
"
/ ...
"
);
// Synchronizes current DNS records from master
function
syncRecords
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
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
);
const
errormsg
=
'
Bad HTTP status code from
'
+
config
.
config
().
master
.
address
+
'
:
'
+
res
.
statusCode
;
reject
(
errormsg
);
return
;
}
...
...
@@ -48,29 +45,34 @@ https.get(config.config().master.address + '/dyndns-list?id=' + config.config().
await
knotdns
.
updateRecord
(
domain
,
ip
);
console
.
log
(
"
Registering domain
"
+
domain
+
"
to
"
+
ip
);
}
};
}
resolve
();
return
;
}
loadRecords
(
records
);
}
catch
(
error
)
{
console
.
error
(
"
Error parsing records from master:
"
,
error
);
process
.
exit
(
1
);
const
errormsg
=
"
Error parsing records from master:
"
+
error
;
reject
(
errormsg
);
return
;
}
})
})
});
}).
on
(
'
error
'
,
(
error
)
=>
{
console
.
error
(
"
Could not sync records from master:
"
,
error
);
process
.
exit
(
1
);
const
errormsg
=
"
Could not sync records from master:
"
+
error
;
reject
(
errormsg
);
return
;
});
})
}
// Register and serve endpoints
// --------------------------------------------------------
// Start HTTP server and process record updates
function
handleUpdates
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
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
());
...
...
@@ -82,4 +84,44 @@ const server_port = config.config().listenPort;
const
server_domain
=
config
.
config
().
listenAddress
;
app
.
listen
(
server_port
,
server_domain
,
function
()
{
console
.
log
(
`This server is listening on http://
${
server_domain
}
:
${
server_port
}
`
);
resolve
();
});
})
}
// Try syncing from master a few times and then serve DNS update requests
function
syncAndRun
(
retries
)
{
console
.
log
(
"
Syncing records from
"
+
config
.
config
().
master
.
address
+
"
/ ...
"
);
syncRecords
().
then
(()
=>
{
handleUpdates
();
})
.
catch
((
errormsg
)
=>
{
console
.
error
(
errormsg
);
if
(
retries
<
retryMax
)
{
console
.
error
(
"
Retrying in 1 min.
"
);
setTimeout
(()
=>
{
syncAndRun
(
retries
+
1
);
},
60000
);
}
else
{
console
.
error
(
"
Giving up.
"
);
process
.
exit
(
1
);
}
})
}
// Initialize
// --------------------------------------------------------
const
app
=
express
();
config
.
init
();
db
.
init
();
// Sync records from master then serve requests
// --------------------------------------------------------
var
retries
=
0
;
syncAndRun
(
retries
);
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