Skip to content

Commit 5e6b76f

Browse files
committed
chore: use consts in relay env and service files and fix nginx file formatting
1 parent 45a9e6a commit 5e6b76f

20 files changed

+66
-53
lines changed

pkg/relays/khatru29/constants.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RELAY_NAME="Khatru29"
1313
RELAY_PRIVKEY="{{.PrivKey}}"
1414
RELAY_DESCRIPTION="Khatru29 Nostr Relay"
1515
RELAY_CONTACT="{{.RelayContact}}"
16-
DATABASE_PATH=/var/lib/khatru29/db
16+
DATABASE_PATH="/var/lib/khatru29/db"
1717
`
1818
const ServiceFilePath = "/etc/systemd/system/khatru29.service"
1919
const ServiceFileTemplate = `[Unit]
@@ -25,8 +25,8 @@ Type=simple
2525
User=nostr
2626
Group=nostr
2727
WorkingDirectory=/home/nostr
28-
EnvironmentFile=/etc/systemd/system/khatru29.env
29-
ExecStart=/usr/local/bin/khatru29
28+
EnvironmentFile={{.EnvFilePath}}
29+
ExecStart={{.BinaryFilePath}}
3030
Restart=on-failure
3131
3232
[Install]

pkg/relays/khatru29/nginx_http.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ server {
5555
# Test configuration:
5656
# https://securityheaders.com/
5757
# https://observatory.mozilla.org/
58-
add_header X-Frame-Options DENY;
58+
add_header X-Frame-Options DENY;
5959
6060
# Avoid MIME type sniffing
61-
add_header X-Content-Type-Options nosniff always;
61+
add_header X-Content-Type-Options nosniff always;
6262
63-
add_header Referrer-Policy "no-referrer" always;
63+
add_header Referrer-Policy "no-referrer" always;
6464
65-
add_header X-XSS-Protection 0 always;
65+
add_header X-XSS-Protection 0 always;
6666
67-
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
67+
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
6868
6969
#### Content-Security-Policy (CSP) ####
70-
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
70+
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
7171
}
7272
7373
server {

pkg/relays/khatru29/service.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func SetupRelayService(domain, privKey, relayContact string) {
4040

4141
// Create the systemd service file
4242
spinner.UpdateText("Creating service file...")
43-
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate)
43+
serviceFileParams := systemd.ServiceFileParams{EnvFilePath: EnvFilePath, BinaryFilePath: BinaryFilePath}
44+
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate, &serviceFileParams)
4445

4546
// Reload systemd to apply the new service
4647
spinner.UpdateText("Reloading systemd daemon...")

pkg/relays/khatru_pyramid/constants.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Type=simple
2727
User=nostr
2828
Group=nostr
2929
WorkingDirectory=/home/nostr
30-
EnvironmentFile=/etc/systemd/system/khatru-pyramid.env
31-
ExecStart=/usr/local/bin/khatru-pyramid
30+
EnvironmentFile={{.EnvFilePath}}
31+
ExecStart={{.BinaryFilePath}}
3232
Restart=on-failure
3333
3434
[Install]

pkg/relays/khatru_pyramid/nginx_http.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ server {
5555
# Test configuration:
5656
# https://securityheaders.com/
5757
# https://observatory.mozilla.org/
58-
add_header X-Frame-Options DENY;
58+
add_header X-Frame-Options DENY;
5959
6060
# Avoid MIME type sniffing
61-
add_header X-Content-Type-Options nosniff always;
61+
add_header X-Content-Type-Options nosniff always;
6262
63-
add_header Referrer-Policy "no-referrer" always;
63+
add_header Referrer-Policy "no-referrer" always;
6464
65-
add_header X-XSS-Protection 0 always;
65+
add_header X-XSS-Protection 0 always;
6666
67-
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
67+
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
6868
6969
#### Content-Security-Policy (CSP) ####
70-
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
70+
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
7171
}
7272
7373
server {

pkg/relays/khatru_pyramid/nginx_https.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ server {
101101
102102
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
103103
104-
#### Content-Security-Policy (CSP) ####
104+
#### Content-Security-Policy (CSP) ####
105105
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none'; upgrade-insecure-requests;" always;
106106
}
107107

pkg/relays/khatru_pyramid/service.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func SetupRelayService(domain, pubKey, relayContact string) {
4040

4141
// Create the systemd service file
4242
spinner.UpdateText("Creating service file...")
43-
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate)
43+
serviceFileParams := systemd.ServiceFileParams{EnvFilePath: EnvFilePath, BinaryFilePath: BinaryFilePath}
44+
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate, &serviceFileParams)
4445

