Skip to content

Commit

Permalink
feat: add support for strfry29, add constants files, and refactor #14 (
Browse files Browse the repository at this point in the history
  • Loading branch information
jchiarulli authored Oct 3, 2024
1 parent 2674764 commit 727d3a9
Show file tree
Hide file tree
Showing 38 changed files with 924 additions and 551 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ The program will automate the following steps:

1. Install necessary dependencies
2. Set up a firewall
3. Configure nginx
4. Obtain a TLS certificate for HTTPS
3. Configure Nginx
4. Obtain a SSL/TLS certificate for HTTPS
5. Install the relay software
6. Set up a systemd service for your relay

Expand Down Expand Up @@ -87,14 +87,14 @@ gpg --verify rwz-x.x.x-manifest.sha512sum.asc
Here's the command to run for the latest version of `rwz`:

```sh
gpg --verify rwz-0.3.0-alpha1-manifest.sha512sum.asc
gpg --verify rwz-0.3.0-alpha2-manifest.sha512sum.asc
```

You should see output similar to the following if the verification was successful:

```sh
gpg: assuming signed data in 'rwz-0.3.0-alpha1-manifest.sha512sum'
gpg: Signature made Thu Sep 26 21:04:47 2024 EDT
gpg: assuming signed data in 'rwz-0.3.0-alpha2-manifest.sha512sum'
gpg: Signature made Thu 03 Oct 2024 07:40:12 PM UTC
gpg: using RSA key 252F57B9DCD920EBF14E6151A8841CC4D10CC288
gpg: Good signature from "NODE-TEC Devs <devs@node-tec.com>" [unknown]
gpg: aka "[jpeg image of size 5143]" [unknown]
Expand Down Expand Up @@ -124,13 +124,13 @@ sha512sum --check rwz-x.x.x-manifest.sha512sum
Here's the command to run for the latest version of `rwz`:

```sh
sha512sum --check rwz-0.3.0-alpha1-manifest.sha512sum
sha512sum --check rwz-0.3.0-alpha2-manifest.sha512sum
```

If the verification was successful you should see the output similar to the following:

```sh
rwz-0.3.0-alpha1-x86_64-linux-gnu.tar.gz: OK
rwz-0.3.0-alpha2-x86_64-linux-gnu.tar.gz: OK
```

By completing the above steps you will have successfully verified the integrity of the binary.
Expand Down
72 changes: 46 additions & 26 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/nodetec/rwz/pkg/relays/khatru29"
"github.com/nodetec/rwz/pkg/relays/khatru_pyramid"
"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/pterm/pterm"
Expand All @@ -15,7 +16,7 @@ import (
var installCmd = &cobra.Command{
Use: "install",
Short: "Install and configure your Nostr relay",
Long: `Install and configure your Nostr relay, including package installation, nginx configuration, firewall setup, SSL certificates, and starting the relay service.`,
Long: `Install and configure your Nostr relay, including package installation, firewall setup, Nginx configuration, SSL/TLS certificates, and starting the relay service.`,
Run: func(cmd *cobra.Command, args []string) {

ui.Greet()
Expand All @@ -24,7 +25,7 @@ var installCmd = &cobra.Command{
pterm.Println()

// Supported relay options
options := []string{"Khatru Pyramid", "strfry", "Khatru29", "WoT Relay"}
options := []string{"Khatru Pyramid", "strfry", "Khatru29", "strfry29", "WoT Relay"}

// Use PTerm's interactive select feature to present the options to the user and capture their selection
// The Show() method displays the options and waits for the user's input
Expand All @@ -38,27 +39,36 @@ var installCmd = &cobra.Command{
if selectedRelayOption == "Khatru Pyramid" || selectedRelayOption == "WoT Relay" {
pterm.Println()
pubkey, _ = pterm.DefaultInteractiveTextInput.Show("Public key (hex not npub)")
} else if selectedRelayOption == "Khatru29" {
} else if selectedRelayOption == "Khatru29" || selectedRelayOption == "strfry29" {
pterm.Println()
privkey, _ = pterm.DefaultInteractiveTextInput.Show("Private key (hex not nsec)")
}

var relayContact string
if selectedRelayOption == "Khatru Pyramid" || selectedRelayOption == "Khatru29" || selectedRelayOption == "WoT Relay" {
pterm.Println()
pterm.Println(pterm.Yellow("Leave email empty if you don't want to provide relay contact information."))

pterm.Println()
relayContact, _ = pterm.DefaultInteractiveTextInput.Show("Email address")
}

pterm.Println()
pterm.Println(pterm.Yellow("If you make a mistake, you can always re-run this installer."))
pterm.Println()

// Step 1: Install necessary packages using APT
manager.AptInstallPackages()

if selectedRelayOption == "Khatru Pyramid" {
// Step 2: Configure the firewall
network.ConfigureFirewall()
// Step 2: Configure the firewall
network.ConfigureFirewall()

if selectedRelayOption == "Khatru Pyramid" {
// Step 3: Configure Nginx for HTTP
khatru_pyramid.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL certificates
var httpsEnabled = network.GetCertificates(relayDomain)
// Step 4: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
khatru_pyramid.ConfigureNginxHttps(relayDomain)
Expand All @@ -68,19 +78,16 @@ var installCmd = &cobra.Command{
khatru_pyramid.InstallRelayBinary()

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

// Step 8: Show success messages
khatru_pyramid.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == "strfry" {
// Step 2: Configure the firewall
network.ConfigureFirewall()

// Step 3: Configure Nginx for HTTP
strfry.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL certificates
var httpsEnabled = network.GetCertificates(relayDomain)
// Step 4: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
strfry.ConfigureNginxHttps(relayDomain)
Expand All @@ -95,14 +102,11 @@ var installCmd = &cobra.Command{
// Step 8: Show success messages
strfry.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == "Khatru29" {
// Step 2: Configure the firewall
network.ConfigureFirewall()

// Step 3: Configure Nginx for HTTP
khatru29.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL certificates
var httpsEnabled = network.GetCertificates(relayDomain)
// Step 4: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
khatru29.ConfigureNginxHttps(relayDomain)
Expand All @@ -112,19 +116,35 @@ var installCmd = &cobra.Command{
khatru29.InstallRelayBinary()

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

// Step 8: Show success messages
khatru29.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == "WoT Relay" {
// Step 2: Configure the firewall
network.ConfigureFirewall()
} else if selectedRelayOption == "strfry29" {
// Step 3: Configure Nginx for HTTP
strfry29.ConfigureNginxHttp(relayDomain)

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

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

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

// Step 8: Show success messages
strfry29.SuccessMessages(relayDomain, httpsEnabled)
} else if selectedRelayOption == "WoT Relay" {
// Step 3: Configure Nginx for HTTP
wot_relay.ConfigureNginxHttp(relayDomain)

// Step 4: Get SSL certificates
var httpsEnabled = network.GetCertificates(relayDomain)
// Step 4: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 5: Configure Nginx for HTTPS
wot_relay.ConfigureNginxHttps(relayDomain)
Expand All @@ -134,7 +154,7 @@ var installCmd = &cobra.Command{
wot_relay.InstallRelayBinary()

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

// Step 8: Show success messages
wot_relay.SuccessMessages(relayDomain, httpsEnabled)
Expand Down
26 changes: 11 additions & 15 deletions pkg/network/certbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ package network

import (
"fmt"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/pterm/pterm"
"log"
"os/exec"
)

// Function to get SSL certificates using Certbot
// Function to get SSL/TLS certificates using Certbot
func GetCertificates(domainName string) bool {
ThemeDefault := pterm.ThemeDefault

var ThemeDefault = pterm.ThemeDefault

var prompt = pterm.InteractiveContinuePrinter{
prompt := pterm.InteractiveContinuePrinter{
DefaultValueIndex: 0,
DefaultText: "Obtain SSL certificates?",
DefaultText: "Obtain SSL/TLS certificates?",
TextStyle: &ThemeDefault.PrimaryStyle,
Options: []string{"yes", "no"},
OptionsStyle: &ThemeDefault.SuccessMessageStyle,
Expand All @@ -25,7 +23,7 @@ func GetCertificates(domainName string) bool {
}

pterm.Println()
pterm.Println(pterm.Cyan("Do you want to obtain SSL certificates using Certbot?"))
pterm.Println(pterm.Cyan("Do you want to obtain SSL/TLS certificates using Certbot?"))
pterm.Println(pterm.Cyan("If you select 'yes', then this step requires that you already have a configured domain name."))
pterm.Println(pterm.Cyan("You can always re-run this installer after you have configured your domain name."))
pterm.Println()
Expand All @@ -37,27 +35,25 @@ func GetCertificates(domainName string) bool {
}

pterm.Println()
pterm.Println(pterm.Yellow("Leave email empty if you don't want to receive notifications from Let's Encrypt about your SSL certificates."))
pterm.Println(pterm.Yellow("Leave email empty if you don't want to receive notifications from Let's Encrypt about your SSL/TLS certificates."))

pterm.Println()
email, _ := pterm.DefaultInteractiveTextInput.Show("Email address")
pterm.Println()

spinner, _ := pterm.DefaultSpinner.Start("Checking SSL certificates...")
spinner, _ := pterm.DefaultSpinner.Start("Checking SSL/TLS certificates...")

var certificatePath = fmt.Sprintf("/etc/letsencrypt/live/%s", domainName)
certificatePath := fmt.Sprintf("/etc/letsencrypt/live/%s", domainName)

// Check if certificates already exist
if files.FileExists(fmt.Sprintf("%s/fullchain.pem", certificatePath)) &&
files.FileExists(fmt.Sprintf("%s/privkey.pem", certificatePath)) &&
files.FileExists(fmt.Sprintf("%s/chain.pem", certificatePath)) {
spinner.Info("SSL certificates already exist.")
spinner.Info("SSL/TLS certificates already exist.")
return true
}

directories.CreateDirectory(fmt.Sprintf("/var/www/%s/.well-known/acme-challenge/", domainName), 0755)

spinner.UpdateText("Obtaining SSL certificates...")
spinner.UpdateText("Obtaining SSL/TLS certificates...")
if email == "" {
cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("/var/www/%s", domainName), "-d", domainName, "--agree-tos", "--no-eff-email", "-q", "--register-unsafely-without-email")
err := cmd.Run()
Expand All @@ -72,6 +68,6 @@ func GetCertificates(domainName string) bool {
}
}

spinner.Success("SSL certificates obtained successfully.")
spinner.Success("SSL/TLS certificates obtained successfully.")
return true
}
3 changes: 3 additions & 0 deletions pkg/relays/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package relays

const BinaryDestDir = "/usr/local/bin"
36 changes: 36 additions & 0 deletions pkg/relays/khatru29/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package khatru29

const DownloadURL = "https://github.com/nodetec/relays/releases/download/v0.3.0/relay29-0.4.0-khatru29-x86_64-linux-gnu.tar.gz"
const BinaryName = "khatru29"
const BinaryFilePath = "/usr/local/bin/khatru29"
const NginxConfigFilePath = "/etc/nginx/conf.d/khatru29.conf"
const DataDirPath = "/var/lib/khatru29"
const ServiceName = "khatru29"
const EnvFilePath = "/etc/systemd/system/khatru29.env"
const EnvFileTemplate = `PORT="5577"
DOMAIN="{{.Domain}}"
RELAY_NAME="Khatru29"
RELAY_PRIVKEY="{{.PrivKey}}"
RELAY_DESCRIPTION="Khatru29 Nostr Relay"
RELAY_CONTACT="{{.RelayContact}}"
DATABASE_PATH=/var/lib/khatru29/db
`
const ServiceFilePath = "/etc/systemd/system/khatru29.service"
const ServiceFileTemplate = `[Unit]
Description=Khatru29 Nostr Relay Service
After=network.target
[Service]
Type=simple
User=nostr
Group=nostr
WorkingDirectory=/home/nostr
EnvironmentFile=/etc/systemd/system/khatru29.env
ExecStart=/usr/local/bin/khatru29
Restart=on-failure
[Install]
WantedBy=multi-user.target
`
const RelayName = "Khatru29"
const GithubLink = "https://github.com/fiatjaf/relay29"
29 changes: 7 additions & 22 deletions pkg/relays/khatru29/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@ package khatru29

import (
"fmt"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/pterm/pterm"
"path/filepath"
)

// Function to download and make the binary executable
func InstallRelayBinary() {
// URL of the binary to download
const downloadURL = "https://github.com/nodetec/relays/releases/download/v0.2.0/khatru29-0.4.0-x86_64-linux-gnu.tar.gz"

// Name of the binary after downloading
const binaryName = "khatru29"

// Destination directory for the binary
const destDir = "/usr/local/bin"

// Data directory for the relay
const dataDir = "/var/lib/khatru29"

spinner, _ := pterm.DefaultSpinner.Start("Installing Khatru29 relay...")

// Ensure the data directory exists
directories.CreateDirectory(dataDir, 0755)

// Determine the file name from the URL
tmpFileName := filepath.Base(downloadURL)
tmpFileName := filepath.Base(DownloadURL)

// Temporary file path
tmpFilePath := fmt.Sprintf("/tmp/%s", tmpFileName)
Expand All @@ -37,17 +22,17 @@ func InstallRelayBinary() {
files.RemoveFile(tmpFilePath)

// Download and copy the file
files.DownloadAndCopyFile(tmpFilePath, downloadURL)
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)

// Extract binary
files.ExtractFile(tmpFilePath, destDir)
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)

// TODO
// Currently, the downloaded binary is expected to have a name that matches the binaryName variable
// Ideally, the extracted binary file should be renamed to match the binaryName variable
// Currently, the downloaded binary is expected to have a name that matches the BinaryName variable
// Ideally, the extracted binary file should be renamed to match the BinaryName variable

// Define the final destination path
destPath := filepath.Join(destDir, binaryName)
destPath := filepath.Join(relays.BinaryDestDir, BinaryName)

// Make the file executable
files.SetPermissions(destPath, 0755)
Expand Down
Loading

0 comments on commit 727d3a9

Please sign in to comment.