Merge pull request #67 from schollz/local

automatically discover local
This commit is contained in:
Zack 2018-04-24 21:01:36 -07:00 committed by GitHub
commit 488b88f9dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 252 additions and 256 deletions

30
Gopkg.lock generated
View File

@ -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

View File

@ -46,7 +46,7 @@
name = "github.com/schollz/mnemonicode"
[[constraint]]
version = "0.1.0"
version = "1.1.0"
name = "github.com/schollz/peerdiscovery"
[[constraint]]

View File

@ -74,13 +74,6 @@ func NewConnection(config *AppConfig) (*Connection, error) {
c.rate = config.Rate
c.Local = config.Local
if c.Local {
c.DontEncrypt = true
if c.Server == "cowyo.com" {
c.Server = ""
}
}
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
config.File = "stdin"
@ -167,60 +160,7 @@ func (c *Connection) Run() error {
}()
defer c.cleanup()
if c.Local {
c.DontEncrypt = true
c.Yes = true
if c.Code == "" {
c.Code = strings.Split(GetRandomName(), "-")[0]
}
}
if c.Local && c.Server == "" {
c.Server = "localhost"
p, err := peerdiscovery.New(peerdiscovery.Settings{
Limit: 1,
TimeLimit: 600 * time.Second,
Delay: 500 * time.Millisecond,
Payload: []byte(c.Code),
})
if err != nil {
return err
}
if c.IsSender {
go p.Discover()
} else {
fmt.Print("Finding local croc relay...")
discovered, err := p.Discover()
if err != nil {
return err
}
if len(discovered) == 0 {
return errors.New("could not find server")
}
c.Server = discovered[0].Address
fmt.Println(discovered[0].Address)
c.Code = string(discovered[0].Payload)
}
}
if c.Local && c.IsSender {
log.Debug("starting relay")
relay := NewRelay(&AppConfig{
Debug: c.Debug,
})
go relay.Run()
}
log.Debug("checking code validity")
if len(c.Code) == 0 {
if c.IsSender {
c.Code = GetRandomName()
} else {
c.Code = getInput("Enter receive code: ")
}
log.Debug("changed code to ", c.Code)
}
// calculate number of threads
c.NumberOfConnections = MAX_NUMBER_THREADS
if c.IsSender {
fsize, err := FileSize(path.Join(c.File.Path, c.File.Name))
@ -233,7 +173,19 @@ func (c *Connection) Run() error {
}
}
runClientError := make(chan error)
if c.IsSender {
if c.Code == "" {
c.Code = GetRandomName()
}
log.Debug("starting relay in case local connections")
relay := NewRelay(&AppConfig{
Debug: c.Debug,
})
go relay.Run()
time.Sleep(200 * time.Millisecond)
if c.DontEncrypt {
// don't encrypt
CopyFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc")
@ -281,21 +233,48 @@ func (c *Connection) Run() error {
fmt.Fprintf(os.Stderr, "Sending %s file named '%s'\n", humanize.Bytes(uint64(c.File.Size)), c.File.Name)
}
if c.Local {
fmt.Fprintf(os.Stderr, "Receive with: croc --local\n")
fmt.Fprintf(os.Stderr, "or croc --local --server %s --code %s\n", GetLocalIP(), c.Code)
} else {
fmt.Fprintf(os.Stderr, "Code is: %s\n", c.Code)
}
fmt.Fprintf(os.Stderr, "Code is: %s\n", c.Code)
// broadcast local connection from sender
log.Debug("settings payload to ", c.Code)
go func() {
go peerdiscovery.Discover(peerdiscovery.Settings{
Limit: 1,
TimeLimit: 600 * time.Second,
Delay: 50 * time.Millisecond,
Payload: []byte(c.Code),
})
runClientError <- c.runClient("localhost")
}()
}
return c.runClient()
log.Debug("checking code validity")
if len(c.Code) == 0 && !c.IsSender {
log.Debug("Finding local croc relay...")
discovered, _ := peerdiscovery.Discover(peerdiscovery.Settings{
Limit: 1,
TimeLimit: 1 * time.Second,
Delay: 50 * time.Millisecond,
Payload: []byte(c.Code),
})
if len(discovered) > 0 {
c.Server = discovered[0].Address
log.Debug(discovered[0].Address)
c.Code = string(discovered[0].Payload)
log.Debugf("discovered code '%s'", c.Code)
time.Sleep(200 * time.Millisecond)
} else {
c.Code = getInput("Enter receive code: ")
log.Debug("changed code to ", c.Code)
}
}
go func() { runClientError <- c.runClient(c.Server) }()
return <-runClientError
}
// runClient spawns threads for parallel uplink/downlink via TCP
func (c *Connection) runClient() error {
func (c *Connection) runClient(serverName string) error {
c.HashedCode = Hash(c.Code)
c.NumberOfConnections = MAX_NUMBER_THREADS
var wg sync.WaitGroup
@ -323,12 +302,12 @@ func (c *Connection) runClient() error {
go func(id int) {
defer wg.Done()
port := strconv.Itoa(27001 + id)
connection, err := net.Dial("tcp", c.Server+":"+port)
connection, err := net.Dial("tcp", serverName+":"+port)
if err != nil {
if c.Server == "cowyo.com" {
if serverName == "cowyo.com" {
fmt.Println("\nCheck http://bit.ly/croc-relay to see if the public server is down or contact the webmaster: @yakczar")
} else {
fmt.Fprintf(os.Stderr, "\nCould not connect to relay %s\n", c.Server)
fmt.Fprintf(os.Stderr, "\nCould not connect to relay %s\n", serverName)
}
os.Exit(1)
}
@ -337,15 +316,15 @@ func (c *Connection) runClient() error {
message := receiveMessage(connection)
log.Debugf("relay says: %s", message)
if c.IsSender {
log.Debugf("telling relay: %s", "s."+c.Code)
log.Debugf("telling relay (%s): %s", c.Server, "s."+c.Code)
metaData, err := json.Marshal(c.File)
if err != nil {
log.Error(err)
}
encryptedMetaData, salt, iv := Encrypt(metaData, c.Code, c.DontEncrypt)
encryptedMetaData, salt, iv := Encrypt(metaData, c.Code)
sendMessage("s."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection)
} else {
log.Debugf("telling relay: %s", "r."+c.Code)
log.Debugf("telling relay (%s): %s", c.Server, "r."+c.Code)
if c.Wait {
// tell server to wait for sender
sendMessage("r."+c.HashedCode+".0.0.0", connection)
@ -409,6 +388,8 @@ func (c *Connection) runClient() error {
responses.Unlock()
time.Sleep(1 * time.Second)
return
} else if strings.Split(sendersAddress, ":")[0] == "127.0.0.1" {
sendersAddress = strings.Replace(sendersAddress, "127.0.0.1", c.Server, 1)
}
// have the main thread ask for the okay
if id == 0 {

View File

@ -1,113 +1,155 @@
<p align="center">
<img
src="https://user-images.githubusercontent.com/6550035/31846899-2b8a7034-b5cf-11e7-9643-afe552226c59.png"
width="100%" border="0" alt="croc">
<br>
<a href="https://github.com/schollz/croc/releases/latest"><img src="https://img.shields.io/badge/version-0.1.0-green.svg?style=flat-square" alt="Version"></a>
<a href="https://gitter.im/schollz/croc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge"><img src="https://img.shields.io/badge/chat-on%20gitter-green.svg?style=flat-square" alt="Version"></a>
</p>
<p align="center">Secure transfer of stuff from one side of the internet to the other.</p>
This is more or less (but mostly *less*) a Golang port of [@warner's](https://github.com/warner) [*magic-wormhole*](https://github.com/warner/magic-wormhole) which allows you to directly transfer files and folders between computers. I decided to make this because I wanted to send my friend Jessie a file using *magic-wormhole* and when I told Jessie how to install the dependencies she made this face: :sob:. So, nominally, *croc* does the same thing (encrypted file transfer directly between computers) without dependencies so you can just double-click on your computer, even if you use Windows.
**Don't we have enough open-source peer-to-peer file-transfer utilities?**
[There](https://github.com/cowbell/sharedrop) [are](https://github.com/webtorrent/instant.io) [great](https://github.com/kern/filepizza) [tools](https://github.com/warner/magic-wormhole) [that](https://github.com/zerotier/toss) [already](https://github.com/ipfs/go-ipfs) [do](https://github.com/zerotier/toss) [this](https://github.com/nils-werner/zget). But, no we don't, because after review, [I found it was useful to make a new one](https://schollz.github.io/sending-a-file/).
# Example
_These two gifs should run in sync if you force-reload (Ctl+F5)_
**Sender:**
![send](https://user-images.githubusercontent.com/6550035/31864532-ad80c6ae-b71b-11e7-91f9-bcba8143d3cf.gif)
**Receiver:**
![receive](https://user-images.githubusercontent.com/6550035/31864531-ad6e22c4-b71b-11e7-901a-02a210057cf1.gif)
**Sender:**
```
$ croc -send croc.exe
Sending 4.4 MB file named 'croc.exe'
Code is: 4-cement-galaxy-alpha
Sending (->24.65.41.43:50843)..
0s [==========================================================] 100%
File sent.
```
**Receiver:**
```
$ croc
Enter receive code: 4-cement-galaxy-alpha
Receiving file (4.4 MB) into: croc.exe
ok? (y/n): y
Receiving (<-50.32.38.188:50843)..
0s [==========================================================] 100%
Received file written to croc.exe
```
Note, by default, you don't need any arguments for receiving! This makes it possible for you to just double click the executable to run (nice for those of us that aren't computer wizards).
# Install
[Download the latest release for your system](https://github.com/schollz/croc/releases/latest).
Or, you can [install Go](https://golang.org/dl/) and build from source with `go get github.com/schollz/croc`.
# How does it work?
*croc* is similar to [magic-wormhole](https://github.com/warner/magic-wormhole#design) in spirit and design. Like *magic-wormhole*, *croc* generates a code phrase for you to share with your friend which allows secure end-to-end transfering of files and folders through a intermediary relay that connects the TCP ports between the two computers.
In *croc*, code phrase is 16 random bits that are [menemonic encoded](http://web.archive.org/web/20101031205747/http://www.tothink.com/mnemonic/) plus a prepended integer to specify number of threads. This code phrase is hashed using sha256 and sent to a relay which maps that key to that connection. When the relay finds a matching key for both the receiver and the sender (i.e. they both have the same code phrase), then the sender transmits the encrypted metadata to the receiver through the relay. Then the receiver decrypts and reviews the metadata (file name, size), and chooses whether to consent to the transfer.
After the receiver consents to the transfer, the sender transmits encrypted data through the relay. The relay setups up [Go channels](https://golang.org/doc/effective_go.html?h=chan#channels) for each connection which pipes all the data incoming from that sender's connection out to the receiver's connection. After the transmission the channels are destroyed and all the connection and meta data information is wiped from the relay server. The encrypted file data never is stored on the relay.
**Encryption**
Encryption uses PBKDF2 (see [RFC2898](http://www.ietf.org/rfc/rfc2898.txt)) where the code phrase shared between the sender and receiver is used as the passphrase. For each of the two encrypted data blocks (metadata stored on relay server, and file data transmitted), a random 8-byte salt is used and a IV is generated according to [NIST Recommendation for Block ciphers, Section 8.2](http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf).
**Decryption**
On the receiver's computer, each piece of received encrypted data is written to a separate file. These files are concatenated and then decrypted. The hash of the decrypted file is then checked against the hash transmitted from the sender (part of the meta data block).
## Run your own relay
*croc* relies on a TCP relay to staple the parallel incoming and outgoing connections. The relay temporarily stores connection information and the encrypted meta information. The default uses a public relay at, `cowyo.com`, which has no guarantees except that I guarantee to turn if off as soon as it gets abused ([click here to check the current status of the public relay](https://stats.uptimerobot.com/lOwJYIgRm)).
I recommend you run your own relay, it is very easy. On your server, `your-server.com`, just run
```
$ croc -relay
```
Now, when you use *croc* to send and receive you should add `-server your-server.com` to use your relay server.
_Note:_ If you are behind a firewall, make sure to open up TCP ports 27001-27009.
# Contribute
I am awed by all the [great contributions](#acknowledgements) made! If you feel like contributing, in any way, by all means you can send an Issue, a PR, ask a question, or tweet me ([@yakczar](http://ctt.ec/Rq054)).
# License
MIT
# Acknowledgements
Thanks...
- ...[@warner](https://github.com/warner) for the [idea](https://github.com/warner/magic-wormhole).
- ...[@tscholl2](https://github.com/tscholl2) for the [encryption gists](https://gist.github.com/tscholl2/dc7dc15dc132ea70a98e8542fefffa28).
- ...[@skorokithakis](https://github.com/skorokithakis) for [code on proxying two connections](https://www.stavros.io/posts/proxying-two-connections-go/).
- ...for making pull requests [@Girbons](https://github.com/Girbons), [@techtide](https://github.com/techtide), [@heymatthew](https://github.com/heymatthew), [@Lunsford94](https://github.com/Lunsford94), [@lummie](https://github.com/lummie), [@jesuiscamille](https://github.com/jesuiscamille), [@threefjord](https://github.com/threefjord), [@marcossegovia](https://github.com/marcossegovia), [@csleong98](https://github.com/csleong98), [@afotescu](https://github.com/afotescu)!
<p align="center">
<img
src="https://user-images.githubusercontent.com/6550035/31846899-2b8a7034-b5cf-11e7-9643-afe552226c59.png"
width="100%" border="0" alt="croc">
<br>
<a href="https://travis-ci.org/schollz/croc"><img src="https://travis-ci.org/schollz/croc.svg?branch=master" alt="Build Status"></a>
<a href="https://github.com/schollz/croc/releases/latest"><img src="https://img.shields.io/badge/version-1.0.0-brightgreen.svg?style=flat-square" alt="Version"></a>
<a href="https://goreportcard.com/report/github.com/schollz/croc"><img src="https://goreportcard.com/badge/github.com/schollz/croc" alt="Go Report Card"></a>
</p>
<p align="center">Easily and securely transfer stuff from one computer to another.</p>
*croc* allows any two computers to directly and securely transfer files and folders. When sending a file, *croc* generates a random code phrase which must be shared with the recipient so they can receive the file. The code phrase encrypts all data and metadata and also serves to authorize the connection between the two computers in a intermediary relay. The relay connects the TCP ports between the two computers and does not store any information (and all information passing through it is encrypted).
I hear you asking, *Why another open-source peer-to-peer file transfer utilities?* [There](https://github.com/cowbell/sharedrop) [are](https://github.com/webtorrent/instant.io) [great](https://github.com/kern/filepizza) [tools](https://github.com/warner/magic-wormhole) [that](https://github.com/zerotier/toss) [already](https://github.com/ipfs/go-ipfs) [do](https://github.com/zerotier/toss) [this](https://github.com/nils-werner/zget). But, after review, [I found it was useful to make another](https://schollz.github.io/sending-a-file/). Namely, *croc* has no dependencies (just [download a binary and run](https://github.com/schollz/croc/releases/latest)), it works on any operating system, and its blazingly fast because it does parallel transfer over multiple TCP ports.
# Example
_These two gifs should run in sync if you force-reload (Ctl+F5)_
**Sender:**
![send](https://raw.githubusercontent.com/schollz/croc/master/logo/sender.gif)
**Receiver:**
![receive](https://raw.githubusercontent.com/schollz/croc/master/logo/receiver.gif)
**Sender:**
```
$ croc -send some-file-or-folder
Sending 4.4 MB file named 'some-file-or-folder'
Code is: cement-galaxy-alpha
Sending (->[1]63982)..
89% |███████████████████████████████████ | [12s:1s]
File sent (2.6 MB/s)
```
**Receiver:**
```
$ croc
Enter receive code: cement-galaxy-alpha
Receiving file (4.4 MB) into: some-file-or-folder
ok? (y/n): y
Receiving (<-[1]63975)..
97% |██████████████████████████████████████ | [13s:0s]
Received file written to some-file-or-folder (2.6 MB/s)
```
Note, by default, you don't need any arguments for receiving! This makes it possible for you to just double click the executable to run (nice for those of us that aren't computer wizards).
## Transfering files between local computers
Its even easier if you want to transfer files between two computers on the same network.
**Sender:**
```
$ croc -send some-file-or-folder -local
```
**Receiver:**
```
$ croc -local
```
Yes, when you run locally you don't even need to use a code. When you run locally, the *croc* receiver will use UDP broadcast packets to automatically find the correct IP address and code to use to transfer the file. When run locally, there is also no encryption so it is even faster.
**Sender:**
![Running locally](https://raw.githubusercontent.com/schollz/croc/master/logo/1.gif)
**Receiver:**
![Running locally](https://raw.githubusercontent.com/schollz/croc/master/logo/2.gif)
## Using *croc* in pipes
You can easily use *croc* in pipes when you need to send data through stdin or get data from stdout.
**Sender:**
```
$ cat some_file_or_folder | croc
```
In this case *croc* will automatically use the stdin data and send and assign a filename like "croc-stdin-123456789".
**Receiver:**
```
$ croc --code code-phrase --yes --stdout | more
```
Here the reciever specified the code (`--code`) so it will not be prompted, and also specified `--yes` so the file will be automatically accepted. The output goes to stdout when flagged with `--stdout`.
# Install
[Download the latest release for your system](https://github.com/schollz/croc/releases/latest).
Or, you can [install Go](https://golang.org/dl/) and build from source with `go get github.com/schollz/croc`.
# How does it work?
*croc* is similar to [magic-wormhole](https://github.com/warner/magic-wormhole#design) in spirit and design. Like *magic-wormhole*, *croc* generates a code phrase for you to share with your friend which allows secure end-to-end transfering of files and folders through a intermediary relay that connects the TCP ports between the two computers.
In *croc*, code phrase is 16 random bits that are [menemonic encoded](http://web.archive.org/web/20101031205747/http://www.tothink.com/mnemonic/). This code phrase is hashed using sha256 and sent to a relay which maps that key to that connection. When the relay finds a matching key for both the receiver and the sender (i.e. they both have the same code phrase), then the sender transmits the encrypted metadata to the receiver through the relay. Then the receiver decrypts and reviews the metadata (file name, size), and chooses whether to consent to the transfer.
After the receiver consents to the transfer, the sender transmits encrypted data through the relay. The relay setups up [Go channels](https://golang.org/doc/effective_go.html?h=chan#channels) for each connection which pipes all the data incoming from that sender's connection out to the receiver's connection. After the transmission the channels are destroyed and all the connection and meta data information is wiped from the relay server. The encrypted file data never is stored on the relay.
**Encryption**
Encryption uses pbkdf2 (see [RFC2898](http://www.ietf.org/rfc/rfc2898.txt)) where the code phrase shared between the sender and receiver is used as the passphrase. For each of the two encrypted data blocks (metadata stored on relay server, and file data transmitted), a random 8-byte salt is used and a IV is generated according to [NIST Recommendation for Block ciphers, Section 8.2](http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf).
**Decryption**
On the receiver's computer, each piece of received encrypted data is written to a separate file. These files are concatenated and then decrypted. The hash of the decrypted file is then checked against the hash transmitted from the sender (part of the meta data block).
## Run your own relay
*croc* relies on a TCP relay to staple the parallel incoming and outgoing connections. The relay temporarily stores connection information and the encrypted meta information. The default uses a public relay at, `cowyo.com`, which has a 30-day uptime of 99.989% ([click here to check the current status of the public relay](https://stats.uptimerobot.com/lOwJYIgRm)).
You can also run your own relay, it is very easy. On your server, `your-server.com`, just run
```
$ croc -relay
```
Now, when you use *croc* to send and receive you should add `-server your-server.com` to use your relay server. Make sure to open up TCP ports 27001-27009.
# Contribute
I am awed by all the [great contributions](#acknowledgements) made! If you feel like contributing, in any way, by all means you can send an Issue, a PR, ask a question, or tweet me ([@yakczar](http://ctt.ec/Rq054)).
# License
MIT
# Acknowledgements
Thanks...
- ...[@warner](https://github.com/warner) for the [idea](https://github.com/warner/magic-wormhole).
- ...[@tscholl2](https://github.com/tscholl2) for the [encryption gists](https://gist.github.com/tscholl2/dc7dc15dc132ea70a98e8542fefffa28).
- ...[@skorokithakis](https://github.com/skorokithakis) for [code on proxying two connections](https://www.stavros.io/posts/proxying-two-connections-go/).
- ...for making pull requests [@Girbons](https://github.com/Girbons), [@techtide](https://github.com/techtide), [@heymatthew](https://github.com/heymatthew), [@Lunsford94](https://github.com/Lunsford94), [@lummie](https://github.com/lummie), [@jesuiscamille](https://github.com/jesuiscamille), [@threefjord](https://github.com/threefjord), [@marcossegovia](https://github.com/marcossegovia), [@csleong98](https://github.com/csleong98), [@afotescu](https://github.com/afotescu), [@callmefever](https://github.com/callmefever), [@El-JojA](https://github.com/El-JojA), [@anatolyyyyyy](https://github.com/anatolyyyyyy), [@goggle](https://github.com/goggle), [@smileboywtu](https://github.com/smileboywtu)!

View File

@ -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)
}

View File

@ -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 {

View File

@ -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 = 8192
// 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

View File

@ -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))
}