peer/peermap: add NewURL func

This commit is contained in:
rkonfj
2024-06-01 17:03:18 +08:00
parent 9a511afaec
commit 9cd971a35d
2 changed files with 13 additions and 7 deletions

View File

@@ -17,7 +17,7 @@ Another p2p network library in Go
#### 1. Run the pgmap daemon
```
$ pgmap -l 127.0.0.1:9987 --secret-key 5172554832d76672d1959a5ac63c5ab9 \
--stun stun.qq.com:3478 --stun stun.miwifi.com:3478
--stun stun.miwifi.com:3478 --stun stunserver.stunprotocol.org:3478
```
#### 2. Wrap pgmap as an https server
@@ -51,15 +51,13 @@ if err != nil {
panic(err)
}
pmap, err := peermap.New(peermapURL, networkSecret)
peermapServer, err := peermap.NewURL(peermapURL, networkSecret)
if err != nil {
panic(err)
}
packetConn, err := p2p.ListenPacket(
pmap,
p2p.ListenPeerID("uniqueString"), // any unique string (less than 256bytes)
)
// peerID is a unique string (less than 256bytes)
packetConn, err := p2p.ListenPacket(peermapServer, p2p.ListenPeerID("uniqueString"))
if err != nil {
panic(err)
}
@@ -83,7 +81,7 @@ for {
```go
...
packetConn, err := p2p.ListenPacket(pmap)
packetConn, err := p2p.ListenPacket(peermapServer)
if err != nil {
panic(err)
}

View File

@@ -30,6 +30,14 @@ func New(server *url.URL, store peer.SecretStore) (*Peermap, error) {
}, nil
}
func NewURL(serverURL string, store peer.SecretStore) (*Peermap, error) {
sURL, err := url.Parse(serverURL)
if err != nil {
return nil, fmt.Errorf("invalid peermap url: %w", err)
}
return New(sURL, store)
}
func (s *Peermap) SecretStore() peer.SecretStore {
return s.store
}