Skip to content

Commit 2b382e9

Browse files
feat: show peer address on http root (closes #2)
1 parent a22bf48 commit 2b382e9

File tree

6 files changed

+104
-4
lines changed

6 files changed

+104
-4
lines changed

cmd/cosmoseed/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const (
1414
)
1515

1616
var (
17-
home, chainID, seeds, logLevel, nodeKeyFile string
18-
showVersion, showNodeID, configReadOnly bool
17+
home, chainID, seeds, logLevel, nodeKeyFile, externalAddress string
18+
showVersion, showNodeID, configReadOnly bool
1919
)
2020

2121
func init() {
@@ -47,6 +47,11 @@ func init() {
4747
utils.GetString("NODE_KEY_FILE", ""),
4848
"override node key file on config.yaml",
4949
)
50+
flag.StringVar(&externalAddress,
51+
"external-address",
52+
utils.GetString("EXTERNAL_ADDRESS", ""),
53+
"external address to use in format '<host>:<port>'",
54+
)
5055

5156
flag.BoolVar(&showVersion, "version", false, "print version and exit")
5257
flag.BoolVar(&showNodeID, "show-node-id", false, "print node ID and exit")

cmd/cosmoseed/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func main() {
5353
cfg.NodeKeyFile = nodeKeyFile
5454
}
5555

56+
if externalAddress != "" {
57+
cfg.ExternalAddress = externalAddress
58+
}
59+
5660
seeder, err := cosmoseed2.NewSeeder(home, cfg)
5761
if err != nil {
5862
panic(err)

pkg/cosmoseed/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ type Config struct {
2323
PeerQueueSize int `yaml:"peerQueueSize" default:"1000"`
2424
DialWorkers int `yaml:"dialWorkers" default:"20"`
2525

26-
ChainID string `yaml:"chainID"`
27-
Seeds string `yaml:"seeds"`
26+
ChainID string `yaml:"chainID"`
27+
Seeds string `yaml:"seeds"`
28+
ExternalAddress string `yaml:"externalAddress,omitempty"`
2829

2930
ApiAddr string `yaml:"apiAddr" default:"0.0.0.0:8080"`
3031
}

pkg/cosmoseed/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ import (
66
)
77

88
func (s *Seeder) registerRoutes(mux *http.ServeMux) {
9+
mux.HandleFunc("/", s.seedAddress)
910
mux.HandleFunc("/peers", s.handlePeers)
1011
}
1112

13+
func (s *Seeder) seedAddress(w http.ResponseWriter, r *http.Request) {
14+
w.Write([]byte(s.GetFullAddress()))
15+
}
16+
1217
func (s *Seeder) handlePeers(w http.ResponseWriter, r *http.Request) {
1318
peers := s.pex.GetPeerSelection()
1419

pkg/cosmoseed/node.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"os/signal"
1010
"path"
11+
"strconv"
12+
"strings"
1113
"syscall"
1214
"time"
1315

@@ -180,6 +182,48 @@ func (s *Seeder) GetNodeID() string {
180182
return s.key.ID()
181183
}
182184

185+
func (s *Seeder) GetP2pAddress() string {
186+
if s.cfg.ExternalAddress != "" {
187+
if parts := strings.Split(s.cfg.ExternalAddress, ":"); len(parts) == 2 {
188+
return parts[0]
189+
}
190+
}
191+
192+
localIp, err := getLocalIP()
193+
if err == nil {
194+
return localIp
195+
}
196+
197+
// If everything above fails just return a default
198+
return "0.0.0.0"
199+
}
200+
201+
func (s *Seeder) GetP2pPort() int {
202+
if s.cfg.ExternalAddress != "" {
203+
if parts := strings.Split(s.cfg.ExternalAddress, ":"); len(parts) == 2 {
204+
port, err := strconv.Atoi(parts[1])
205+
if err == nil {
206+
return port
207+
}
208+
}
209+
}
210+
211+
parts := strings.Split(s.cfg.ListenAddr, ":")
212+
if len(parts) > 1 {
213+
port, err := strconv.Atoi(parts[len(parts)-1])
214+
if err == nil {
215+
return port
216+
}
217+
}
218+
219+
// If everything above fails just return default port
220+
return 26656
221+
}
222+
223+
func (s *Seeder) GetFullAddress() string {
224+
return fmt.Sprintf("%s@%s:%d", s.GetNodeID(), s.GetP2pAddress(), s.GetP2pPort())
225+
}
226+
183227
func generateP2PConfig(home string, cfg *Config) *config.P2PConfig {
184228
p2pConfig := config.DefaultP2PConfig()
185229

pkg/cosmoseed/utils.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cosmoseed
22

33
import (
44
"fmt"
5+
"net"
56
"os"
67
"path/filepath"
78
"strings"
@@ -30,3 +31,43 @@ func splitAndTrimEmpty(s, sep, cutset string) []string {
3031
}
3132
return nonEmptyStrings
3233
}
34+
35+
func getLocalIP() (string, error) {
36+
ifaces, err := net.Interfaces()
37+
if err != nil {
38+
return "", err
39+
}
40+
for _, iface := range ifaces {
41+
// Skip interfaces that are down or loopback
42+
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
43+
continue
44+
}
45+
46+
addrs, err := iface.Addrs()
47+
if err != nil {
48+
continue
49+
}
50+
51+
for _, addr := range addrs {
52+
var ip net.IP
53+
54+
switch v := addr.(type) {
55+
case *net.IPNet:
56+
ip = v.IP
57+
case *net.IPAddr:
58+
ip = v.IP
59+
}
60+
61+
if ip == nil || ip.IsLoopback() {
62+
continue
63+
}
64+
65+
// Return the first IPv4 address
66+
ip = ip.To4()
67+
if ip != nil {
68+
return ip.String(), nil
69+
}
70+
}
71+
}
72+
return "", fmt.Errorf("no connected network interface found")
73+
}

0 commit comments

Comments
 (0)