/
cmd_run.go
158 lines (135 loc) · 3.57 KB
/
cmd_run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"io"
"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/rtctunnel/rtctunnel/signal"
"github.com/spf13/cobra"
)
func init() {
runCmd := &cobra.Command{
Use: "run",
Short: "run the rtctunnel",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := LoadConfig(options.configFile)
if err != nil {
log.WithError(err).Fatal("failed to load config file")
}
if !cfg.KeyPair.Private.Valid() {
log.WithError(err).Fatal("invalid config file, missing private key")
}
log.WithFields(log.Fields{
"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
if route.LocalPeer == cfg.KeyPair.Public {
peerPublicKey = route.RemotePeer
} else if route.RemotePeer == cfg.KeyPair.Public {
peerPublicKey = route.LocalPeer
}
conn, ok := peerConns[peerPublicKey]
if !ok {
var err error
conn, err = peer.Open(cfg.KeyPair, peerPublicKey)
if err != nil {
log.WithError(err).Fatal("failed to open peer connection")
}
peerConns[peerPublicKey] = conn
go acceptRemote(cfg, conn)
}
if route.LocalPeer == cfg.KeyPair.Public {
go localListener(conn, route)
}
}
select {}
},
}
rootCmd.AddCommand(runCmd)
}
func acceptRemote(cfg *Config, pc *peer.Conn) {
for {
remote, port, err := pc.Accept()
if err != nil {
log.WithError(err).Fatal("failed to accept remote connection")
}
allowed := false
for _, r := range cfg.Routes {
if r.RemotePeer == cfg.KeyPair.Public && r.RemotePort == port {
allowed = true
break
}
}
if !allowed {
log.WithField("port", port).Warn("remote peer attempted to connect to disallowed port")
remote.Close()
continue
}
local, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(port)))
if err != nil {
log.WithError(err).Warn("failed to establish connection to local port")
remote.Close()
continue
}
go joinConns(local, remote)
}
}
func localListener(pc *peer.Conn, route Route) {
log.WithField("route", route).Info("starting local listener")
li, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(route.LocalPort)))
if err != nil {
log.WithError(err).Fatal("failed to create listener")
}
defer li.Close()
for {
local, err := li.Accept()
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(time.Second)
continue
}
log.WithError(err).Fatal("error accepting connection")
}
remote, err := pc.Open(route.RemotePort)
if err != nil {
local.Close()
log.WithError(err).Warn("failed to create data channel")
continue
}
go joinConns(local, remote)
}
}
func joinConns(c1, c2 net.Conn) {
defer c1.Close()
defer c2.Close()
errc := make(chan error, 2)
go func() {
_, err := io.Copy(c1, c2)
errc <- err
}()
go func() {
_, err := io.Copy(c2, c1)
errc <- err
}()
err := <-errc
if err != nil {
log.WithError(err).Warn("error copying data between connections")
}
}