mirror of
https://github.com/sigcn/pg.git
synced 2025-09-27 01:05:51 +08:00
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package fileshare
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/url"
|
|
|
|
"github.com/rkonfj/peerguard/p2p"
|
|
"github.com/rkonfj/peerguard/peer"
|
|
"github.com/rkonfj/peerguard/peer/peermap"
|
|
)
|
|
|
|
type PublicNetwork struct {
|
|
Name string
|
|
Server string
|
|
PrivateKey string
|
|
}
|
|
|
|
func (pn *PublicNetwork) ListenPacket(udpPort int) (net.PacketConn, error) {
|
|
pmapURL, err := url.Parse(pn.Server)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid peermap URL: %w", err)
|
|
}
|
|
network := cmp.Or(pn.Name, "pubnet")
|
|
pmap, err := peermap.New(pmapURL, &peer.NetworkSecret{Network: network, Secret: network})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create peermap failed: %w", err)
|
|
}
|
|
return p2p.ListenPacket(pmap, pn.secureOption(), p2p.ListenUDPPort(udpPort))
|
|
}
|
|
|
|
func (pn *PublicNetwork) secureOption() p2p.Option {
|
|
if len(pn.PrivateKey) > 0 {
|
|
return p2p.ListenPeerCurve25519(pn.PrivateKey)
|
|
}
|
|
return p2p.ListenPeerSecure()
|
|
}
|
|
|
|
type ProgressBar interface {
|
|
io.Writer
|
|
Add(progress int) error
|
|
}
|
|
|
|
type NopProgress struct {
|
|
}
|
|
|
|
func (w NopProgress) Write(p []byte) (n int, err error) {
|
|
n = len(p)
|
|
return
|
|
}
|
|
|
|
func (w NopProgress) Add(int) error { return nil }
|