Fix: the issue of regenerating the config file without filling the placeholders.

This commit is contained in:
Shahrad Elahi 2024-02-15 11:12:05 +03:30
parent ccc3b20132
commit ff674dda61
6 changed files with 72 additions and 61 deletions

View File

@ -5,8 +5,8 @@ WORKDIR /app
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY --from=chriswayg/tor-alpine:latest /usr/local/bin/obfs4proxy /usr/local/bin/obfs4proxy
COPY --from=chriswayg/tor-alpine:latest /usr/local/bin/meek-server /usr/local/bin/meek-server
COPY --from=chriswayg/tor-alpine:latest --platform=$BUILDPLATFORM /usr/local/bin/obfs4proxy /usr/local/bin/obfs4proxy
COPY --from=chriswayg/tor-alpine:latest --platform=$BUILDPLATFORM /usr/local/bin/meek-server /usr/local/bin/meek-server
# Update and upgrade packages
RUN apk update && apk upgrade &&\
@ -21,7 +21,7 @@ RUN apk update && apk upgrade &&\
# Clear APK cache
rm -rf /var/cache/apk/*
COPY /config/torrc /etc/tor/torrc
COPY /config/torrc.template /etc/tor/torrc.template
# Copy user scripts
COPY /bin /usr/local/bin

View File

@ -22,7 +22,7 @@ RUN apk update && apk upgrade &&\
rm -rf /var/cache/apk/*
# Copy Tor Configs
COPY /config/torrc /etc/tor/torrc
COPY /config/torrc.template /etc/tor/torrc.template
COPY /config/obfs4-bridges.conf /etc/torrc.d/obfs4-bridges.conf
# Copy user scripts

View File

@ -1,9 +1,11 @@
#!/usr/bin/env bash
set -e
TOR_CONFIG="/etc/tor/torrc"
ENV_FILE="/app/.env"
TOR_CONFIG="/etc/tor/torrc"
TOR_CONFIG_TEMPLATE="${TOR_CONFIG}.template"
log() {
local level=$1
local message=$2
@ -15,6 +17,9 @@ to_camel_case() {
}
generate_tor_config() {
# Copying the torrc template to the torrc file
cp "${TOR_CONFIG_TEMPLATE}" "${TOR_CONFIG}"
# IP address of the container
local inet_address="$(hostname -i | awk '{print $1}')"
@ -33,16 +38,11 @@ generate_tor_config() {
awk -F= '!a[tolower($1)]++' "${TOR_CONFIG}" >"/tmp/$(basename "${TOR_CONFIG}")" &&
mv "/tmp/$(basename "${TOR_CONFIG}")" "${TOR_CONFIG}"
# Checking if there is /etc/torrc.d folder and if there is
# any file in it, adding them to the torrc file
# Checking if there is /etc/torrc.d folder and if there are use globbing to include all files
local torrc_files=$(find /etc/torrc.d -type f -name "*.conf")
if [ -n "${torrc_files}" ]; then
log "notice" "Found torrc.d folder with configuration files"
for file in ${torrc_files}; do
log "notice" "Adding $file to the main torrc file"
cat "$file" >>"${TOR_CONFIG}"
done
echo "%include /etc/torrc.d/*.conf" >>"${TOR_CONFIG}"
fi
# Remove comment line with single Hash

31
web/src/lib/services.ts Normal file
View File

@ -0,0 +1,31 @@
import { execa } from 'execa';
import logger from '$lib/logger';
export const SERVICES = <const>{
tor: {
name: 'Tor',
command: {
start: 'screen -L -Logfile /var/vlogs/tor -dmS "tor" tor -f /etc/tor/torrc',
stop: 'pkill tor',
logs: 'logs tor',
},
},
};
export type ServiceName = keyof typeof SERVICES;
export function restart(serviceName: ServiceName) {
// Stop
const { exitCode: stopExitCode } = execa(SERVICES[serviceName].command.stop, { shell: true });
// Start
const { exitCode: startExitCode } = execa(SERVICES[serviceName].command.start, { shell: true });
logger.info({
message: `Restarted ${serviceName} service`,
stopExitCode,
startExitCode,
});
return stopExitCode === 0 && startExitCode === 0;
}

View File

@ -3,59 +3,26 @@ import type { PageServerLoad } from './$types';
import logger from '$lib/logger';
import { execa } from 'execa';
import { promises } from 'node:fs';
const services: Record<string, Service> = {
tor: {
name: 'Tor',
command: {
start: 'screen -L -Logfile /var/vlogs/tor -dmS "tor" tor -f /etc/tor/torrc',
stop: 'pkill tor',
logs: 'logs tor',
},
},
};
interface Service {
name: string;
command: {
start: string;
stop: string;
logs: string;
};
}
import { restart, type ServiceName, SERVICES } from '$lib/services';
export const load: PageServerLoad = async ({ params }) => {
if (!params.serviceName || !services[params.serviceName]) {
const { serviceName } = params as { serviceName: ServiceName | undefined };
if (!serviceName || !SERVICES[serviceName]) {
logger.error('Service not found. service:', serviceName);
throw error(404, 'Not Found');
}
const { name } = SERVICES[serviceName];
return {
slug: params.serviceName,
title: services[params.serviceName].name,
title: name,
};
};
function restartService(serviceName: string) {
// Stop
const { exitCode: stopExitCode } = execa(services[serviceName].command.stop, { shell: true });
// Start
const { exitCode: startExitCode } = execa(services[serviceName].command.start, { shell: true });
logger.info({
message: `Restarted ${serviceName} service`,
stopExitCode,
startExitCode,
});
return {
stopExitCode,
startExitCode,
};
}
export const actions: Actions = {
clearLogs: async ({ request, params }) => {
clearLogs: async ({ params }) => {
const { serviceName } = params;
try {
@ -66,23 +33,36 @@ export const actions: Actions = {
throw error(500, 'Unhandled Exception');
}
},
logs: async ({ request, params }) => {
const { serviceName } = params;
logs: async ({ params }) => {
const { serviceName } = params as { serviceName: ServiceName | undefined };
if (!serviceName || !SERVICES[serviceName]) {
logger.error('Service not found. service:', serviceName);
throw error(404, 'Not Found');
}
const service = SERVICES[serviceName];
try {
const { stdout } = await execa(services[serviceName!].command.logs, { shell: true });
const { stdout } = await execa(service.command.logs, { shell: true });
return { logs: stdout };
} catch (e) {
logger.error(e);
throw error(500, 'Unhandled Exception');
}
},
restart: async ({ request, params }) => {
const { serviceName } = params;
restart: async ({ params }) => {
const { serviceName } = params as { serviceName: ServiceName | undefined };
if (!serviceName || !SERVICES[serviceName]) {
logger.error('Service not found. service:', serviceName);
throw error(404, 'Not Found');
}
try {
const { stopExitCode, startExitCode } = restartService(serviceName!);
return { stopExitCode, startExitCode };
const success = restart(serviceName!);
return { success };
} catch (e) {
logger.error(e);
throw error(500, 'Unhandled Exception');