4546
// Reload systemd to apply the new service
4647
spinner.UpdateText("Reloading systemd daemon...")

pkg/relays/strfry/constants.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ After=network.target
2525
Type=simple
2626
User=nostr
2727
Group=nostr
28-
ExecStart=/usr/local/bin/strfry --config=/etc/strfry/strfry.conf relay
28+
ExecStart={{.BinaryFilePath}} --config={{.ConfigFilePath}} relay
2929
Restart=on-failure
3030
RestartSec=5
3131
ProtectHome=yes

pkg/relays/strfry/nginx_http.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ func ConfigureNginxHttp(domainName string) {
4949
add_header X-Frame-Options DENY;
5050
5151
# Avoid MIME type sniffing
52-
add_header X-Content-Type-Options nosniff always;
52+
add_header X-Content-Type-Options nosniff always;
5353
54-
add_header Referrer-Policy "no-referrer" always;
54+
add_header Referrer-Policy "no-referrer" always;
5555
56-
add_header X-XSS-Protection 0 always;
56+
add_header X-XSS-Protection 0 always;
5757
58-
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
58+
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
5959
6060
#### Content-Security-Policy (CSP) ####
61-
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
61+
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
6262
}
6363
6464
server {

pkg/relays/strfry/nginx_https.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ConfigureNginxHttps(domainName string) {
7575
ssl_stapling on;
7676
ssl_stapling_verify on;
7777
78-
#### Security Headers ####
78+
#### Security Headers ####
7979
# Test configuration:
8080
# https://securityheaders.com/
8181
# https://observatory.mozilla.org/

pkg/relays/strfry/service.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func SetupRelayService(domain string) {
5858

5959
// Create the systemd service file
6060
spinner.UpdateText("Creating service file...")
61-
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate)
61+
serviceFileParams := systemd.ServiceFileParams{BinaryFilePath: BinaryFilePath, ConfigFilePath: ConfigFilePath}
62+
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate, &serviceFileParams)
6263

6364
// Reload systemd to apply the new service
6465
spinner.UpdateText("Reloading systemd daemon...")

pkg/relays/strfry29/constants.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const PluginFilePath = "/usr/local/bin/strfry29.json"
2323
const PluginFileTemplate = `{
2424
"domain": "{{.Domain}}",
2525
"relay_secret_key": "{{.RelaySecretKey}}",
26-
"strfry_config_path": "/etc/strfry29/strfry.conf",
27-
"strfry_executable_path": "/usr/local/bin/strfry"
26+
"strfry_config_path": "{{.ConfigFilePath}}",
27+
"strfry_executable_path": "{{.BinaryFilePath}}"
2828
}
2929
`
3030
const ServiceName = "strfry29"
@@ -41,7 +41,7 @@ After=network.target
4141
Type=simple
4242
User=nostr
4343
Group=nostr
44-
ExecStart=/usr/local/bin/strfry --config=/etc/strfry29/strfry.conf relay
44+
ExecStart={{.BinaryFilePath}} --config={{.ConfigFilePath}} relay
4545
Restart=on-failure
4646
RestartSec=5
4747
ProtectHome=yes

pkg/relays/strfry29/nginx_http.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ func ConfigureNginxHttp(domainName string) {
4646
# Test configuration:
4747
# https://securityheaders.com/
4848
# https://observatory.mozilla.org/
49-
add_header X-Frame-Options DENY;
49+
add_header X-Frame-Options DENY;
5050
5151
# Avoid MIME type sniffing
52-
add_header X-Content-Type-Options nosniff always;
52+
add_header X-Content-Type-Options nosniff always;
5353
54-
add_header Referrer-Policy "no-referrer" always;
54+
add_header Referrer-Policy "no-referrer" always;
5555
56-
add_header X-XSS-Protection 0 always;
56+
add_header X-XSS-Protection 0 always;
5757
58-
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
58+
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
5959
6060
#### Content-Security-Policy (CSP) ####
61-
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
61+
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
6262
}
6363
6464
server {

pkg/relays/strfry29/nginx_https.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ConfigureNginxHttps(domainName string) {
7575
ssl_stapling on;
7676
ssl_stapling_verify on;
7777
78-
#### Security Headers ####
78+
#### Security Headers ####
7979
# Test configuration:
8080
# https://securityheaders.com/
8181
# https://observatory.mozilla.org/

pkg/relays/strfry29/service.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ func SetupRelayService(domain, relaySecretKey string) {
6767

6868
// Create the strfry29.json file
6969
spinner.UpdateText("Creating plugin file...")
70-
pluginFileParams := plugins.PluginFileParams{Domain: domain, RelaySecretKey: relaySecretKey}
70+
pluginFileParams := plugins.PluginFileParams{Domain: domain, RelaySecretKey: relaySecretKey, ConfigFilePath: ConfigFilePath, BinaryFilePath: BinaryFilePath}
7171
plugins.CreatePluginFile(PluginFilePath, PluginFileTemplate, &pluginFileParams)
7272

7373
// Use chown command to set ownership of the strfry29.json file to the nostr user
7474
files.SetOwnerAndGroup("nostr", "nostr", PluginFilePath)
7575

7676
// Create the systemd service file
7777
spinner.UpdateText("Creating service file...")
78-
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate)
78+
serviceFileParams := systemd.ServiceFileParams{BinaryFilePath: BinaryFilePath, ConfigFilePath: ConfigFilePath}
79+
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate, &serviceFileParams)
7980

8081
// Reload systemd to apply the new service
8182
spinner.UpdateText("Reloading systemd daemon...")

pkg/relays/wot_relay/constants.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Type=simple
6060
User=nostr
6161
Group=nostr
6262
WorkingDirectory=/home/nostr
63-
EnvironmentFile=/etc/systemd/system/wot-relay.env
64-
ExecStart=/usr/local/bin/wot-relay
63+
EnvironmentFile={{.EnvFilePath}}
64+
ExecStart={{.BinaryFilePath}}
6565
Restart=on-failure
6666
MemoryHigh=512M
6767
MemoryMax=1G

pkg/relays/wot_relay/nginx_http.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ server {
5757
# Test configuration:
5858
# https://securityheaders.com/
5959
# https://observatory.mozilla.org/
60-
add_header X-Frame-Options DENY;
60+
add_header X-Frame-Options DENY;
6161
6262
# Avoid MIME type sniffing
63-
add_header X-Content-Type-Options nosniff always;
63+
add_header X-Content-Type-Options nosniff always;
6464
65-
add_header Referrer-Policy "no-referrer" always;
65+
add_header Referrer-Policy "no-referrer" always;
6666
67-
add_header X-XSS-Protection 0 always;
67+
add_header X-XSS-Protection 0 always;
6868
69-
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
69+
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
7070
7171
#### Content-Security-Policy (CSP) ####
72-
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
72+
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
7373
}
7474
7575
server {

pkg/relays/wot_relay/service.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ func SetupRelayService(domain, pubKey, relayContact string, httpsEnabled bool) {
6363

6464
// Create the systemd service file
6565
spinner.UpdateText("Creating service file...")
66-
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate)
66+
serviceFileParams := systemd.ServiceFileParams{EnvFilePath: EnvFilePath, BinaryFilePath: BinaryFilePath}
67+
systemd.CreateServiceFile(ServiceFilePath, ServiceFileTemplate, &serviceFileParams)
6768

6869
// Reload systemd to apply the new service
6970
spinner.UpdateText("Reloading systemd daemon...")

pkg/utils/plugins/utils.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
type PluginFileParams struct {
1111
Domain string
1212
RelaySecretKey string
13+
ConfigFilePath string
14+
BinaryFilePath string
1315
}
1416

1517
func CreatePluginFile(pluginFilePath, pluginTemplate string, pluginFileParams *PluginFileParams) {
@@ -28,7 +30,7 @@ func CreatePluginFile(pluginFilePath, pluginTemplate string, pluginFileParams *P
2830
os.Exit(1)
2931
}
3032

31-
err = pluginTmpl.Execute(pluginFile, struct{ Domain, RelaySecretKey string }{Domain: pluginFileParams.Domain, RelaySecretKey: pluginFileParams.RelaySecretKey})
33+
err = pluginTmpl.Execute(pluginFile, struct{ Domain, RelaySecretKey, ConfigFilePath, BinaryFilePath string }{Domain: pluginFileParams.Domain, RelaySecretKey: pluginFileParams.RelaySecretKey, ConfigFilePath: pluginFileParams.ConfigFilePath, BinaryFilePath: pluginFileParams.BinaryFilePath})
3234
if err != nil {
3335
pterm.Println()
3436
pterm.Error.Println(fmt.Sprintf("Failed to execute plugin template: %v", err))

pkg/utils/systemd/utils.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ type EnvFileParams struct {
1616
RelayContact string
1717
}
1818

19+
type ServiceFileParams struct {
20+
EnvFilePath string
21+
BinaryFilePath string
22+
ConfigFilePath string
23+
}
24+
1925
func CreateEnvFile(envFilePath, envTemplate string, envFileParams *EnvFileParams) {
2026
envFile, err := os.Create(envFilePath)
2127
if err != nil {
@@ -47,7 +53,7 @@ func CreateEnvFile(envFilePath, envTemplate string, envFileParams *EnvFileParams
4753
}
4854
}
4955

50-
func CreateServiceFile(serviceFilePath, serviceTemplate string) {
56+
func CreateServiceFile(serviceFilePath, serviceTemplate string, serviceFileParams *ServiceFileParams) {
5157
serviceFile, err := os.Create(serviceFilePath)
5258
if err != nil {
5359
pterm.Println()
@@ -56,14 +62,14 @@ func CreateServiceFile(serviceFilePath, serviceTemplate string) {
5662
}
5763
defer serviceFile.Close()
5864

59-
tmpl, err := template.New("service").Parse(serviceTemplate)
65+
serviceTmpl, err := template.New("service").Parse(serviceTemplate)
6066
if err != nil {
6167
pterm.Println()
6268
pterm.Error.Println(fmt.Sprintf("Failed to parse service template: %v", err))
6369
os.Exit(1)
6470
}
6571

66-
err = tmpl.Execute(serviceFile, struct{}{})
72+
err = serviceTmpl.Execute(serviceFile, struct{ EnvFilePath, BinaryFilePath, ConfigFilePath string }{EnvFilePath: serviceFileParams.EnvFilePath, BinaryFilePath: serviceFileParams.BinaryFilePath, ConfigFilePath: serviceFileParams.ConfigFilePath})
6773
if err != nil {
6874
pterm.Println()
6975
pterm.Error.Println(fmt.Sprintf("Failed to execute service template: %v", err))

0 commit comments

Comments
 (0)