mirror of https://github.com/schollz/croc.git
update dependencies
This commit is contained in:
parent
90f1277ee5
commit
346284cbed
|
@ -52,8 +52,8 @@
|
|||
[[projects]]
|
||||
name = "github.com/schollz/peerdiscovery"
|
||||
packages = ["."]
|
||||
revision = "2665d149a222cb3cf607ef0cf70ce7909462c50f"
|
||||
version = "v0.1.0"
|
||||
revision = "94ddd4cf05a8a6052e334c69ac1cd86486de10af"
|
||||
version = "v1.1.0"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/schollz/progressbar"
|
||||
|
@ -88,41 +88,25 @@
|
|||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/yudai/hcl"
|
||||
packages = [
|
||||
".",
|
||||
"hcl",
|
||||
"json"
|
||||
]
|
||||
packages = [".","hcl","json"]
|
||||
revision = "5fa2393b3552119bf33a69adb1402a1160cba23d"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/crypto"
|
||||
packages = [
|
||||
"pbkdf2",
|
||||
"scrypt",
|
||||
"ssh/terminal"
|
||||
]
|
||||
packages = ["pbkdf2","scrypt","ssh/terminal"]
|
||||
revision = "2b6c08872f4b66da917bb4ce98df4f0307330f78"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/net"
|
||||
packages = [
|
||||
"bpf",
|
||||
"internal/iana",
|
||||
"internal/socket",
|
||||
"ipv4"
|
||||
]
|
||||
packages = ["bpf","internal/iana","internal/socket","ipv4"]
|
||||
revision = "5f9ae10d9af5b1c89ae6904293b14b064d4ada23"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/sys"
|
||||
packages = [
|
||||
"unix",
|
||||
"windows"
|
||||
]
|
||||
packages = ["unix","windows"]
|
||||
revision = "79b0c6888797020a994db17c8510466c72fe75d9"
|
||||
|
||||
[[projects]]
|
||||
|
@ -140,6 +124,6 @@
|
|||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "4145287e28323d22a74771b8f17cc9a94d13e6297e0de995820e1bc7953948d7"
|
||||
inputs-digest = "93fd6df35a71aeabf37bf934c95bab3e099b86f74ad0479e0ce491695a996527"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
name = "github.com/schollz/mnemonicode"
|
||||
|
||||
[[constraint]]
|
||||
version = "0.1.0"
|
||||
version = "1.1.0"
|
||||
name = "github.com/schollz/peerdiscovery"
|
||||
|
||||
[[constraint]]
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
[![travis](https://travis-ci.org/schollz/peerdiscovery.svg?branch=master)](https://travis-ci.org/schollz/peerdiscovery)
|
||||
[![go report card](https://goreportcard.com/badge/github.com/schollz/peerdiscovery)](https://goreportcard.com/report/github.com/schollz/peerdiscovery)
|
||||
[![coverage](https://img.shields.io/badge/coverage-83%25-brightgreen.svg)](https://gocover.io/github.com/schollz/peerdiscovery)
|
||||
[![coverage](https://img.shields.io/badge/coverage-76%25-brightgreen.svg)](https://gocover.io/github.com/schollz/peerdiscovery)
|
||||
[![godocs](https://godoc.org/github.com/schollz/peerdiscovery?status.svg)](https://godoc.org/github.com/schollz/peerdiscovery)
|
||||
|
||||
Pure-go library for cross-platform thread-safe local peer discovery using UDP broadcast. I needed to use peer discovery for [croc](https://github.com/schollz/croc) and everything I tried had problems, so I made another one.
|
||||
|
@ -21,8 +21,7 @@ go get -u github.com/schollz/peerdiscovery
|
|||
The following is a code to find the first peer on the local network and print it out.
|
||||
|
||||
```golang
|
||||
p, _ := peerdiscovery.New(peerdiscovery.Settings{Limit: 1})
|
||||
discoveries, _ := p.Discover()
|
||||
discoveries, _ := peerdiscovery.Discover(peerdiscovery.Settings{Limit: 1})
|
||||
for _, d := range discoveries {
|
||||
fmt.Printf("discovered '%s'\n", d.Address)
|
||||
}
|
||||
|
|
|
@ -23,17 +23,14 @@ func main() {
|
|||
}()
|
||||
|
||||
// discover peers
|
||||
p, err := peerdiscovery.New(peerdiscovery.Settings{
|
||||
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
|
||||
Limit: -1,
|
||||
Payload: []byte(randStringBytesMaskImprSrc(10)),
|
||||
Delay: 500 * time.Millisecond,
|
||||
TimeLimit: 10 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
discoveries, err := p.Discover()
|
||||
// print out results
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
|
|
|
@ -26,11 +26,12 @@ type Settings struct {
|
|||
// Limit is the number of peers to discover, use < 1 for unlimited.
|
||||
Limit int
|
||||
// Port is the port to broadcast on (the peers must also broadcast using the same port).
|
||||
// The default port is 999.
|
||||
// The default port is 9999.
|
||||
Port string
|
||||
// MulticastAddress specifies the multicast address.
|
||||
// You should be able to use any between 224.0.0.0 to 239.255.255.255.
|
||||
// By default it uses the Simple Service Discovery Protocol address (239.255.255.250).
|
||||
// By default it uses the Simple Service Discovery Protocol
|
||||
// address (239.255.255.250).
|
||||
MulticastAddress string
|
||||
// Payload is the bytes that are sent out with each broadcast. Must be short.
|
||||
Payload []byte
|
||||
|
@ -44,23 +45,25 @@ type Settings struct {
|
|||
multicastAddressNumbers []uint8
|
||||
}
|
||||
|
||||
// PeerDiscovery is the object that can do the discovery for finding LAN peers.
|
||||
type PeerDiscovery struct {
|
||||
// peerDiscovery is the object that can do the discovery for finding LAN peers.
|
||||
type peerDiscovery struct {
|
||||
settings Settings
|
||||
|
||||
received map[string][]byte
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// New returns a new PeerDiscovery object which can be used to discover peers.
|
||||
// initialize returns a new peerDiscovery object which can be used to discover peers.
|
||||
// The settings are optional. If any setting is not supplied, then defaults are used.
|
||||
// See the Settings for more information.
|
||||
func New(settings ...Settings) (p *PeerDiscovery, err error) {
|
||||
p = new(PeerDiscovery)
|
||||
func initialize(settings Settings) (p *peerDiscovery, err error) {
|
||||
p = new(peerDiscovery)
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
if len(settings) > 0 {
|
||||
p.settings = settings[0]
|
||||
}
|
||||
|
||||
// initialize settings
|
||||
p.settings = settings
|
||||
|
||||
// defaults
|
||||
if p.settings.Port == "" {
|
||||
p.settings.Port = "9999"
|
||||
|
@ -97,7 +100,16 @@ func New(settings ...Settings) (p *PeerDiscovery, err error) {
|
|||
// Discover will use the created settings to scan for LAN peers. It will return
|
||||
// an array of the discovered peers and their associate payloads. It will not
|
||||
// return broadcasts sent to itself.
|
||||
func (p *PeerDiscovery) Discover() (discoveries []Discovered, err error) {
|
||||
func Discover(settings ...Settings) (discoveries []Discovered, err error) {
|
||||
s := Settings{}
|
||||
if len(settings) > 0 {
|
||||
s = settings[0]
|
||||
}
|
||||
p, err := initialize(s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.RLock()
|
||||
address := p.settings.MulticastAddress + ":" + p.settings.Port
|
||||
portNum := p.settings.portNum
|
||||
|
@ -188,12 +200,13 @@ func (p *PeerDiscovery) Discover() (discoveries []Discovered, err error) {
|
|||
}
|
||||
|
||||
const (
|
||||
maxDatagramSize = 65507
|
||||
// https://en.wikipedia.org/wiki/User_Datagram_Protocol#Packet_structure
|
||||
maxDatagramSize = 66507
|
||||
)
|
||||
|
||||
// Listen binds to the UDP address and port given and writes packets received
|
||||
// from that address to a buffer which is passed to a hander
|
||||
func (p *PeerDiscovery) listen() (recievedBytes []byte, err error) {
|
||||
func (p *peerDiscovery) listen() (recievedBytes []byte, err error) {
|
||||
p.RLock()
|
||||
address := p.settings.MulticastAddress + ":" + p.settings.Port
|
||||
portNum := p.settings.portNum
|
||||
|
|
|
@ -7,34 +7,14 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSettings(t *testing.T) {
|
||||
_, err := New()
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = New(Settings{
|
||||
Limit: -1,
|
||||
Payload: []byte("payload"),
|
||||
Delay: 500 * time.Millisecond,
|
||||
TimeLimit: 10 * time.Second,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = New(Settings{
|
||||
MulticastAddress: "assd.asdf.asdf.asfd",
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestDiscovery(t *testing.T) {
|
||||
p, _ := New(Settings{
|
||||
// should not be able to "discover" itself
|
||||
discoveries, err := Discover(Settings{
|
||||
Limit: -1,
|
||||
Payload: []byte("payload"),
|
||||
Delay: 500 * time.Millisecond,
|
||||
TimeLimit: 5 * time.Second,
|
||||
})
|
||||
|
||||
// should not be able to "discover" itself
|
||||
discoveries, err := p.Discover()
|
||||
assert.Nil(t, err)
|
||||
assert.Zero(t, len(discoveries))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue