Skip to content

Commit 2032917

Browse files
committed
feat: add private network support with preshared key option
1 parent 95e532a commit 2032917

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

main.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/hex"
56
"encoding/json"
67
"flag"
78
"fmt"
@@ -19,6 +20,7 @@ import (
1920
"github.com/libp2p/go-libp2p/core/host"
2021
"github.com/libp2p/go-libp2p/core/network"
2122
"github.com/libp2p/go-libp2p/core/peer"
23+
"github.com/libp2p/go-libp2p/core/pnet"
2224
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
2325
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
2426
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
@@ -102,6 +104,7 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
102104
func main() {
103105
name := flag.String("name", "", "Your node name")
104106
bootstrap := flag.String("bootstrap", "", "Comma-separated list of bootstrap node multiaddrs (overrides defaults)")
107+
pskString := flag.String("psk", "", "Preshared key for private network (hex encoded, 64 characters)")
105108
flag.Parse()
106109

107110
if *name == "" {
@@ -118,9 +121,19 @@ func main() {
118121
bootstrapPeers = bootstrapNodes
119122
}
120123

124+
var psk pnet.PSK
125+
if *pskString != "" {
126+
var err error
127+
psk, err = parsePSK(*pskString)
128+
if err != nil {
129+
log.Fatalf("Failed to parse PSK: %v", err)
130+
}
131+
fmt.Println("Using private network with preshared key")
132+
}
133+
121134
ctx, cancel := context.WithCancel(context.Background())
122135

123-
h, err := createHost(ctx)
136+
h, err := createHost(ctx, psk)
124137
if err != nil {
125138
log.Fatalf("Failed to create host: %v", err)
126139
}
@@ -216,16 +229,42 @@ func main() {
216229
}
217230
}
218231

219-
func createHost(ctx context.Context) (host.Host, error) {
220-
return libp2p.New(
232+
func createHost(ctx context.Context, psk pnet.PSK) (host.Host, error) {
233+
opts := []libp2p.Option{
221234
libp2p.ListenAddrStrings(
222235
"/ip4/0.0.0.0/tcp/0",
223236
"/ip6/::/tcp/0",
224237
),
225238
libp2p.EnableNATService(),
226239
libp2p.EnableHolePunching(),
227240
libp2p.EnableRelay(),
228-
)
241+
}
242+
243+
if psk != nil {
244+
opts = append(opts, libp2p.PrivateNetwork(psk))
245+
}
246+
247+
return libp2p.New(opts...)
248+
}
249+
250+
func parsePSK(s string) (pnet.PSK, error) {
251+
s = strings.TrimSpace(s)
252+
s = strings.TrimPrefix(s, "0x")
253+
s = strings.TrimPrefix(s, "0X")
254+
255+
data, err := hex.DecodeString(s)
256+
if err != nil {
257+
return nil, fmt.Errorf("decoding hex PSK: %w", err)
258+
}
259+
260+
if len(data) != 32 {
261+
return nil, fmt.Errorf("PSK must be 32 bytes (64 hex characters), got %d bytes", len(data))
262+
}
263+
264+
var psk [32]byte
265+
copy(psk[:], data)
266+
267+
return psk[:], nil
229268
}
230269

231270
func connectToBootstrapNodes(ctx context.Context, h host.Host, bootstrapPeers []string) {

0 commit comments

Comments
 (0)