Skip to content

Commit f26c1c2

Browse files
authored
Merge pull request #18 from bcdevtools/feat/provide-addrbook.json
feat: provide addrbook.json
2 parents 343fd5c + c5f5ef6 commit f26c1c2

File tree

7 files changed

+105
-5
lines changed

7 files changed

+105
-5
lines changed

client/html/index.tmpl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
<link rel="shortcut icon" href="{[{ .favicon }]}" />
2121
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
2222
<link rel="stylesheet" href="/resources/site.css"/>
23+
{[{ if .brand }]}
2324
<style type="text/css">
25+
.watermark-brand {
26+
background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='100px' width='300px' opacity='0.05'><text transform='translate(20, 100) rotate(-25)' fill='rgb(245,45,45)' font-size='46'>{[{ .brand }]}</text></svg>");
27+
}
2428
</style>
29+
{[{ end }]}
2530
</head>
26-
<body>
31+
<body class="watermark-brand">
2732
<div id="main" class="container">
2833
<h3 class="mt-3">{[{ .description }]}</h3>
2934
{[{ if .logo }]}
@@ -61,6 +66,24 @@
6166
</div>
6267
</div>
6368
</div>
69+
<div class="accordion-item">
70+
<h3 class="accordion-header" id="headingAddrbook">
71+
<span class="accordion-button collapsed cursor-pointer" data-bs-toggle="collapse" data-bs-target="#collapseAddrbook" aria-expanded="false" aria-controls="collapseAddrbook">
72+
Addrbook
73+
</span>
74+
</h3>
75+
<div id="collapseAddrbook" class="accordion-collapse collapse" aria-labelledby="headingAddrbook" data-bs-parent="#accordionMain">
76+
<div class="card card-body">
77+
{[{ if gt .livePeersCount 0 }]}
78+
<p>Download: <a href="/download/addrbook.json">addrbook.json</a></p>
79+
<pre class="border p-2"><code>wget -O addrbook.json https://{[{ .host }]}/download/addrbook.json --inet4-only
80+
mv addrbook.json $HOME/{[{ .generalNodeHomeName }]}/config</code></pre>
81+
{[{ else }]}
82+
<h3 class="text-danger">Addrbook temporary not available</h3>
83+
{[{ end }]}
84+
</div>
85+
</div>
86+
</div>
6487
<div class="accordion-item">
6588
<h3 class="accordion-header" id="headingSnapshot">
6689
<span class="accordion-button cursor-pointer" data-bs-toggle="collapse" data-bs-target="#collapseSnapshot" aria-expanded="true" aria-controls="collapseSnapshot">
@@ -148,7 +171,7 @@ s|^(rpc_servers[[:space:]]+=[[:space:]]+).*$|\1\"$SNAP_RPC,$SNAP_RPC\"| ; \
148171
s|^(trust_height[[:space:]]+=[[:space:]]+).*$|\1$BLOCK_HEIGHT| ; \
149172
s|^(trust_hash[[:space:]]+=[[:space:]]+).*$|\1\"$TRUST_HASH\"|" $HOME/{[{ .generalNodeHomeName }]}/config/config.toml</code></pre>
150173
{[{ if gt .livePeersCount 0 }]}
151-
<p>Consider adding {[{ .livePeersCount }]} live-peers above to <b>`persistent_peers`</b> in <b>`config.toml`</b></p>
174+
<p>Consider adding {[{ .livePeersCount }]} live-peers above to <b>`persistent_peers`</b> in <b>`config.toml`</b> and download <b>`addrbook.json`</b></p>
152175
{[{ end }]}
153176
<p>Stop the node</p>
154177
<pre class="border p-2"><code>sudo systemctl stop {[{ .generalBinaryName }]}</code></pre>

client/html/resources/site.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
.cursor-pointer {
1111
cursor: pointer;
12-
}
12+
}

