Skip to content

Commit

Permalink
Remove usage of 'app.Addr' (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 5, 2019
1 parent aaf996b commit 0711d12
Show file tree
Hide file tree
Showing 24 changed files with 140 additions and 127 deletions.
3 changes: 2 additions & 1 deletion cmd/apps/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/skycoin/dmsg/cipher"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

func main() {
Expand Down Expand Up @@ -47,7 +48,7 @@ func main() {
log.Fatal("Failed to construct PubKey: ", err, os.Args[1])
}

conn, err := helloworldApp.Dial(&app.Addr{PubKey: remotePK, Port: 10})
conn, err := helloworldApp.Dial(&routing.Addr{PubKey: remotePK, Port: 10})
if err != nil {
log.Fatal("Failed to open remote conn: ", err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/apps/skychat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/skycoin/skywire/internal/netutil"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

var addr = flag.String("addr", ":8000", "address to bind")
Expand Down Expand Up @@ -64,7 +65,7 @@ func listenLoop() {
return
}

raddr := conn.RemoteAddr().(*app.Addr)
raddr := conn.RemoteAddr().(*routing.Addr)
connsMu.Lock()
chatConns[raddr.PubKey] = conn
connsMu.Unlock()
Expand All @@ -74,7 +75,7 @@ func listenLoop() {
}

func handleConn(conn net.Conn) {
raddr := conn.RemoteAddr().(*app.Addr)
raddr := conn.RemoteAddr().(*routing.Addr)
for {
buf := make([]byte, 32*1024)
n, err := conn.Read(buf)
Expand Down Expand Up @@ -106,7 +107,7 @@ func messageHandler(w http.ResponseWriter, req *http.Request) {
return
}

addr := &app.Addr{PubKey: pk, Port: 1}
addr := &routing.Addr{PubKey: pk, Port: 1}
connsMu.Lock()
conn, ok := chatConns[pk]
connsMu.Unlock()
Expand Down
3 changes: 2 additions & 1 deletion cmd/apps/therealproxy-client/therealproxy-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/skycoin/skywire/internal/netutil"
"github.com/skycoin/skywire/internal/therealproxy"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

const socksPort = 3
Expand Down Expand Up @@ -43,7 +44,7 @@ func main() {

var conn net.Conn
err = r.Do(func() error {
conn, err = socksApp.Dial(&app.Addr{PubKey: pk, Port: uint16(socksPort)})
conn, err = socksApp.Dial(&routing.Addr{PubKey: pk, Port: uint16(socksPort)})
return err
})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/therealssh/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/kr/pty"
"github.com/skycoin/dmsg/cipher"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

// Port reserved for SSH app
Expand All @@ -28,7 +28,7 @@ var Debug = false
// SSHChannel defines communication channel parameters.
type SSHChannel struct {
RemoteID uint32
RemoteAddr *app.Addr
RemoteAddr *routing.Addr

conn net.Conn
msgCh chan []byte
Expand All @@ -44,14 +44,14 @@ type SSHChannel struct {
}

// OpenChannel constructs new SSHChannel with empty Session.
func OpenChannel(remoteID uint32, remoteAddr *app.Addr, conn net.Conn) *SSHChannel {
func OpenChannel(remoteID uint32, remoteAddr *routing.Addr, conn net.Conn) *SSHChannel {
return &SSHChannel{RemoteID: remoteID, conn: conn, RemoteAddr: remoteAddr, msgCh: make(chan []byte),
dataCh: make(chan []byte), done: make(chan struct{})}
}

// OpenClientChannel constructs new client SSHChannel with empty Session.
func OpenClientChannel(remoteID uint32, remotePK cipher.PubKey, conn net.Conn) *SSHChannel {
ch := OpenChannel(remoteID, &app.Addr{PubKey: remotePK, Port: Port}, conn)
ch := OpenChannel(remoteID, &routing.Addr{PubKey: remotePK, Port: Port}, conn)
return ch
}

Expand Down
4 changes: 2 additions & 2 deletions internal/therealssh/channel_pty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

func TestChannelServe(t *testing.T) {
in, out := net.Pipe()
ch := OpenChannel(1, &app.Addr{PubKey: cipher.PubKey{}, Port: Port}, in)
ch := OpenChannel(1, &routing.Addr{PubKey: cipher.PubKey{}, Port: Port}, in)

errCh := make(chan error)
go func() {
Expand Down
10 changes: 5 additions & 5 deletions internal/therealssh/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

func TestChannelSendWrite(t *testing.T) {
pk, _ := cipher.GenerateKeyPair()
in, out := net.Pipe()
ch := OpenChannel(1, &app.Addr{PubKey: pk, Port: Port}, in)
ch := OpenChannel(1, &routing.Addr{PubKey: pk, Port: Port}, in)

errCh := make(chan error)
go func() {
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestChannelSendWrite(t *testing.T) {
}

func TestChannelRead(t *testing.T) {
ch := OpenChannel(1, &app.Addr{PubKey: cipher.PubKey{}, Port: Port}, nil)
ch := OpenChannel(1, &routing.Addr{PubKey: cipher.PubKey{}, Port: Port}, nil)

buf := make([]byte, 3)
go func() {
Expand All @@ -67,7 +67,7 @@ func TestChannelRead(t *testing.T) {

func TestChannelRequest(t *testing.T) {
in, out := net.Pipe()
ch := OpenChannel(1, &app.Addr{PubKey: cipher.PubKey{}, Port: Port}, in)
ch := OpenChannel(1, &routing.Addr{PubKey: cipher.PubKey{}, Port: Port}, in)

type data struct {
res []byte
Expand All @@ -93,7 +93,7 @@ func TestChannelRequest(t *testing.T) {

func TestChannelServeSocket(t *testing.T) {
in, out := net.Pipe()
ch := OpenChannel(1, &app.Addr{PubKey: cipher.PubKey{}, Port: Port}, in)
ch := OpenChannel(1, &routing.Addr{PubKey: cipher.PubKey{}, Port: Port}, in)

assert.Equal(t, filepath.Join(os.TempDir(), "therealsshd-1"), ch.SocketPath())

Expand Down
8 changes: 4 additions & 4 deletions internal/therealssh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
"github.com/skycoin/dmsg/cipher"

"github.com/skycoin/skywire/internal/netutil"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

var r = netutil.NewRetrier(50*time.Millisecond, 5, 2)

// Dialer dials to a remote node.
type Dialer interface {
Dial(raddr *app.Addr) (net.Conn, error)
Dial(raddr *routing.Addr) (net.Conn, error)
}

// Client proxies CLI's requests to a remote server. Control messages
Expand Down Expand Up @@ -54,7 +54,7 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSH
var err error

err = r.Do(func() error {
conn, err = c.dialer.Dial(&app.Addr{PubKey: remotePK, Port: Port})
conn, err = c.dialer.Dial(&routing.Addr{PubKey: remotePK, Port: Port})
return err
})
if err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *Client) serveConn(conn net.Conn) error {
return err
}

raddr := conn.RemoteAddr().(*app.Addr)
raddr := conn.RemoteAddr().(*routing.Addr)
payload := buf[:n]

if len(payload) < 5 {
Expand Down
14 changes: 7 additions & 7 deletions internal/therealssh/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

func TestClientOpenChannel(t *testing.T) {
Expand Down Expand Up @@ -50,26 +50,26 @@ func TestClientHandleResponse(t *testing.T) {
in, out := net.Pipe()
errCh := make(chan error)
go func() {
errCh <- c.serveConn(&mockConn{out, &app.Addr{PubKey: pk, Port: Port}})
errCh <- c.serveConn(&mockConn{out, &routing.Addr{PubKey: pk, Port: Port}})
}()

_, err := in.Write(appendU32([]byte{byte(CmdChannelResponse)}, 0))
require.NoError(t, err)
assert.Equal(t, "channel is not opened", (<-errCh).Error())

go func() {
errCh <- c.serveConn(&mockConn{out, &app.Addr{PubKey: cipher.PubKey{}, Port: Port}})
errCh <- c.serveConn(&mockConn{out, &routing.Addr{PubKey: cipher.PubKey{}, Port: Port}})
}()

ch := OpenChannel(4, &app.Addr{PubKey: pk, Port: Port}, nil)
ch := OpenChannel(4, &routing.Addr{PubKey: pk, Port: Port}, nil)
c.chans.add(ch)

_, err = in.Write(appendU32([]byte{byte(CmdChannelResponse)}, 0))
require.NoError(t, err)
assert.Equal(t, "unauthorized", (<-errCh).Error())

go func() {
errCh <- c.serveConn(&mockConn{out, &app.Addr{PubKey: pk, Port: Port}})
errCh <- c.serveConn(&mockConn{out, &routing.Addr{PubKey: pk, Port: Port}})
}()
dataCh := make(chan []byte)
go func() {
Expand All @@ -91,13 +91,13 @@ func newPipeDialer() (net.Conn, *pipeDialer) {
return out, &pipeDialer{in}
}

func (d *pipeDialer) Dial(raddr *app.Addr) (net.Conn, error) {
func (d *pipeDialer) Dial(raddr *routing.Addr) (net.Conn, error) {
return d.conn, nil
}

type mockConn struct {
net.Conn
addr *app.Addr
addr *routing.Addr
}

func (conn *mockConn) RemoteAddr() net.Addr {
Expand Down
6 changes: 3 additions & 3 deletions internal/therealssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/skycoin/dmsg/cipher"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

// CommandType represents global protocol messages.
Expand Down Expand Up @@ -66,7 +66,7 @@ func NewServer(auth Authorizer) *Server {
}

// OpenChannel opens new client channel.
func (s *Server) OpenChannel(remoteAddr *app.Addr, remoteID uint32, conn net.Conn) error {
func (s *Server) OpenChannel(remoteAddr *routing.Addr, remoteID uint32, conn net.Conn) error {
debug("opening new channel")
channel := OpenChannel(remoteID, remoteAddr, conn)
var res []byte
Expand Down Expand Up @@ -147,7 +147,7 @@ func (s *Server) Serve(conn net.Conn) error {
return err
}

raddr := conn.RemoteAddr().(*app.Addr)
raddr := conn.RemoteAddr().(*routing.Addr)
payload := buf[:n]

if len(payload) < 5 {
Expand Down
10 changes: 5 additions & 5 deletions internal/therealssh/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

func TestMain(m *testing.M) {
Expand All @@ -37,7 +37,7 @@ func TestServerOpenChannel(t *testing.T) {
in, out := net.Pipe()
errCh := make(chan error)
go func() {
errCh <- s.OpenChannel(&app.Addr{PubKey: cipher.PubKey{}, Port: Port}, 4, in)
errCh <- s.OpenChannel(&routing.Addr{PubKey: cipher.PubKey{}, Port: Port}, 4, in)
}()

buf := make([]byte, 18)
Expand All @@ -49,7 +49,7 @@ func TestServerOpenChannel(t *testing.T) {
assert.Equal(t, []byte("unauthorized"), buf[6:])

go func() {
errCh <- s.OpenChannel(&app.Addr{PubKey: pk, Port: Port}, 4, in)
errCh <- s.OpenChannel(&routing.Addr{PubKey: pk, Port: Port}, 4, in)
}()

buf = make([]byte, 10)
Expand All @@ -70,7 +70,7 @@ func TestServerHandleRequest(t *testing.T) {
assert.Equal(t, "channel is not opened", err.Error())

in, out := net.Pipe()
ch := OpenChannel(4, &app.Addr{PubKey: pk, Port: Port}, in)
ch := OpenChannel(4, &routing.Addr{PubKey: pk, Port: Port}, in)
s.chans.add(ch)

errCh := make(chan error)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestServerHandleData(t *testing.T) {
require.Error(t, err)
assert.Equal(t, "channel is not opened", err.Error())

ch := OpenChannel(4, &app.Addr{PubKey: pk, Port: Port}, nil)
ch := OpenChannel(4, &routing.Addr{PubKey: pk, Port: Port}, nil)
s.chans.add(ch)

err = s.HandleData(cipher.PubKey{}, 0, []byte("foo"))
Expand Down
20 changes: 11 additions & 9 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"os/exec"
"path/filepath"
"sync"

"github.com/skycoin/skywire/pkg/routing"
)

const (
Expand All @@ -36,7 +38,7 @@ type App struct {
config Config
proto *Protocol

acceptChan chan [2]*Addr
acceptChan chan [2]*routing.Addr
doneChan chan struct{}

conns map[LoopAddr]io.ReadWriteCloser
Expand Down Expand Up @@ -69,7 +71,7 @@ func SetupFromPipe(config *Config, inFD, outFD uintptr) (*App, error) {
app := &App{
config: *config,
proto: NewProtocol(pipeConn),
acceptChan: make(chan [2]*Addr),
acceptChan: make(chan [2]*routing.Addr),
doneChan: make(chan struct{}),
conns: make(map[LoopAddr]io.ReadWriteCloser),
}
Expand Down Expand Up @@ -128,8 +130,8 @@ func (app *App) Accept() (net.Conn, error) {
}

// Dial sends create loop request to a Node and returns net.Conn for created loop.
func (app *App) Dial(raddr *Addr) (net.Conn, error) {
laddr := &Addr{}
func (app *App) Dial(raddr *routing.Addr) (net.Conn, error) {
laddr := &routing.Addr{}
err := app.proto.Send(FrameCreateLoop, raddr, laddr)
if err != nil {
return nil, err
Expand All @@ -145,7 +147,7 @@ func (app *App) Dial(raddr *Addr) (net.Conn, error) {

// Addr returns empty Addr, implements net.Listener.
func (app *App) Addr() net.Addr {
return &Addr{}
return &routing.Addr{}
}

func (app *App) handleProto() {
Expand Down Expand Up @@ -226,7 +228,7 @@ func (app *App) closeConn(data []byte) error {
}

func (app *App) confirmLoop(data []byte) error {
addrs := [2]*Addr{}
addrs := [2]*routing.Addr{}
if err := json.Unmarshal(data, &addrs); err != nil {
return err
}
Expand All @@ -252,11 +254,11 @@ func (app *App) confirmLoop(data []byte) error {

type appConn struct {
net.Conn
laddr *Addr
raddr *Addr
laddr *routing.Addr
raddr *routing.Addr
}

func newAppConn(conn net.Conn, laddr, raddr *Addr) *appConn {
func newAppConn(conn net.Conn, laddr, raddr *routing.Addr) *appConn {
return &appConn{conn, laddr, raddr}
}

Expand Down
Loading

0 comments on commit 0711d12

Please sign in to comment.