aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Bayles <jophba@chromium.org>2021-04-13 12:52:11 -0700
committerJordan Bayles <jophba@chromium.org>2021-04-15 20:21:37 +0000
commitd924b4eb5fe647490d61561e2e69608895c21e7c (patch)
tree484d14cd18c0740e023eb6e7af7964511db198f0
parentc49eea6ccaf616d171f77275eaeb53f277f8c571 (diff)
downloadopenscreen-d924b4eb5fe647490d61561e2e69608895c21e7c.tar.gz
[OSP] Update go implementation to use IP addresses
Update Go code to use IP addresses instead of hostnames from mDNS, and run `go fmt`. This patch is originally from pthatcher, and includes additional updates to be appropriate for merging to master now. NOTE: this patch makes OSP go work fully on Linux, however there are remaining issues to get it working successfully on Mac OSX. For history, see: https://chromium-review.googlesource.com/c/openscreen/+/1483998 Change-Id: I368728b414e8f2437dc4427a37eb95d710463b20 Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/2823916 Reviewed-by: Brandon Tolsch <btolsch@chromium.org>
-rw-r--r--osp/go/README.md (renamed from osp/go/README)7
-rw-r--r--osp/go/client.go2
-rw-r--r--osp/go/cmd/osp.go46
-rw-r--r--osp/go/cmd/test.go4
-rw-r--r--osp/go/controller.go2
-rw-r--r--osp/go/go.mod2
-rw-r--r--osp/go/go.sum2
-rw-r--r--osp/go/mdns.go23
-rw-r--r--osp/go/messages.go2
-rw-r--r--osp/go/quic.go4
-rw-r--r--osp/go/receiver.go2
-rw-r--r--osp/go/server.go4
12 files changed, 68 insertions, 32 deletions
diff --git a/osp/go/README b/osp/go/README.md
index f94ba080..96864ef8 100644
--- a/osp/go/README
+++ b/osp/go/README.md
@@ -1,7 +1,10 @@
-Run command line app:
+To run the command line app:
+
+```bash
$ go run cmd/osp.go server TV
$ go run cmd/osp.go browse
$ go run cmd/osp.go fling TV http://youtube.com
-(may require apt-get install libwebkit2gtk-4.0 on linux)
+```
+(may require `apt-get install libwebkit2gtk-4.0` on linux)
diff --git a/osp/go/client.go b/osp/go/client.go
index 61b52148..ead684b0 100644
--- a/osp/go/client.go
+++ b/osp/go/client.go
@@ -4,7 +4,7 @@
package osp
-// TODO(pthatcher):
+// TODO(jophba):
// - Read messages as well, and more than one
import (
diff --git a/osp/go/cmd/osp.go b/osp/go/cmd/osp.go
index ad8d7f0d..f9c27bb9 100644
--- a/osp/go/cmd/osp.go
+++ b/osp/go/cmd/osp.go
@@ -4,7 +4,8 @@
package main
-// TODO(pthatcher): Add response messages from receiver
+// TODO(jophba):
+// Add response messages from receiver
// Inject JS into viewURL to using .Eval and .Bind to send and receiver presentation connection messages
@@ -15,8 +16,9 @@ import (
"fmt"
"log"
- "osp"
+ "osp"
+ mdns "github.com/grandcat/zeroconf"
"github.com/zserge/webview"
)
@@ -30,7 +32,7 @@ func runServer(ctx context.Context, mdnsInstanceName string, port int) {
func browseMdns(ctx context.Context) {
entries, err := osp.BrowseMdns(ctx)
- if (err != nil) {
+ if err != nil {
log.Fatalf("Failed to browse mDNS: %v\n", err)
}
for entry := range entries {
@@ -38,21 +40,35 @@ func browseMdns(ctx context.Context) {
}
}
+func getMdnsHost(entry *mdns.ServiceEntry) string {
+ for _, ipv6 := range entry.AddrIPv6 {
+ log.Printf("Choosing IPv6 address [%s]\n", ipv6)
+ return fmt.Sprintf("[%s]", ipv6)
+ }
+ for _, ipv4 := range entry.AddrIPv4 {
+ log.Printf("Choosing IPv4 address %s\n", ipv4)
+ return fmt.Sprintf("%s", ipv4)
+ }
+
+ // This shouldn't happen
+ log.Printf("No IP address found. Falling back to hostname %s\n", entry.HostName)
+ return entry.HostName
+}
+
func flingUrl(ctx context.Context, target string, url string) {
log.Printf("Search for %s\n", target)
- entries, err := osp.BrowseMdns(ctx)
- if (err != nil) {
+ entries, err := osp.LookupMdns(ctx, target)
+ if err != nil {
log.Fatalf("Failed to browse mDNS: %v\n", err)
}
for entry := range entries {
- if entry.Instance == target {
- log.Printf("Fling %s to %s:%d\n", url, entry.HostName, entry.Port)
- err := osp.StartPresentation(ctx, entry.HostName, entry.Port, url);
- if err != nil {
- log.Fatalln("Failed to start presentation.");
- }
- break
+ log.Printf("Fling %s to %s:%d\n", url, entry.HostName, entry.Port)
+ host := getMdnsHost(entry)
+ err := osp.StartPresentation(ctx, host, entry.Port, url)
+ if err != nil {
+ log.Fatalln("Failed to start presentation.")
}
+ break
}
}
@@ -91,10 +107,10 @@ func main() {
log.Fatalln("Usage: osp server name")
}
mdnsInstanceName := args[1]
- runServer(ctx, mdnsInstanceName, *port)
+ runServer(ctx, mdnsInstanceName, *port)
case "browse":
- browseMdns(ctx)
+ browseMdns(ctx)
case "fling":
if len(args) < 3 {
@@ -103,7 +119,7 @@ func main() {
target := args[1]
url := args[2]
- flingUrl(ctx, target, url)
+ flingUrl(ctx, target, url)
case "view":
if len(args) < 2 {
diff --git a/osp/go/cmd/test.go b/osp/go/cmd/test.go
index ad6142b8..ac068f42 100644
--- a/osp/go/cmd/test.go
+++ b/osp/go/cmd/test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// TODO(pthatcher): Use proper testing framework
+// TODO(jophba): Use proper testing framework
package main
@@ -17,7 +17,7 @@ import (
)
func testMdns() {
- // TODO(pthatcher): log error if it fails
+ // TODO(jophba): log error if it fails
ctx := context.Background()
instance := "TV"
port := 10000
diff --git a/osp/go/controller.go b/osp/go/controller.go
index 005ff52d..6fa25358 100644
--- a/osp/go/controller.go
+++ b/osp/go/controller.go
@@ -4,7 +4,7 @@
package osp
-// TODO(pthatcher):
+// TODO(jophba):
// - Read and check the response message
// - Make a nice object API with methods that can do more than one thing per connection
// - Make it possible to have a presentation controller that is a server
diff --git a/osp/go/go.mod b/osp/go/go.mod
index 4928182e..a4c8fbfe 100644
--- a/osp/go/go.mod
+++ b/osp/go/go.mod
@@ -11,7 +11,7 @@ require (
github.com/lucas-clemente/quic-go-certificates v0.0.0-20160823095156-d2f86524cced // indirect
github.com/miekg/dns v1.1.2 // indirect
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2
- github.com/zserge/webview v0.0.0-20181018084947-f390a2df9ec5
+ github.com/zserge/webview v0.0.0-20200121135717-9c1b0a888aa4
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e // indirect
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect
diff --git a/osp/go/go.sum b/osp/go/go.sum
index 1a164b0a..4d12cc27 100644
--- a/osp/go/go.sum
+++ b/osp/go/go.sum
@@ -20,6 +20,8 @@ github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/zserge/webview v0.0.0-20181018084947-f390a2df9ec5 h1:1zYVGLwZR4gPRQdEiOBf9s63ZHGfCkQ/p99d1zHuZBQ=
github.com/zserge/webview v0.0.0-20181018084947-f390a2df9ec5/go.mod h1:a1CV8KR4Dd1eP2g+mEijGOp+HKczwdKHWyx0aPHKvo4=
+github.com/zserge/webview v0.0.0-20200121135717-9c1b0a888aa4 h1:UjGpx0KjJegeVC/TZEL/dSCTUXajewpIA1NTF8snadg=
+github.com/zserge/webview v0.0.0-20200121135717-9c1b0a888aa4/go.mod h1:a1CV8KR4Dd1eP2g+mEijGOp+HKczwdKHWyx0aPHKvo4=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
diff --git a/osp/go/mdns.go b/osp/go/mdns.go
index 591ef6e7..7bda7ec6 100644
--- a/osp/go/mdns.go
+++ b/osp/go/mdns.go
@@ -4,9 +4,9 @@
package osp
-// TODO(pthatcher):
+// TODO(jophba):
// - Make our own abstraction that has
-// .InstanceName, .HostName, .MetadataVersion, .FingerPrint
+// .InstanceName, .HostName, .MetadataVersion, .FingerPrint
// rather than using mdns.ServiceEntry
// - Advertise TXT (text below) with "fp" and "mv"
@@ -21,8 +21,9 @@ const (
MdnsDomain = "local"
)
-// Returns a channel of mDNS entries
-// The critical parts are entry.Target (name) entry.HostName (address)
+// Returns a channel of mDNS entries. The critical parts are
+// entry.Target (service name) entry.HostName, entry.AddrIPv4, and
+// entry.AddrIPv6.
func BrowseMdns(ctx context.Context) (<-chan *mdns.ServiceEntry, error) {
entries := make(chan *mdns.ServiceEntry)
@@ -35,6 +36,20 @@ func BrowseMdns(ctx context.Context) (<-chan *mdns.ServiceEntry, error) {
return entries, err
}
+// Returns a channel of mDNS entries. The critical parts are,
+// entry.HostName, entry.AddrIPv4, and entry.AddrIPv6.
+func LookupMdns(ctx context.Context, target string) (<-chan *mdns.ServiceEntry, error) {
+ entries := make(chan *mdns.ServiceEntry)
+
+ resolver, err := mdns.NewResolver(nil)
+ if err != nil {
+ return entries, err
+ }
+
+ err = resolver.Lookup(ctx, target, MdnsServiceType, MdnsDomain, entries)
+ return entries, err
+}
+
func RunMdnsServer(ctx context.Context, instance string, port int) error {
var text []string
server, err := mdns.Register(instance, MdnsServiceType, MdnsDomain, port, text, nil /* ifaces */)
diff --git a/osp/go/messages.go b/osp/go/messages.go
index ffc85633..174ef300 100644
--- a/osp/go/messages.go
+++ b/osp/go/messages.go
@@ -4,7 +4,7 @@
package osp
-// TODO(pthatcher):
+// TODO(jophba):
// - Read and write size prefixes
import (
diff --git a/osp/go/quic.go b/osp/go/quic.go
index 8fdff9aa..1ec2224c 100644
--- a/osp/go/quic.go
+++ b/osp/go/quic.go
@@ -4,7 +4,7 @@
package osp
-// TODO(pthatcher):
+// TODO(jophba):
// - avoid NetworkIdleTimeout
// - make a client object that can send and receive more than one stream
// - make a server object that can send and receive more than one stream
@@ -56,7 +56,7 @@ func readAllStreams(ctx context.Context, session quic.Session, streams chan<- io
// Returns a quic.Session object with a .OpenStreamSync method to send streams
func DialAsQuicClient(ctx context.Context, hostname string, port int) (quic.Session, error) {
- // TODO(pthatcher): Change InsecureSkipVerify
+ // TODO(jophba): Change InsecureSkipVerify
tlsConfig := &tls.Config{InsecureSkipVerify: true}
addr := fmt.Sprintf("%s:%d", hostname, port)
session, err := quic.DialAddrContext(ctx, addr, tlsConfig, nil)
diff --git a/osp/go/receiver.go b/osp/go/receiver.go
index de1f3727..ace8b606 100644
--- a/osp/go/receiver.go
+++ b/osp/go/receiver.go
@@ -4,7 +4,7 @@
package osp
-// TODO(pthatcher):
+// TODO(jophba):
// - Send a response message
// - Make a nice object API with methods
// - Make it possible to have a presentation receiver that is a client
diff --git a/osp/go/server.go b/osp/go/server.go
index e7770576..8f325fd4 100644
--- a/osp/go/server.go
+++ b/osp/go/server.go
@@ -4,7 +4,7 @@
package osp
-// TODO(pthatcher):
+// TODO(jophba):
// - Write messages as well
import (
@@ -14,7 +14,7 @@ import (
)
func ReadMessagesAsServer(ctx context.Context, instanceName string, port int, cert tls.Certificate, messages chan<- interface{}) error {
- // TODO(pthatcher): log error if it fails
+ // TODO(jophba): log error if it fails
go RunMdnsServer(ctx, instanceName, port)
streams := make(chan io.ReadWriteCloser)
go RunQuicServer(ctx, port, cert, streams)