Skip to content

Commit

Permalink
Read passphrase from either passphrase file, if present
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenson committed Nov 20, 2023
1 parent 34e2e4d commit 6e64b11
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
11 changes: 6 additions & 5 deletions tor-miner/cmd/tor-miner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package main

import (
"context"
"errors"
"fmt"
"os"

"gbenson.net/tor-miner"
)

func main() {
if len(os.Args) < 2 {
fmt.Println("usage: tor-miner CONFIG_PASSPHRASE [XMRIG_ARGS...]")
os.Exit(2)
}

app := miner.Runner{}
if err := app.Run(context.Background()); err != nil {
if errors.Is(err, miner.UsageError) {
fmt.Println(err)
os.Exit(2)
}

fmt.Println("tor-miner:", err)
os.Exit(1)
}
Expand Down
46 changes: 38 additions & 8 deletions tor-miner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package miner
import (
"context"
"encoding/base32"
"errors"
"fmt"
"io/fs"
"net"
net_url "net/url"
"os"
Expand All @@ -15,12 +17,15 @@ import (
)

const (
DefaultMinerPath = "xmrig"
TorProxyAddr = "127.0.0.1:9050"
TorStartupTimeout = time.Minute
DefaultAPIAddr = "127.0.0.1:3638"
DefaultMinerPath = "xmrig"
TorProxyAddr = "127.0.0.1:9050"
TorStartupTimeout = time.Minute
DefaultAPIAddr = "127.0.0.1:3638"
)

var UsageError = errors.New(
"usage: tor-miner [CONFIG_PASSPHRASE] [XMRIG_ARGS...]")

var httpAPI = regexp.MustCompile(`HTTP API\s+(\S+:\d+)\n`)

type Runner struct {
Expand All @@ -36,24 +41,26 @@ type Runner struct {
}

func (r *Runner) Run(ctx context.Context) error {
if len(os.Args) < 2 {
return panique("invocation error")
password, err := configPassword()
if err != nil {
return err
}
if r.isStarted {
return panique("already started")
}
r.isStarted = true

config, err := DefaultConfig(os.Args[1])
config, err := DefaultConfig(password)
if err != nil {
return err
}
password = ""

if r.MinerPath == "" {
r.MinerPath = DefaultMinerPath
}
if r.MinerArgs == nil {
r.MinerArgs = os.Args[2:]
r.MinerArgs = os.Args[1:]
}
if r.LocalAPI.URL == "" {
r.LocalAPI.URL = "http://" + DefaultAPIAddr
Expand Down Expand Up @@ -179,6 +186,29 @@ func (r *Runner) Run(ctx context.Context) error {
return monitor(cmds, &r.LocalAPI, r.onionAPI, &config.Monitor)
}

func configPassword() (string, error) {
if len(os.Args) < 2 || strings.HasPrefix(os.Args[1], "-") {
for _, filename := range []string{
"/run/secrets/tor_miner_config_passphrase",
"/etc/tor-miner/config_passphrase",
} {
bytes, err := os.ReadFile(filename)
if err == nil {
return strings.TrimSpace(string(bytes)), nil
} else if !errors.Is(err, fs.ErrNotExist) {
return "", err
}
msg, _ := strings.CutPrefix(err.Error(), "open ")
fmt.Println("tor-miner:", msg)
}

return "", UsageError
}
password := os.Args[1]
os.Args = append(os.Args[:1], os.Args[2:]...)
return password, nil
}

func (r *Runner) dryRun(ctx context.Context) (string, error) {
cmd := r.newMinerCommand(ctx, "--dry-run")
out, err := cmd.CombinedOutput()
Expand Down

0 comments on commit 6e64b11

Please sign in to comment.