Skip to content

Commit

Permalink
update readme, refactor code for more flexibility, allow configuratio…
Browse files Browse the repository at this point in the history
…n of the signaling channel
  • Loading branch information
calebdoxsey committed Mar 14, 2019
1 parent 3edac96 commit 16961e7
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 61 deletions.
26 changes: 26 additions & 0 deletions README.md
Expand Up @@ -55,3 +55,29 @@ rtctunnel run
Typically it would be run in the background.

A docker-compose example is available in [examples/redis](https://github.com/rtctunnel/rtctunnel/tree/master/examples/redis).

## Configuration

Configuration is stored in a yaml file based on [github.com/kirsle/configdir](https://github.com/kirsle/configdir):

```text
Linux: $XDG_CONFIG_HOME or "$HOME/.config"
Windows: %APPDATA% or "C:\\Users\\%USER%\\AppData\\Roaming"
macOS: $HOME/Library/Application Support
```

You can see the path by running `rtctunnel help`.

### Signal Channel

In addition to the key pair and routes you can set the signal channel in the config:

```yaml
keypair:
public: xxx
private: xxx
routes: []
signalchannel: operator://localhost:8000
```

The default signal channel is `operator://operator.rtctunnel.com`, a custom signaling server whose source code can be found at [github.com/rtctunnel/operator](https://github.com/rtctunnel/operator). Currently no other signaling schemes are defined.
55 changes: 55 additions & 0 deletions channels/channels.go
@@ -0,0 +1,55 @@
package channels

import (
"fmt"
"net/url"
"sync"
)

// A Channel facilitates signaling.
type Channel interface {
Send(key, data string) error
Recv(key string) (data string, err error)
}

// A Factory returns a Channel from an address
type Factory = func(addr string) (Channel, error)

var channelFactories = struct {
sync.Mutex
m map[string]Factory
}{
m: make(map[string]Factory),
}

// RegisterFactory registers a new Factory
func RegisterFactory(scheme string, factory Factory) {
channelFactories.Lock()
channelFactories.m[scheme] = factory
channelFactories.Unlock()
}

// Get returns a channel for the given address
func Get(addr string) (Channel, error) {
u, err := url.Parse(addr)
if err != nil {
return nil, err
}

channelFactories.Lock()
factory, ok := channelFactories.m[u.Scheme]
channelFactories.Unlock()
if !ok {
return nil, fmt.Errorf("no channel factory registered for %s", u.Scheme)
}

return factory(addr)
}

// Must panics if there's an error
func Must(ch Channel, err error) Channel {
if err != nil {
panic(err)
}
return ch
}
29 changes: 21 additions & 8 deletions signal/operator.go → channels/operator/operator.go
@@ -1,33 +1,46 @@
package signal
package operator

import (
"errors"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"

"github.com/apex/log"
"github.com/rtctunnel/rtctunnel/channels"
)

func init() {
channels.RegisterFactory("operator", func(addr string) (channels.Channel, error) {
return New(strings.Replace(addr, "operator://", "https://", 1)), nil
})
}

// DefaultClient is the client to use for making http requests
var DefaultClient = &http.Client{
Timeout: 30 * time.Second,
}

// An OperatorChannel signals over a custom http server.
type OperatorChannel struct {
// An operatorChannel signals over a custom http server.
type operatorChannel struct {
url string
}

// NewOperatorChannel creates a new OperatorChannel.
func NewOperatorChannel(url string) *OperatorChannel {
return &OperatorChannel{url: url}
// New creates a new operatorChannel.
func New(url string) channels.Channel {
_, err := DefaultClient.Head(url)
if err != nil && strings.Contains(err.Error(), "server gave HTTP response") {
// switch to http
url = strings.Replace(url, "https://", "http://", 1)
}
return &operatorChannel{url: url}
}

// Recv receives a message at the given key.
func (c *OperatorChannel) Recv(key string) (data string, err error) {
func (c *operatorChannel) Recv(key string) (data string, err error) {
log.WithFields(log.Fields{
"url": c.url,
"key": key,
Expand Down Expand Up @@ -71,7 +84,7 @@ func (c *OperatorChannel) Recv(key string) (data string, err error) {
}

// Send sends a message to the given key with the given data.
func (c *OperatorChannel) Send(key, data string) error {
func (c *operatorChannel) Send(key, data string) error {
log.WithFields(log.Fields{
"url": c.url,
"key": key,
Expand Down
20 changes: 16 additions & 4 deletions cmd/rtctunnel/cmd_run.go
Expand Up @@ -6,9 +6,12 @@ import (
"net"
"time"

"github.com/apex/log"
"github.com/rtctunnel/rtctunnel/channels"
_ "github.com/rtctunnel/rtctunnel/channels/operator"
"github.com/rtctunnel/rtctunnel/crypt"
"github.com/rtctunnel/rtctunnel/peer"
"github.com/apex/log"
"github.com/rtctunnel/rtctunnel/signal"
"github.com/spf13/cobra"
)

Expand All @@ -27,11 +30,20 @@ func init() {
}

log.WithFields(log.Fields{
"config-file": options.configFile,
"public-key": cfg.KeyPair.Public,
"routes": cfg.Routes,
"config-file": options.configFile,
"public-key": cfg.KeyPair.Public,
"routes": cfg.Routes,
"signal-channel": cfg.SignalChannel,
}).Info("using config")

if cfg.SignalChannel != "" {
ch, err := channels.Get(cfg.SignalChannel)
if err != nil {
log.WithError(err).Fatal("invalid signalchannel in yaml config")
}
signal.SetDefaultOptions(signal.WithChannel(ch))
}

peerConns := map[crypt.Key]*peer.Conn{}
for _, route := range cfg.Routes {
var peerPublicKey crypt.Key
Expand Down
5 changes: 3 additions & 2 deletions cmd/rtctunnel/config.go
Expand Up @@ -18,8 +18,9 @@ type Route struct {

// A Config is the configuration for the RTCTunnel.
type Config struct {
KeyPair crypt.KeyPair
Routes []Route
KeyPair crypt.KeyPair
Routes []Route
SignalChannel string `yaml:"signalchannel,omitempty"`
}

// LoadConfig loads the config off of the disk.
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Expand Up @@ -3,24 +3,20 @@ module github.com/rtctunnel/rtctunnel
require (
github.com/apex/log v1.1.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.2.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/mr-tron/base58 v1.1.0
github.com/pions/dtls v1.2.2-0.20190309090603-e5ab2e462976 // indirect
github.com/pions/pkg v0.0.0-20190125070457-f0a98342fda8 // indirect
github.com/pions/transport v0.4.0 // indirect
github.com/pions/webrtc v1.2.1-0.20190311180755-c89940a0bd72
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.3.0
github.com/xtaci/smux v1.0.7
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20190311183353-d8887717615a // indirect
golang.org/x/sys v0.0.0-20190311152110-c8c8c57fd1e1 // indirect
gopkg.in/yaml.v2 v2.2.1
gotest.tools v2.2.0+incompatible // indirect
)
30 changes: 11 additions & 19 deletions go.sum
Expand Up @@ -7,14 +7,17 @@ github.com/cloudflare/sidh v0.0.0-20181111220428-fc8e6378752b/go.mod h1:o/DcCuWF
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand All @@ -23,20 +26,16 @@ github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDS
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pions/datachannel v1.2.0 h1:N12qhHSRVlgBcaal2Hi4skdz7VI4yz6bNC5IJDMzCNw=
github.com/pions/datachannel v1.2.0/go.mod h1:MKPEKJRwX/a9/tyQvcVTUI9szyf8ZuUyZxSA9AVMSro=
github.com/pions/dtls v1.0.2 h1:VOIp3whGooFWS4X0RBOXaykNORYr05yjs8mlNYqRc+4=
github.com/pions/dtls v1.0.2/go.mod h1:T22vu8VCOxNmIrbe3nnM1UdIo3m1Bx5CJNkHyehahLg=
github.com/pions/dtls v1.2.1 h1:QR7HLXROoi61iBUnHXDIJ1dtzFCiiXlHMe+lqgAH4W8=
github.com/pions/dtls v1.2.1/go.mod h1:OgJcO0SqrDdQzqkCTdAp4xCQlbCmwZtGyhbthbq9zIA=
github.com/pions/dtls v1.2.2-0.20190309090603-e5ab2e462976 h1:32lm2leANxavm+YPz0tLGO3r7gMZatJaRiMaBtB2/zs=
github.com/pions/dtls v1.2.2-0.20190309090603-e5ab2e462976/go.mod h1:OgJcO0SqrDdQzqkCTdAp4xCQlbCmwZtGyhbthbq9zIA=
github.com/pions/pkg v0.0.0-20181115215726-b60cd756f712 h1:ciXO7F7PusyAzW/EZJt01bETgfTxP/BIGoWQ15pBP54=
github.com/pions/pkg v0.0.0-20181115215726-b60cd756f712/go.mod h1:r9wKZs+Xxv2acLspex4CHQiIhFjGK1zGP+nUm/8klXA=
github.com/pions/pkg v0.0.0-20190125070457-f0a98342fda8 h1:OY9MWJ8yh5SuX7qSMx4F92cXnliCYOKQv4Qxyu92UvQ=
github.com/pions/pkg v0.0.0-20190125070457-f0a98342fda8/go.mod h1:r9wKZs+Xxv2acLspex4CHQiIhFjGK1zGP+nUm/8klXA=
github.com/pions/qtls-vendor-extracted v0.0.0-20190210024908-018998217c65 h1:skcEQZ2eUdm1WKlYu7y1y0HBzOwa1pgSAwvhG6PrI2s=
github.com/pions/qtls-vendor-extracted v0.0.0-20190210024908-018998217c65/go.mod h1:tSUehzG/8OAT3JvWvnovveLfRMM8NvgfN1LzwSrBX5s=
github.com/pions/quic v0.0.1 h1:SvloojnZl+wiaee/yKI88n/wQosFMCvatAKyxoRoiFQ=
Expand All @@ -61,8 +60,6 @@ github.com/pions/transport v0.1.0/go.mod h1:HLhzI7I0k8TyiQ99hfRZNRf84lG76eaFnZHn
github.com/pions/transport v0.2.0/go.mod h1:HLhzI7I0k8TyiQ99hfRZNRf84lG76eaFnZHnVy/wFnM=
github.com/pions/transport v0.4.0 h1:1N6fluzmj5W/16eFLDsCB18s/xEkjVTek0K4IJz75FU=
github.com/pions/transport v0.4.0/go.mod h1:9gvUd8ZeyU4ZX7dhNuUq97mPoekopkd7eCJEyhKwVO0=
github.com/pions/webrtc v1.2.0 h1:U/UYMmhlVF9c1S2cfYZhR+frL0daetdP6yQnWOQzhJo=
github.com/pions/webrtc v1.2.0/go.mod h1:bih1dMY7qksVxZTG2XMjIA6J7D5b92+MJzXYe+G2kng=
github.com/pions/webrtc v1.2.1-0.20190311180755-c89940a0bd72 h1:jtK3btaBpQOBQ+9Xx/iR82SZW05bIDp7bXSGLN1jERk=
github.com/pions/webrtc v1.2.1-0.20190311180755-c89940a0bd72/go.mod h1:HIOMC7FF/cEeWWYvNXt96kE7edclpbD0GwjW2LNgIWQ=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand All @@ -76,38 +73,33 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/xtaci/smux v1.0.7 h1:ragFTIwevybZKibSfltLxG2biJ4Y9eFQGhcBntoEhz4=
github.com/xtaci/smux v1.0.7/go.mod h1:f+nYm6SpuHMy/SH0zpbvAFHT1QoMcgLOsWcFip5KfPw=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b h1:Elez2XeF2p9uyVj0yEUDqQ56NFcDtcBNkYP7yv8YbUE=
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2 h1:NwxKRvbkH5MsNkvOtPZi3/3kmI8CAzs3mtv+GLQMkNo=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190311152110-c8c8c57fd1e1 h1:FQNj2xvjQ1lgFyzbSybGZr792Y8Dy95D7uuqnZAzNaA=
golang.org/x/sys v0.0.0-20190311152110-c8c8c57fd1e1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:y0IMTfclpMdsdIbr6uwmJn5/WZ7vFuObxDMdrylFM3A=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

0 comments on commit 16961e7

Please sign in to comment.