Skip to content

Commit

Permalink
Merge branch 'milestone2' into feature/update-visor
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
  • Loading branch information
nkryuchkov committed Mar 2, 2020
2 parents 5416066 + 55446dc commit 480cc39
Show file tree
Hide file tree
Showing 112 changed files with 9,573 additions and 40,138 deletions.
4 changes: 4 additions & 0 deletions cmd/apps/skychat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func handleConn(conn net.Conn) {
n, err := conn.Read(buf)
if err != nil {
log.Println("Failed to read packet:", err)
raddr := conn.RemoteAddr().(appnet.Addr)
connsMu.Lock()
delete(chatConns, raddr.PubKey)
connsMu.Unlock()
return
}

Expand Down
60 changes: 40 additions & 20 deletions cmd/apps/skysocks-client/skysocks-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"flag"
"fmt"
"io"
"net"
"time"

Expand All @@ -29,6 +30,24 @@ const (

var r = netutil.NewRetrier(time.Second, 0, 1)

func dialServer(appCl *app.Client, pk cipher.PubKey) (net.Conn, error) {
var conn net.Conn
err := r.Do(func() error {
var err error
conn, err = appCl.Dial(appnet.Addr{
Net: netType,
PubKey: pk,
Port: socksPort,
})
return err
})
if err != nil {
return nil, err
}

return conn, nil
}

func main() {
log := app.NewLogger(appName)
skysocks.Log = log.PackageLogger("skysocks")
Expand Down Expand Up @@ -64,29 +83,30 @@ func main() {
log.Fatal("Invalid server PubKey: ", err)
}

var conn net.Conn
err = r.Do(func() error {
conn, err = socksApp.Dial(appnet.Addr{
Net: netType,
PubKey: pk,
Port: socksPort,
})
return err
})
if err != nil {
log.Fatal("Failed to dial to a server: ", err)
}
for {
conn, err := dialServer(socksApp, pk)
if err != nil {
log.Fatalf("Failed to dial to a server: %v", err)
}

log.Printf("Connected to %v\n", pk)
log.Printf("Connected to %v\n", pk)

client, err := skysocks.NewClient(conn)
if err != nil {
log.Fatal("Failed to create a new client: ", err)
}
client, err := skysocks.NewClient(conn)
if err != nil {
log.Fatal("Failed to create a new client: ", err)
}

log.Printf("Serving proxy client %v\n", *addr)

if err := client.ListenAndServe(*addr); err != nil {
log.Errorf("Error serving proxy client: %v\n", err)
}

log.Printf("Serving proxy client %v\n", *addr)
// need to filter this out, cause usually client failure means app conn is already closed
if err := conn.Close(); err != nil && err != io.ErrClosedPipe {
log.Errorf("Error closing app conn: %v\n", err)
}

if err := client.ListenAndServe(*addr); err != nil {
log.Fatalf("Error serving proxy client: %v\n", err)
log.Println("Reconnecting to skysocks server")
}
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/SkycoinProject/skywire-mainnet
go 1.13

require (
github.com/SkycoinProject/dmsg v0.0.0-20200220122410-79d9d7bac617
github.com/SkycoinProject/dmsg v0.0.0-20200227084433-7605a550f502
github.com/SkycoinProject/skycoin v0.27.0
github.com/SkycoinProject/yamux v0.0.0-20191213015001-a36efeefbf6a
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
Expand All @@ -12,9 +12,9 @@ require (
github.com/google/uuid v1.1.1
github.com/gorilla/handlers v1.4.2
github.com/gorilla/securecookie v1.1.1
github.com/hashicorp/yamux v0.0.0-20190923154419-df201c70410d
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mholt/archiver/v3 v3.3.0
github.com/pkg/profile v1.3.0
Expand All @@ -25,9 +25,9 @@ require (
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
go.etcd.io/bbolt v1.3.3
golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
golang.org/x/net v0.0.0-20191204025024-5ee1b9f4859a
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
golang.org/x/sys v0.0.0-20200301040627-c5d0d7b4ec88 // indirect
)

//replace github.com/SkycoinProject/dmsg => ../dmsg
30 changes: 30 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ github.com/SkycoinProject/dmsg v0.0.0-20200203035036-dbbed345b710 h1:92T9NENXATj
github.com/SkycoinProject/dmsg v0.0.0-20200203035036-dbbed345b710/go.mod h1:/nTdcMBrMHE39N6fxm300DtMly3UvZXPfwxBa9U8oGs=
github.com/SkycoinProject/dmsg v0.0.0-20200213062255-a362e46e2625 h1:wMVijitgUc9BljDl2eSj23nl29YpTcs5LQFIVxioFiM=
github.com/SkycoinProject/dmsg v0.0.0-20200213062255-a362e46e2625/go.mod h1:/nTdcMBrMHE39N6fxm300DtMly3UvZXPfwxBa9U8oGs=
github.com/SkycoinProject/dmsg v0.0.0-20200218101358-80f8406e37c4 h1:F+5F6fb5hntRCBUwV3REWodGaPb6uYZqHGGPMgCGk8Y=
github.com/SkycoinProject/dmsg v0.0.0-20200218101358-80f8406e37c4/go.mod h1:/nTdcMBrMHE39N6fxm300DtMly3UvZXPfwxBa9U8oGs=
github.com/SkycoinProject/dmsg v0.0.0-20200220122410-79d9d7bac617 h1:dTlZiB/kaSMezzwyEsOs2fjbbDkgoLzdLSwh7GmOIK4=
github.com/SkycoinProject/dmsg v0.0.0-20200220122410-79d9d7bac617/go.mod h1:eCoemDDyfyfNTFrapYKNEItwtRIj54UGpu4Ffcznuds=
github.com/SkycoinProject/dmsg v0.0.0-20200224064625-1b539081519c h1:TBwm7dzyUYnOG/Ycb3HBh7JshQavePHHfh5NOAzlNww=
github.com/SkycoinProject/dmsg v0.0.0-20200224064625-1b539081519c/go.mod h1:eCoemDDyfyfNTFrapYKNEItwtRIj54UGpu4Ffcznuds=
github.com/SkycoinProject/dmsg v0.0.0-20200226145926-514fc8d015a1 h1:51rz38hyi2RKpNr/CEJgsVwjy22yt8gEess2WlzibmA=
github.com/SkycoinProject/dmsg v0.0.0-20200226145926-514fc8d015a1/go.mod h1:eCoemDDyfyfNTFrapYKNEItwtRIj54UGpu4Ffcznuds=
github.com/SkycoinProject/dmsg v0.0.0-20200227084433-7605a550f502 h1:fxCUIQ5EXmo0LpryqsqREysixAqtjSO6m60nDbe12iQ=
github.com/SkycoinProject/dmsg v0.0.0-20200227084433-7605a550f502/go.mod h1:eCoemDDyfyfNTFrapYKNEItwtRIj54UGpu4Ffcznuds=
github.com/SkycoinProject/skycoin v0.26.0 h1:8/ZRZb2VM2DM4YTIitRJMZ3Yo/3H1FFmbCMx5o6ekmA=
github.com/SkycoinProject/skycoin v0.26.0/go.mod h1:xqPLOKh5B6GBZlGA7B5IJfQmCy7mwimD9NlqxR3gMXo=
github.com/SkycoinProject/skycoin v0.27.0 h1:N3IHxj8ossHOcsxLYOYugT+OaELLncYHJHxbbYLPPmY=
Expand Down Expand Up @@ -145,6 +153,8 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
Expand Down Expand Up @@ -250,6 +260,18 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 h1:+ELyKg6m8UBf0nPFSqD0mi7zUfwPyXo23HNjMnXPz7w=
golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200208060501-ecb85df21340 h1:KOcEaR10tFr7gdJV2GCKw8Os5yED1u1aOqHjOAb6d2Y=
golang.org/x/crypto v0.0.0-20200208060501-ecb85df21340/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 h1:wCWoJcFExDgyYx2m2hpHgwz8W3+FPdfldvIgzqDIhyg=
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 h1:Sy5bstxEqwwbYs6n0/pBuxKENqOeZUgD45Gp3Q3pqLg=
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200219234226-1ad67e1f0ef4 h1:4icQlpeqbz3WxfgP6Eq3szTj95KTrlH/CwzBzoxuFd0=
golang.org/x/crypto v0.0.0-20200219234226-1ad67e1f0ef4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975 h1:/Tl7pH94bvbAAHBdZJT947M/+gp0+CqQXDtMRC0fseo=
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw=
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
Expand Down Expand Up @@ -284,6 +306,14 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtD
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 h1:sfkvUWPNGwSV+8/fNqctR5lS2AqCSqYwXdrjCxp/dXo=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200219091948-cb0a6d8edb6c h1:jceGD5YNJGgGMkJz79agzOln1K9TaZUjv5ird16qniQ=
golang.org/x/sys v0.0.0-20200219091948-cb0a6d8edb6c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200301040627-c5d0d7b4ec88 h1:LNVdAhESTW4gWDhYvciNcGoS9CEcxRiUKE9kSgw+X3s=
golang.org/x/sys v0.0.0-20200301040627-c5d0d7b4ec88/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=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
74 changes: 65 additions & 9 deletions internal/skysocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import (
"fmt"
"io"
"net"
"sync"
"time"

"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/SkycoinProject/yamux"

"github.com/SkycoinProject/skywire-mainnet/pkg/router"
)

// Log is skysocks package level logger, it can be replaced with a different one from outside the package
Expand All @@ -16,18 +20,26 @@ var Log = logging.MustGetLogger("skysocks") // nolint: gochecknoglobals
type Client struct {
session *yamux.Session
listener net.Listener
once sync.Once
closeC chan struct{}
}

// NewClient constructs a new Client.
func NewClient(conn io.ReadWriteCloser) (*Client, error) {
session, err := yamux.Client(conn, nil)
func NewClient(conn net.Conn) (*Client, error) {
c := &Client{
closeC: make(chan struct{}),
}

sessionCfg := yamux.DefaultConfig()
sessionCfg.EnableKeepAlive = false
session, err := yamux.Client(conn, sessionCfg)
if err != nil {
return nil, fmt.Errorf("error creating client: yamux: %s", err)
}

c := &Client{
session: session,
}
c.session = session

go c.sessionKeepAliveLoop()

return c, nil
}
Expand All @@ -45,6 +57,12 @@ func (c *Client) ListenAndServe(addr string) error {
c.listener = l

for {
select {
case <-c.closeC:
return nil
default:
}

conn, err := l.Accept()
if err != nil {
Log.Printf("Error accepting: %v\n", err)
Expand All @@ -55,7 +73,9 @@ func (c *Client) ListenAndServe(addr string) error {

stream, err := c.session.Open()
if err != nil {
return fmt.Errorf("error on `ListenAndServe`: yamux: %s", err)
c.close()

return fmt.Errorf("error opening yamux stream: %w", err)
}

Log.Println("Opened session skysocks client")
Expand All @@ -64,6 +84,24 @@ func (c *Client) ListenAndServe(addr string) error {
}
}

func (c *Client) sessionKeepAliveLoop() {
ticker := time.NewTicker(router.DefaultRouteKeepAlive / 2)
defer ticker.Stop()

for {
select {
case <-c.closeC:
return
case <-ticker.C:
if c.session.IsClosed() {
c.close()

return
}
}
}
}

func (c *Client) handleStream(conn, stream net.Conn) {
const errorCount = 2

Expand Down Expand Up @@ -104,15 +142,33 @@ func (c *Client) handleStream(conn, stream net.Conn) {
}

close(errCh)

if c.session.IsClosed() {
c.close()
}
}

func (c *Client) close() {
Log.Error("Session failed, closing skysocks client")
if err := c.Close(); err != nil {
Log.WithError(err).Error("Error closing skysocks client")
}
}

// Close implement io.Closer.
func (c *Client) Close() error {
Log.Infoln("Closing proxy client")

if c == nil {
return nil
}

return c.listener.Close()
var err error
c.once.Do(func() {
Log.Infoln("Closing proxy client")

close(c.closeC)

err = c.listener.Close()
})

return err
}
11 changes: 9 additions & 2 deletions internal/skysocks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sync/atomic"

"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/SkycoinProject/yamux"
"github.com/armon/go-socks5"
"github.com/hashicorp/yamux"
)

// Server implements multiplexing proxy server using yamux.
Expand Down Expand Up @@ -46,13 +46,20 @@ func (s *Server) Serve(l net.Listener) error {
conn, err := l.Accept()
if err != nil {
if s.isClosed() {
s.log.WithError(err).Debugln("Failed to accept skysocks connection, but server is closed")
return nil
}

s.log.WithError(err).Debugln("Failed to accept skysocks connection")

return fmt.Errorf("accept: %s", err)
}

session, err := yamux.Server(conn, nil)
s.log.Infoln("Accepted new skysocks connection")

sessionCfg := yamux.DefaultConfig()
sessionCfg.EnableKeepAlive = false
session, err := yamux.Server(conn, sessionCfg)
if err != nil {
return fmt.Errorf("yamux server failure: %s", err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/app/appserver/rpc_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ func (r *RPCGateway) Read(req *ReadReq, resp *ReadResp) error {
copy(resp.B, buf[:resp.N])
}

fmt.Printf("ERROR READING FROM APP CONN SERVER SIDE: %v\n", err)

resp.Err = ioErrToRPCIOErr(err)

// avoid error in RPC pipeline, error is included in response body
Expand Down
7 changes: 0 additions & 7 deletions pkg/app/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ type Conn struct {
// Read reads from connection.
func (c *Conn) Read(b []byte) (int, error) {
n, err := c.rpc.Read(c.id, b)
if err == io.EOF {
fmt.Println("EOF READING FROM APP CONN")
}
/*if err != nil && err != io.EOF {
fmt.Printf("READ ERROR: %v\n", err)
return n, &net.OpError{Op: "read", Net: c.local.Network(), Source: c.local, Addr: c.remote, Err: err}
}*/

return n, err
}
Expand Down
Loading

0 comments on commit 480cc39

Please sign in to comment.