1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Server: Resolves #8153: Allow setting NTP server using NTP_SERVER env variable

This commit is contained in:
Laurent Cozic 2023-05-10 12:50:48 +01:00
parent 8cedf27fea
commit 33f0811ad2
3 changed files with 23 additions and 10 deletions

View File

@ -2,19 +2,29 @@ import shim from './shim';
import time from './time'; import time from './time';
const ntpClient_ = require('./vendor/ntp-client'); const ntpClient_ = require('./vendor/ntp-client');
const server = { interface NtpServer {
domain: 'pool.ntp.org', domain: string;
port: 123, port: number;
}; }
function ntpClient() { function ntpClient() {
ntpClient_.dgram = shim.dgram(); ntpClient_.dgram = shim.dgram();
return ntpClient_; return ntpClient_;
} }
export async function getNetworkTime(): Promise<Date> { const parseNtpServer = (ntpServer: string): NtpServer => {
const s = ntpServer.split(':');
if (s.length !== 2) throw new Error('NTP server URL must be in format `domain:port`');
return {
domain: s[0],
port: Number(s[1]),
};
};
export async function getNetworkTime(ntpServer: string): Promise<Date> {
return new Promise((resolve: Function, reject: Function) => { return new Promise((resolve: Function, reject: Function) => {
ntpClient().getNetworkTime(server.domain, server.port, (error: any, date: Date) => { const s = parseNtpServer(ntpServer);
ntpClient().getNetworkTime(s.domain, s.port, (error: any, date: Date) => {
if (error) { if (error) {
reject(error); reject(error);
return; return;
@ -25,7 +35,7 @@ export async function getNetworkTime(): Promise<Date> {
}); });
} }
export async function getDeviceTimeDrift(): Promise<number> { export async function getDeviceTimeDrift(ntpServer: string): Promise<number> {
const maxTries = 3; const maxTries = 3;
let tryCount = 0; let tryCount = 0;
@ -34,12 +44,12 @@ export async function getDeviceTimeDrift(): Promise<number> {
while (true) { while (true) {
tryCount++; tryCount++;
try { try {
ntpTime = await getNetworkTime(); ntpTime = await getNetworkTime(ntpServer);
break; break;
} catch (error) { } catch (error) {
if (tryCount >= maxTries) { if (tryCount >= maxTries) {
const newError = typeof error === 'string' ? new Error(error) : error; const newError = typeof error === 'string' ? new Error(error) : error;
newError.message = `Cannot retrieve the network time from ${server.domain}:${server.port}: ${newError.message}`; newError.message = `Cannot retrieve the network time from ${ntpServer}: ${newError.message}`;
throw newError; throw newError;
} else { } else {
await time.msleep(tryCount * 1000); await time.msleep(tryCount * 1000);

View File

@ -255,7 +255,8 @@ async function main() {
appLogger().info(`Starting server v${config().appVersion} (${env}) on port ${config().port} and PID ${process.pid}...`); appLogger().info(`Starting server v${config().appVersion} (${env}) on port ${config().port} and PID ${process.pid}...`);
if (config().maxTimeDrift) { if (config().maxTimeDrift) {
const timeDrift = await getDeviceTimeDrift(); appLogger().info(`Checking for time drift using NTP server: ${config().NTP_SERVER}`);
const timeDrift = await getDeviceTimeDrift(config().NTP_SERVER);
if (Math.abs(timeDrift) > config().maxTimeDrift) { if (Math.abs(timeDrift) > config().maxTimeDrift) {
throw new Error(`The device time drift is ${timeDrift}ms (Max allowed: ${config().maxTimeDrift}ms) - cannot continue as it could cause data loss and conflicts on the sync clients. You may increase env var MAX_TIME_DRIFT to pass the check, or set to 0 to disabled the check.`); throw new Error(`The device time drift is ${timeDrift}ms (Max allowed: ${config().maxTimeDrift}ms) - cannot continue as it could cause data loss and conflicts on the sync clients. You may increase env var MAX_TIME_DRIFT to pass the check, or set to 0 to disabled the check.`);
} }

View File

@ -31,6 +31,7 @@ const defaultEnvValues: EnvVariables = {
// check. https://github.com/laurent22/joplin/issues/5738 // check. https://github.com/laurent22/joplin/issues/5738
MAX_TIME_DRIFT: 2000, MAX_TIME_DRIFT: 2000,
NTP_SERVER: 'pool.ntp.org:123',
// ================================================== // ==================================================
// URL config // URL config
@ -109,6 +110,7 @@ export interface EnvVariables {
COOKIES_SECURE: boolean; COOKIES_SECURE: boolean;
RUNNING_IN_DOCKER: boolean; RUNNING_IN_DOCKER: boolean;
MAX_TIME_DRIFT: number; MAX_TIME_DRIFT: number;
NTP_SERVER: string;
APP_BASE_URL: string; APP_BASE_URL: string;
USER_CONTENT_BASE_URL: string; USER_CONTENT_BASE_URL: string;