@@ -88,26 +88,36 @@ if ! [ -x $(command -v curl) ]; then
88
88
fi
89
89
90
90
if [[ -z $DOMAIN_NAME ]]; then
91
-
92
- echo " Enter the domain name for your new EmailEngine installation."
93
- echo " (ex. example.com or test.example.com)"
94
- echo " Leave empty to autogenerate a domain name."
95
-
96
- while [ -z " $DOMAIN_NAME " ]
97
- do
98
- # echo -en "\n"
99
- read -p " Domain/Subdomain name: " DOMAIN_NAME
100
-
101
- if [ -z " $DOMAIN_NAME " ]
102
- then
103
- DOMAIN_NAME=$( curl --silent --fail -XPOST " https://api.nodemailer.com/autoassign" -H " Content-Type: application/json" -d ' {
104
- "prefix": "engine",
105
- "version": "1",
106
- "requestor": "install"
107
- }' )
91
+ # For upgrades, try to extract existing domain from Caddyfile
92
+ if [ " $IS_UPGRADE " = true ] && [ -f /etc/caddy/Caddyfile ]; then
93
+ EXISTING_DOMAIN=$( grep -E ' ^[a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9] \{' /etc/caddy/Caddyfile 2> /dev/null | head -1 | cut -d' ' -f1 || echo " " )
94
+ if [ -n " $EXISTING_DOMAIN " ]; then
95
+ DOMAIN_NAME=" $EXISTING_DOMAIN "
96
+ echo " Using existing domain: $DOMAIN_NAME "
108
97
fi
109
- done
110
-
98
+ fi
99
+
100
+ # Only prompt for domain if not found (new installation or upgrade without existing domain)
101
+ if [ -z " $DOMAIN_NAME " ]; then
102
+ echo " Enter the domain name for your new EmailEngine installation."
103
+ echo " (ex. example.com or test.example.com)"
104
+ echo " Leave empty to autogenerate a domain name."
105
+
106
+ while [ -z " $DOMAIN_NAME " ]
107
+ do
108
+ # echo -en "\n"
109
+ read -p " Domain/Subdomain name: " DOMAIN_NAME
110
+
111
+ if [ -z " $DOMAIN_NAME " ]
112
+ then
113
+ DOMAIN_NAME=$( curl --silent --fail -XPOST " https://api.nodemailer.com/autoassign" -H " Content-Type: application/json" -d ' {
114
+ "prefix": "engine",
115
+ "version": "1",
116
+ "requestor": "install"
117
+ }' )
118
+ fi
119
+ done
120
+ fi
111
121
fi
112
122
113
123
echo " Using the domain name \" ${DOMAIN_NAME} \" for this installation."
@@ -243,8 +253,22 @@ systemctl restart emailengine || { echo "Failed to start EmailEngine service"; e
243
253
# Log installation details
244
254
echo " $( date) : EmailEngine ${VERSION:- latest} installed/upgraded for $DOMAIN_NAME " >> /var/log/emailengine-install.log
245
255
246
- # Create Caddyfile with security headers (skip if upgrading and file exists)
247
- if [ " $IS_UPGRADE " = false ] || [ ! -f /etc/caddy/Caddyfile ]; then
256
+ # Check if domain exists in Caddyfile
257
+ DOMAIN_EXISTS_IN_CADDY=false
258
+ if [ -f /etc/caddy/Caddyfile ]; then
259
+ if grep -q " ^${DOMAIN_NAME} {" /etc/caddy/Caddyfile 2> /dev/null; then
260
+ DOMAIN_EXISTS_IN_CADDY=true
261
+ fi
262
+ fi
263
+
264
+ # Create or update Caddyfile only if needed
265
+ if [ " $IS_UPGRADE " = false ] || [ ! -f /etc/caddy/Caddyfile ] || [ " $DOMAIN_EXISTS_IN_CADDY " = false ]; then
266
+ if [ " $DOMAIN_EXISTS_IN_CADDY " = false ] && [ -f /etc/caddy/Caddyfile ]; then
267
+ echo " Domain ${DOMAIN_NAME} not found in Caddy configuration, adding it..."
268
+ # Backup existing Caddyfile
269
+ cp /etc/caddy/Caddyfile /etc/caddy/Caddyfile.backup.$( date +%Y%m%d%H%M%S)
270
+ fi
271
+
248
272
echo "
249
273
:80 {
250
274
redir https://${DOMAIN_NAME} {uri}
@@ -253,8 +277,6 @@ if [ "$IS_UPGRADE" = false ] || [ ! -f /etc/caddy/Caddyfile ]; then
253
277
${DOMAIN_NAME} {
254
278
reverse_proxy localhost:3000 {
255
279
header_up X-Real-IP {remote_host}
256
- header_up X-Forwarded-For {remote_host}
257
- header_up X-Forwarded-Proto {scheme}
258
280
}
259
281
260
282
header {
@@ -275,7 +297,7 @@ ${DOMAIN_NAME} {
275
297
caddy validate --config /etc/caddy/Caddyfile || { echo " Invalid Caddy configuration" ; exit 1; }
276
298
echo " Caddy configuration validated successfully"
277
299
else
278
- echo " Using existing Caddy configuration"
300
+ echo " Domain ${DOMAIN_NAME} already configured in Caddy, keeping existing configuration"
279
301
fi
280
302
281
303
# Create upgrade script
0 commit comments