mirror of https://github.com/schollz/croc.git
update dependencies
This commit is contained in:
parent
f0f7a43888
commit
bd4eaf90df
|
@ -50,10 +50,10 @@
|
|||
revision = "15c9654387fad6d257aa28f9be57b9f124101955"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/schollz/peerdiscovery"
|
||||
packages = ["."]
|
||||
revision = "5485f2fb8e48e812ac8c0387729e4952603bfaff"
|
||||
revision = "2665d149a222cb3cf607ef0cf70ce7909462c50f"
|
||||
version = "v0.1.0"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/schollz/progressbar"
|
||||
|
@ -140,6 +140,6 @@
|
|||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "9fcf09b3c3f693e027cf26c3b1721bea6ef6626a52385f7ed093433f72b8c6d6"
|
||||
inputs-digest = "4145287e28323d22a74771b8f17cc9a94d13e6297e0de995820e1bc7953948d7"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
name = "github.com/schollz/mnemonicode"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
version = "0.1.0"
|
||||
name = "github.com/schollz/peerdiscovery"
|
||||
|
||||
[[constraint]]
|
||||
|
|
|
@ -169,7 +169,7 @@ func (c *Connection) Run() error {
|
|||
c.DontEncrypt = true
|
||||
c.Yes = true
|
||||
if c.Code == "" {
|
||||
c.Code = peerdiscovery.RandStringBytesMaskImprSrc(4)
|
||||
c.Code = strings.Split(GetRandomName(), "-")[0]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- tip
|
|
@ -1,2 +1,55 @@
|
|||
# peerdiscovery
|
||||
Discover LAN peers using UDP
|
||||
|
||||
[![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)
|
||||
[![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.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
Make sure you have Go 1.5+.
|
||||
|
||||
```
|
||||
go get -u github.com/schollz/peerdiscovery
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
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()
|
||||
for _, d := range discoveries {
|
||||
fmt.Printf("discovered '%s'\n", d.Address)
|
||||
}
|
||||
```
|
||||
|
||||
Here's the output when running on two computers. (*Run these gifs in sync by hitting Ctl + F5*).
|
||||
|
||||
**Computer 1:**
|
||||
|
||||
![computer 1](https://user-images.githubusercontent.com/6550035/39165714-ba7167d8-473a-11e8-82b5-fb7401ce2138.gif)
|
||||
|
||||
**Computer 2:**
|
||||
|
||||
![computer 1](https://user-images.githubusercontent.com/6550035/39165716-ba8db9ec-473a-11e8-96f7-e8c64faac676.gif)
|
||||
|
||||
For more examples, see [the scanning example](https://github.com/schollz/peerdiscovery/blob/master/examples/main.go) or [the docs](https://godoc.org/github.com/schollz/peerdiscovery).
|
||||
|
||||
|
||||
## Contributing
|
||||
|
||||
Pull requests are welcome. Feel free to...
|
||||
|
||||
- Revise documentation
|
||||
- Add new features
|
||||
- Fix bugs
|
||||
- Suggest improvements
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/schollz/peerdiscovery"
|
||||
)
|
||||
|
||||
func main() {
|
||||
p, err := peerdiscovery.New(peerdiscovery.Settings{
|
||||
Limit: -1,
|
||||
Payload: []byte(peerdiscovery.RandStringBytesMaskImprSrc(10)),
|
||||
Delay: 500 * time.Millisecond,
|
||||
TimeLimit: 10 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
discoveries, err := p.Discover()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
for _, d := range discoveries {
|
||||
log.Printf("discovered ip '%s' with payload '%s'", d.Address, d.Payload)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,43 +1,51 @@
|
|||
package peerdiscovery
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
math_rand "math/rand"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/schollz/peerdiscovery"
|
||||
"github.com/schollz/progressbar"
|
||||
)
|
||||
|
||||
// GetLocalIP returns the local ip address
|
||||
func GetLocalIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
func main() {
|
||||
fmt.Println("Scanning for 10 seconds to find LAN peers")
|
||||
// show progress bar
|
||||
go func() {
|
||||
bar := progressbar.New(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
bar.Add(1)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
fmt.Print("\n")
|
||||
}()
|
||||
|
||||
// discover peers
|
||||
p, err := peerdiscovery.New(peerdiscovery.Settings{
|
||||
Limit: -1,
|
||||
Payload: []byte(randStringBytesMaskImprSrc(10)),
|
||||
Delay: 500 * time.Millisecond,
|
||||
TimeLimit: 10 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
return "localhost"
|
||||
log.Fatal(err)
|
||||
}
|
||||
bestIP := "localhost"
|
||||
for _, address := range addrs {
|
||||
// check the address type and if it is not a loopback the display it
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil && (strings.Contains(ipnet.IP.String(), "192.168.1") || strings.Contains(ipnet.IP.String(), "192.168")) {
|
||||
return ipnet.IP.String()
|
||||
|
||||
discoveries, err := p.Discover()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
if len(discoveries) > 0 {
|
||||
fmt.Printf("Found %d other computers\n", len(discoveries))
|
||||
for i, d := range discoveries {
|
||||
fmt.Printf("%d) '%s' with payload '%s'\n", i, d.Address, d.Payload)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Found no devices. You need to run this on another computer at the same time.")
|
||||
}
|
||||
}
|
||||
return bestIP
|
||||
}
|
||||
|
||||
// GetLocalIPs returns the local ip address
|
||||
func GetLocalIPs() (ips map[string]struct{}) {
|
||||
ips = make(map[string]struct{})
|
||||
ips["localhost"] = struct{}{}
|
||||
ips["127.0.0.1"] = struct{}{}
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, address := range addrs {
|
||||
ips[strings.Split(address.String(), "/")[0]] = struct{}{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// src is seeds the random generator for generating random strings
|
||||
|
@ -51,7 +59,7 @@ const (
|
|||
)
|
||||
|
||||
// RandStringBytesMaskImprSrc prints a random string
|
||||
func RandStringBytesMaskImprSrc(n int) string {
|
||||
func randStringBytesMaskImprSrc(n int) string {
|
||||
b := make([]byte, n)
|
||||
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
||||
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
|
@ -10,29 +10,50 @@ import (
|
|||
"golang.org/x/net/ipv4"
|
||||
)
|
||||
|
||||
// Discovered is the structure of the discovered peers,
|
||||
// which holds their local address (port removed) and
|
||||
// a payload if there is one.
|
||||
type Discovered struct {
|
||||
// Address is the local address of a discovered peer.
|
||||
Address string
|
||||
// Payload is the associated payload from discovered peer.
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
// Settings are the settings that can be specified for
|
||||
// doing peer discovery.
|
||||
type Settings struct {
|
||||
Limit int
|
||||
Port string
|
||||
// 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.
|
||||
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).
|
||||
MulticastAddress string
|
||||
// Payload is the bytes that are sent out with each broadcast. Must be short.
|
||||
Payload []byte
|
||||
// Delay is the amount of time between broadcasts. The default delay is 1 second.
|
||||
Delay time.Duration
|
||||
// TimeLimit is the amount of time to spend discovering, if the limit is not reached.
|
||||
// The default time limit is 10 seconds.
|
||||
TimeLimit time.Duration
|
||||
|
||||
portNum int
|
||||
MulticastAddress string
|
||||
multicastAddressNumbers []uint8
|
||||
Payload []byte
|
||||
Delay time.Duration
|
||||
TimeLimit time.Duration
|
||||
}
|
||||
|
||||
// PeerDiscovery is the object that can do the discovery for finding LAN peers.
|
||||
type PeerDiscovery struct {
|
||||
settings Settings
|
||||
localIP string
|
||||
received map[string][]byte
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// New 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)
|
||||
p.Lock()
|
||||
|
@ -56,7 +77,6 @@ func New(settings ...Settings) (p *PeerDiscovery, err error) {
|
|||
if p.settings.TimeLimit == time.Duration(0) {
|
||||
p.settings.TimeLimit = 10 * time.Second
|
||||
}
|
||||
p.localIP = GetLocalIP()
|
||||
p.received = make(map[string][]byte)
|
||||
p.settings.multicastAddressNumbers = []uint8{0, 0, 0, 0}
|
||||
for i, num := range strings.Split(p.settings.MulticastAddress, ".") {
|
||||
|
@ -74,6 +94,9 @@ func New(settings ...Settings) (p *PeerDiscovery, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// 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) {
|
||||
p.RLock()
|
||||
address := p.settings.MulticastAddress + ":" + p.settings.Port
|
||||
|
@ -176,7 +199,7 @@ func (p *PeerDiscovery) listen() (recievedBytes []byte, err error) {
|
|||
portNum := p.settings.portNum
|
||||
multicastAddressNumbers := p.settings.multicastAddressNumbers
|
||||
p.RUnlock()
|
||||
localIPs := GetLocalIPs()
|
||||
localIPs := getLocalIPs()
|
||||
|
||||
// get interfaces
|
||||
ifaces, err := net.Interfaces()
|
||||
|
@ -230,3 +253,18 @@ func (p *PeerDiscovery) listen() (recievedBytes []byte, err error) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
// getLocalIPs returns the local ip address
|
||||
func getLocalIPs() (ips map[string]struct{}) {
|
||||
ips = make(map[string]struct{})
|
||||
ips["localhost"] = struct{}{}
|
||||
ips["127.0.0.1"] = struct{}{}
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, address := range addrs {
|
||||
ips[strings.Split(address.String(), "/")[0]] = struct{}{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package peerdiscovery
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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{
|
||||
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