client/statik/statik.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package web_server
2+
3+
import (
4+
webtypes "github.com/bcdevtools/node-management/services/web_server/types"
5+
"github.com/bcdevtools/node-management/types"
6+
"github.com/bcdevtools/node-management/utils"
7+
"github.com/gin-gonic/gin"
8+
"github.com/pkg/errors"
9+
"net/http"
10+
"time"
11+
)
12+
13+
var cacheAddrBook *types.TimeBasedCache
14+
15+
func HandleDownloadAddrBook(c *gin.Context) {
16+
w := wrapGin(c)
17+
18+
addrBook, err := getAddrbook(w.Config())
19+
if err != nil {
20+
utils.PrintlnStdErr("ERR: failed to get addrbook.json:", err)
21+
w.PrepareDefaultErrorResponse().
22+
WithResult("failed to get addrbook.json").
23+
SendResponse()
24+
return
25+
}
26+
if addrBook == nil || len(addrBook.Addrs) == 0 {
27+
w.PrepareDefaultErrorResponse().
28+
WithHttpStatusCode(http.StatusServiceUnavailable).
29+
WithResult("failed to get addrbook.json").
30+
SendResponse()
31+
return
32+
}
33+
34+
c.Header("Content-Disposition", "attachment; filename=addrbook.json")
35+
c.JSON(http.StatusOK, addrBook)
36+
}
37+
38+
func getAddrbook(cfg webtypes.Config) (*types.AddrBook, error) {
39+
if addrBook := cacheAddrBook.GetRL(); addrBook != nil {
40+
return addrBook.(*types.AddrBook), nil
41+
}
42+
43+
addrBook, err := cacheAddrBook.UpdateWL(func() (any, error) {
44+
addrBook := &types.AddrBook{}
45+
if err := addrBook.ReadAddrBook(cfg.GetAddrBookFilePath()); err != nil {
46+
return nil, errors.Wrap(err, "failed to read addrbook")
47+
}
48+
49+
livePeers := addrBook.GetLivePeers(48 * time.Hour)
50+
51+
if len(livePeers) == 0 && cfg.Debug {
52+
// load random, include dead peers, on debug mode
53+
livePeers = addrBook.Addrs
54+
if len(livePeers) > 10 {
55+
livePeers = livePeers[:10]
56+
}
57+
}
58+
59+
addrBook.Addrs = livePeers
60+
61+
return addrBook, nil
62+
}, true)
63+
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
return addrBook.(*types.AddrBook), nil
69+
}
70+
71+
func init() {
72+
cacheAddrBook = types.NewTimeBasedCache(60 * time.Second)
73+
}

services/web_server/handle_node_peers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func HandleApiNodeLivePeers(c *gin.Context) {
2020
if err != nil {
2121
utils.PrintlnStdErr("ERR: failed to get live peers:", err)
2222
w.PrepareDefaultErrorResponse().WithResult("failed to get live peers").SendResponse()
23+
return
2324
}
2425

2526
w.PrepareDefaultSuccessResponse(peers).SendResponse()

services/web_server/handle_web_page.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ func HandleWebIndex(c *gin.Context) {
4646
fmt.Println(chainDescriptionLines)
4747

4848
c.HTML(http.StatusOK, "index.tmpl", gin.H{
49+
"host": c.Request.Host,
50+
"brand": cfg.Brand,
4951
"title": fmt.Sprintf("%s snapshot by %s", cfg.ChainName, cfg.Brand),
50-
"description": fmt.Sprintf("Snapshot data, live-peers for %s (%s) by %s", cfg.ChainName, cfg.ChainID, cfg.Brand),
52+
"description": fmt.Sprintf("Snapshot data, live-peers, addrbook for %s by %s", cfg.ChainName, cfg.Brand),
5153
"chainName": cfg.ChainName,
5254
"chainDescLines": chainDescriptionLines,
5355
"chainId": cfg.ChainID,

services/web_server/web_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func StartWebServer(cfg webtypes.Config) {
6060

6161
// Web
6262
r.GET("/", HandleWebIndex)
63+
r.GET("/download/addrbook.json", HandleDownloadAddrBook)
6364

6465
fmt.Println("INF: starting Web service at", binding)
6566

0 commit comments

Comments
 (0)