Skip to content

Commit

Permalink
config: allow reading externalIPs from files
Browse files Browse the repository at this point in the history
Tor hidden services dumps the onion hostname into a file on disk. If Tor
and btcd is started together -- for example in a sidecar pattern -- the
onion address is not known before executions.

This allows the user to provide --externalip a path to a file containing
any hostname, for example the onion address as dumped by the Tor
service.
  • Loading branch information
martinohansen committed Jun 16, 2022
1 parent 04aac1e commit c59b36b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions server.go
Expand Up @@ -6,6 +6,7 @@
package main

import (
"bufio"
"bytes"
"crypto/rand"
"crypto/tls"
Expand All @@ -14,6 +15,8 @@ import (
"fmt"
"math"
"net"
"os"
"path"
"runtime"
"sort"
"strconv"
Expand Down Expand Up @@ -3088,6 +3091,28 @@ func initListeners(amgr *addrmgr.AddrManager, listenAddrs []string, services wir
}

for _, sip := range cfg.ExternalIPs {

// If sip looks like a file path try and read the content and use it
// as an external IP
if strings.Contains(sip, "/") {
path := path.Clean(sip)
file, err := os.Open(path)
if err != nil {
srvrLog.Warnf("Can not open file %s: %s", path, err)
} else {

// Init a scanner and read the first line only
scanner := bufio.NewScanner(file)
scanner.Scan()

if err := scanner.Err(); err != nil {
srvrLog.Warnf("Can not read file %s: %s", path, err)
} else {
sip = scanner.Text()
}
}
}

eport := uint16(defaultPort)
host, portstr, err := net.SplitHostPort(sip)
if err != nil {
Expand All @@ -3111,6 +3136,8 @@ func initListeners(amgr *addrmgr.AddrManager, listenAddrs []string, services wir
err = amgr.AddLocalAddress(na, addrmgr.ManualPrio)
if err != nil {
amgrLog.Warnf("Skipping specified external IP: %v", err)
} else {
srvrLog.Infof("Adding %s to the advatise list", na.Addr)
}
}
} else {
Expand Down

0 comments on commit c59b36b

Please sign in to comment.