Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor nostr user creation, remove root from nginx https fil… #62

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 50 additions & 36 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package cmd

import (
"fmt"

"github.com/nodetec/rwz/pkg/manager"
"github.com/nodetec/rwz/pkg/network"
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/relays/khatru29"
"github.com/nodetec/rwz/pkg/relays/khatru_pyramid"
"github.com/nodetec/rwz/pkg/relays/nostr_rs_relay"
"github.com/nodetec/rwz/pkg/relays/strfry"
"github.com/nodetec/rwz/pkg/relays/strfry29"
"github.com/nodetec/rwz/pkg/relays/wot_relay"
"github.com/nodetec/rwz/pkg/ui"
"github.com/nodetec/rwz/pkg/utils/users"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -84,119 +88,129 @@ var installCmd = &cobra.Command{
// Step 2: Configure the firewall
network.ConfigureFirewall()

// Setp 3: Create relay user
spinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Checking if '%s' user exists...", relays.User))
if !users.UserExists(relays.User) {
spinner.UpdateText(fmt.Sprintf("Creating '%s' user...", relays.User))
users.CreateUser(relays.User, true)
spinner.Success(fmt.Sprintf("Created '%s' user.", relays.User))
} else {
spinner.Success(fmt.Sprintf("'%s' user already exists.", relays.User))
}

if selectedRelayOption == khatru_pyramid.RelayName {
// Step 3: Configure Nginx for HTTP
// Step 4: Configure Nginx for HTTP
khatru_pyramid.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL/TLS certificates
// Step 5: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
// Step 6: Configure Nginx for HTTPS
khatru_pyramid.ConfigureNginxHttps(relayDomain)
}

// Step 6: Download and install the relay binary
// Step 7: Download and install the relay binary
khatru_pyramid.InstallRelayBinary()

// Step 7: Set up the relay service
// Step 8: Set up the relay service
khatru_pyramid.SetupRelayService(relayDomain, pubKey, relayContact)

// Step 8: Show success messages
// Step 9: Show success messages
khatru_pyramid.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == nostr_rs_relay.RelayName {
// Step 3: Configure Nginx for HTTP
// Step 4: Configure Nginx for HTTP
nostr_rs_relay.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL/TLS certificates
// Step 5: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
// Step 6: Configure Nginx for HTTPS
nostr_rs_relay.ConfigureNginxHttps(relayDomain)
}

// Step 6: Download and install the relay binary
// Step 7: Download and install the relay binary
nostr_rs_relay.InstallRelayBinary()

// Step 7: Set up the relay service
// Step 8: Set up the relay service
nostr_rs_relay.SetupRelayService(relayDomain, pubKey, relayContact, httpsEnabled)

// Step 8: Show success messages
// Step 9: Show success messages
nostr_rs_relay.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == strfry.RelayName {
// Step 3: Configure Nginx for HTTP
// Step 4: Configure Nginx for HTTP
strfry.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL/TLS certificates
// Step 5: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
// Step 6: Configure Nginx for HTTPS
strfry.ConfigureNginxHttps(relayDomain)
}

// Step 6: Download and install the relay binary
// Step 7: Download and install the relay binary
strfry.InstallRelayBinary()

// Step 7: Set up the relay service
// Step 8: Set up the relay service
strfry.SetupRelayService(relayDomain)

// Step 8: Show success messages
// Step 9: Show success messages
strfry.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == wot_relay.RelayName {
// Step 3: Configure Nginx for HTTP
// Step 4: Configure Nginx for HTTP
wot_relay.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL/TLS certificates
// Step 5: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
// Step 6: Configure Nginx for HTTPS
wot_relay.ConfigureNginxHttps(relayDomain)
}

// Step 6: Download and install the relay binary
// Step 7: Download and install the relay binary
wot_relay.InstallRelayBinary()

// Step 7: Set up the relay service
// Step 8: Set up the relay service
wot_relay.SetupRelayService(relayDomain, pubKey, relayContact, httpsEnabled)

// Step 8: Show success messages
// Step 9: Show success messages
wot_relay.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == khatru29.RelayName {
// Step 3: Configure Nginx for HTTP
// Step 4: Configure Nginx for HTTP
khatru29.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL/TLS certificates
// Step 5: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
// Step 6: Configure Nginx for HTTPS
khatru29.ConfigureNginxHttps(relayDomain)
}

// Step 6: Download and install the relay binary
// Step 7: Download and install the relay binary
khatru29.InstallRelayBinary()

// Step 7: Set up the relay service
// Step 8: Set up the relay service
khatru29.SetupRelayService(relayDomain, privKey, relayContact)

// Step 8: Show success messages
// Step 9: Show success messages
khatru29.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == strfry29.RelayName {
// Step 3: Configure Nginx for HTTP
// Step 4: Configure Nginx for HTTP
strfry29.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL/TLS certificates
// Step 5: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
// Step 6: Configure Nginx for HTTPS
strfry29.ConfigureNginxHttps(relayDomain)
}

// Step 6: Download and install the relay binary
// Step 7: Download and install the relay binary
strfry29.InstallRelayBinary()

// Step 7: Set up the relay service
// Step 8: Set up the relay service
strfry29.SetupRelayService(relayDomain, privKey)

// Step 8: Show success messages
// Step 9: Show success messages
strfry29.SuccessMessages(relayDomain, httpsEnabled)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func AptInstallPackages(selectedRelayOption string) {

exec.Command("apt", "update", "-qq").Run()

packages := []string{"nginx", "certbot", "python3-certbot-nginx", "ufw", "fail2ban", "git"}
packages := []string{"nginx", "certbot", "python3-certbot-nginx", "ufw", "fail2ban"}

if selectedRelayOption == nostr_rs_relay.RelayName || selectedRelayOption == strfry.RelayName || selectedRelayOption == strfry29.RelayName {
packages = append(packages, "git")
Expand Down
1 change: 1 addition & 0 deletions pkg/relays/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ package relays

const BinaryDestDir = "/usr/local/bin"
const TmpDirPath = "/tmp"
const User = "nostr"
4 changes: 1 addition & 3 deletions pkg/relays/khatru29/nginx_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ server {
listen [::]:443 ssl http2;
server_name %s;

root %s/%s;

location / {
proxy_pass http://khatru29_websocket;
proxy_http_version 1.1;
Expand Down Expand Up @@ -121,7 +119,7 @@ server {
return 301 https://%s$request_uri;
}
}
`, domainName, network.WWWDirPath, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)
`, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)

files.WriteFile(NginxConfigFilePath, configContent, 0644)

Expand Down
12 changes: 2 additions & 10 deletions pkg/relays/khatru29/service.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package khatru29

import (
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/nodetec/rwz/pkg/utils/systemd"
"github.com/nodetec/rwz/pkg/utils/users"
"github.com/pterm/pterm"
)

// Function to set up the relay service
func SetupRelayService(domain, privKey, relayContact string) {
spinner, _ := pterm.DefaultSpinner.Start("Configuring relay service...")

// Ensure the user for the relay service exists
if !users.UserExists("nostr") {
spinner.UpdateText("Creating user 'nostr'...")
users.CreateUser("nostr", true)
} else {
spinner.UpdateText("User 'nostr' already exists")
}

// Ensure the data directory exists and set ownership
spinner.UpdateText("Creating data directory...")
directories.CreateDirectory(DataDirPath, 0755)

// Use chown command to set ownership of the data directory to the nostr user
directories.SetOwnerAndGroup("nostr", "nostr", DataDirPath)
directories.SetOwnerAndGroup(relays.User, relays.User, DataDirPath)

// Check if the environment file exists and remove it if it does
files.RemoveFile(EnvFilePath)
Expand Down
4 changes: 1 addition & 3 deletions pkg/relays/khatru_pyramid/nginx_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ server {
listen [::]:443 ssl http2;
server_name %s;

root %s/%s;

location / {
proxy_pass http://khatru_pyramid_websocket;
proxy_http_version 1.1;
Expand Down Expand Up @@ -121,7 +119,7 @@ server {
return 301 https://%s$request_uri;
}
}
`, domainName, network.WWWDirPath, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)
`, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)

files.WriteFile(NginxConfigFilePath, configContent, 0644)

Expand Down
12 changes: 2 additions & 10 deletions pkg/relays/khatru_pyramid/service.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package khatru_pyramid

import (
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/nodetec/rwz/pkg/utils/systemd"
"github.com/nodetec/rwz/pkg/utils/users"
"github.com/pterm/pterm"
)

// Function to set up the relay service
func SetupRelayService(domain, pubKey, relayContact string) {
spinner, _ := pterm.DefaultSpinner.Start("Configuring relay service...")

// Ensure the user for the relay service exists
if !users.UserExists("nostr") {
spinner.UpdateText("Creating user 'nostr'...")
users.CreateUser("nostr", true)
} else {
spinner.UpdateText("User 'nostr' already exists")
}

// Ensure the data directory exists and set ownership
spinner.UpdateText("Creating data directory...")
directories.CreateDirectory(DataDirPath, 0755)

// Use chown command to set ownership of the data directory to the nostr user
directories.SetOwnerAndGroup("nostr", "nostr", DataDirPath)
directories.SetOwnerAndGroup(relays.User, relays.User, DataDirPath)

// Check if the environment file exists and remove it if it does
files.RemoveFile(EnvFilePath)
Expand Down
4 changes: 1 addition & 3 deletions pkg/relays/nostr_rs_relay/nginx_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ server {
listen [::]:443 ssl http2;
server_name %s;

root %s/%s;

location / {
proxy_pass http://nostr_rs_relay_websocket;
proxy_http_version 1.1;
Expand Down Expand Up @@ -120,7 +118,7 @@ server {
return 301 https://%s$request_uri;
}
}
`, domainName, network.WWWDirPath, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)
`, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)

files.WriteFile(NginxConfigFilePath, configContent, 0644)

Expand Down
16 changes: 4 additions & 12 deletions pkg/relays/nostr_rs_relay/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,31 @@ package nostr_rs_relay

import (
"fmt"
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/nodetec/rwz/pkg/utils/network"
"github.com/nodetec/rwz/pkg/utils/systemd"
"github.com/nodetec/rwz/pkg/utils/users"
"github.com/pterm/pterm"
)

// Function to set up the relay service
func SetupRelayService(domain, pubKey, relayContact string, httpsEnabled bool) {
spinner, _ := pterm.DefaultSpinner.Start("Configuring relay service...")

// Ensure the user for the relay service exists
if !users.UserExists("nostr") {
spinner.UpdateText("Creating user 'nostr'...")
users.CreateUser("nostr", true)
} else {
spinner.UpdateText("User 'nostr' already exists")
}

// Ensure the data directory exists and set ownership
spinner.UpdateText("Creating data directory...")
directories.CreateDirectory(DataDirPath, 0755)

// Use chown command to set ownership of the data directory to the nostr user
directories.SetOwnerAndGroup("nostr", "nostr", DataDirPath)
directories.SetOwnerAndGroup(relays.User, relays.User, DataDirPath)

// Ensure the config directory exists and set ownership
spinner.UpdateText("Creating config directory...")
directories.CreateDirectory(ConfigDirPath, 0755)

// Use chown command to set ownership of the config directory to the nostr user
directories.SetOwnerAndGroup("nostr", "nostr", ConfigDirPath)
directories.SetOwnerAndGroup(relays.User, relays.User, ConfigDirPath)

// Check for and remove existing config file
files.RemoveFile(ConfigFilePath)
Expand Down Expand Up @@ -64,7 +56,7 @@ func SetupRelayService(domain, pubKey, relayContact string, httpsEnabled bool) {
files.CopyFile(TmpConfigFilePath, ConfigDirPath)

// Use chown command to set ownership of the config file to the nostr user
files.SetOwnerAndGroup("nostr", "nostr", ConfigFilePath)
files.SetOwnerAndGroup(relays.User, relays.User, ConfigFilePath)

// Create the systemd service file
spinner.UpdateText("Creating service file...")
Expand Down
4 changes: 1 addition & 3 deletions pkg/relays/strfry/nginx_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ server {
listen [::]:443 ssl http2;
server_name %s;

root %s/%s;

location / {
proxy_pass http://strfry_websocket;
proxy_http_version 1.1;
Expand Down Expand Up @@ -121,7 +119,7 @@ server {
return 301 https://%s$request_uri;
}
}
`, domainName, network.WWWDirPath, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)
`, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)

files.WriteFile(NginxConfigFilePath, configContent, 0644)

Expand Down
Loading
Loading