Skip to content

Commit

Permalink
Support SDU. (#1)
Browse files Browse the repository at this point in the history
* Update dependencies.
* Work to support SDUs.
* Fix golint issues.
* Modified the golint target.
  • Loading branch information
masterjk authored Oct 29, 2020
1 parent fcb7172 commit 901aaff
Show file tree
Hide file tree
Showing 141 changed files with 7,690 additions and 11,164 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VERSION="0.0.1-$(shell git rev-parse --short=8 HEAD)"
DOCKER_ARGS="--rm -u $(shell id -u) -e GOCACHE=/tmp/"

GOLANG_IMAGE="golang:1.15.2"
GOLINT_IMAGE="cytopia/golint:latest"
GOLINT_IMAGE="golangci/golangci-lint:v1.31.0"

.PHONY: default fmt vet test coverage build

Expand All @@ -24,8 +24,8 @@ vet:

lint:
@echo ➭ Running go lint
@docker run "${DOCKER_ARGS}" -v `pwd`:/go/src/${PROJECT_SRCDIR} \
-w /go/src/${PROJECT_SRCDIR} ${GOLINT_IMAGE} ./... | grep -v vendor || true
@docker run --rm -v `pwd`:/app \
-w /app ${GOLINT_IMAGE} golangci-lint run -v

test:
@echo ➭ Running go test
Expand Down
103 changes: 38 additions & 65 deletions cmd/cli/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io"
"net"
"os"

Expand Down Expand Up @@ -72,41 +71,58 @@ func main() {
// Accept the unix connection
sourceConn, err := listener.AcceptUnix()
if err != nil {
log.WithError(err).Error("Error accepting the unix connection")
log.WithError(err).Error("Error accepting the unix socket connection")
os.Exit(1)
}

// Connect to destination socket
destinationConn, err := connectDestinationSocket(*destinationSocketFile)
if err != nil {
log.Error("Unable to connect to write socket")
log.WithError(err).Error("Unable to connect to destination socket")
os.Exit(1)
}

go func(sourceConn, destinationConn net.Conn) {
for {
// send request to cardano-node
err = relayMessage(sourceConn, destinationConn, borderRequest)
if err != nil {
if err != io.EOF {
log.WithError(err).Error("Error relaying message from source to destination")
}
break
}

// send response from cardano-node
err = relayMessage(destinationConn, sourceConn, borderResponse)
if err != nil {
if err != io.EOF {
log.WithError(err).Error("Error relaying message from source to destination")
}
break
}
}
go proxy(sourceConn, destinationConn, "src->dst", borderRequest)
go proxy(destinationConn, sourceConn, "dst->src", borderResponse)
}(sourceConn, destinationConn)
}
}

func proxy(source, dest net.Conn, mode, border string) {

data := []byte{}

for {
buf := make([]byte, receivePacketSize)
read, err1 := source.Read(buf)
if err1 != nil {
log.WithError(err1).Error("error reading ", mode)
break
}

_, err2 := dest.Write(buf[:read])
if err2 != nil {
log.WithError(err2).Error("error writing ", mode)
}

data = append(data, buf[:read]...)
}

sdus, err := multiplex.ParseServiceDataUnits(data)
if err != nil {
log.WithError(err).Error("Error parsing SDUs")
return
}

for _, sdu := range sdus {
fmt.Println(border)
fmt.Println(sdu.Debug())
}

log.Debug("Retiring thread for ", mode)
}

// connectDestinationSocket returns a connection to the destination unix socket, after doing some checks
func connectDestinationSocket(filename string) (net.Conn, error) {

Expand All @@ -122,46 +138,3 @@ func connectDestinationSocket(filename string) (net.Conn, error) {

return net.Dial(networkUnix, filename)
}

// debugShelleyContainer prints out the shelley container
func debugShelleyContainer(data []byte, mode string) error {
container, err := multiplex.ParseContainer(data)
if err != nil {
return err
}

fmt.Println("\n\n" + mode)
fmt.Println(container.Debug())
return nil
}

// relayMessage sends the message from the source to destination
func relayMessage(source, destination net.Conn, mode string) error {

buf := make([]byte, receivePacketSize)

// Read from socket
read, err := source.Read(buf[:])
if err != nil {
if err != io.EOF {
log.WithError(err).Error("Error reading from source socket")
}
return err
}
log.Debugf("Successfuly read [%d] bytes from the source", read)

// Decode and print out
if err = debugShelleyContainer(buf[:read], mode); err != nil {
log.WithError(err).Error("Error parsing shelly container")
return err
}

// Write to socket
wrote, err := destination.Write(buf[:read])
if err != nil {
log.WithError(err).Error("Error writing to destination socket")
return err
}
log.Debugf("Successfuly sent [%d] bytes to the destination", wrote)
return nil
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gocardano/shelley-socket-proxy
go 1.15

require (
github.com/gocardano/go-cardano-client v0.0.2
github.com/gocardano/go-cardano-client v0.0.4
github.com/sirupsen/logrus v1.7.0
golang.org/x/sys v0.0.0-20201028094953-708e7fb298ac // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gocardano/go-cardano-client v0.0.2 h1:vMFn4WD8RbEz3hnLIGDpkqOH4bsf3KHjBabEXq0zrL8=
github.com/gocardano/go-cardano-client v0.0.2/go.mod h1:TmxEJOyaI77mOug9XIK+PcjbO8mgyEJ9m4zU1nrnvys=
github.com/gocardano/go-cardano-client v0.0.3 h1:5g6VmG6XQML+0KpsIekbxMFupihhRFfjdCeM/yyKe9Y=
github.com/gocardano/go-cardano-client v0.0.3/go.mod h1:TmxEJOyaI77mOug9XIK+PcjbO8mgyEJ9m4zU1nrnvys=
github.com/gocardano/go-cardano-client v0.0.4 h1:j+IE6rd2ZyhtmXmG+Vkb9zNIx3p2mlfxG952um0SJYE=
github.com/gocardano/go-cardano-client v0.0.4/go.mod h1:TmxEJOyaI77mOug9XIK+PcjbO8mgyEJ9m4zU1nrnvys=
github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -324,6 +328,8 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201028094953-708e7fb298ac h1:bplbaOojU0hnrC9nvWJ5Nvp/gPIWKFMiGBFI9Cpp16I=
golang.org/x/sys v0.0.0-20201028094953-708e7fb298ac/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
14 changes: 7 additions & 7 deletions vendor/github.com/gocardano/go-cardano-client/errors/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 0 additions & 144 deletions vendor/github.com/gocardano/go-cardano-client/multiplex/container.go

This file was deleted.

Loading

0 comments on commit 901aaff

Please sign in to comment.