Skip to content

Commit

Permalink
use vhost tls
Browse files Browse the repository at this point in the history
  • Loading branch information
dviejokfs committed Apr 3, 2022
1 parent 0b88583 commit cea397b
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 620 deletions.
65 changes: 0 additions & 65 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ package client

import (
"fmt"
"github.com/hashicorp/yamux"
"github.com/kfsoftware/getout/pkg/client"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"io"
"net"
"sync"
"time"
)

Expand All @@ -22,67 +18,6 @@ type clientCmd struct {
func (c *clientCmd) validate() error {
return nil
}
func startTunnel(session *yamux.Session, remoteAddress string) error {
for {
conn, err := session.Accept()
if err != nil {
log.Trace().Msgf("Failed to accept connections: %v", err)
return err
}
destConn, err := net.DialTimeout("tcp", remoteAddress, time.Second*5)
if err != nil {
log.Trace().Msgf("Failed to connect to remote address: %v", err)
conn.Write([]byte("Failed to connect to remote address"))
connCloseErr := conn.Close()
if connCloseErr != nil {
log.Trace().Msgf("Failed to close connection: %v", connCloseErr)
}
if destConn != nil {
destConn.Close()
}
return err
}
log.Debug().Msgf("client %s connected", conn.RemoteAddr().String())
copyConn := func(writer, reader net.Conn) {
defer writer.Close()
defer reader.Close()
_, err := io.Copy(writer, reader)
if err != nil {
fmt.Printf("io.Copy error: %s", err)
}
log.Info().Msgf("Connection finished")
}
_ = copyConn
var wg sync.WaitGroup
wg.Add(2)

transfer := func(side string, dst, src net.Conn) {
log.Debug().Msgf("proxing %s -> %s", src.RemoteAddr(), dst.RemoteAddr())

n, err := io.Copy(dst, src)
if err != nil {
log.Error().Msgf("%s: copy error: %s", side, err)
}

if err := src.Close(); err != nil {
log.Debug().Msgf("%s: close error: %s", side, err)
}

// not for yamux streams, but for client to local server connections
if d, ok := dst.(*net.TCPConn); ok {
if err := d.CloseWrite(); err != nil {
log.Debug().Msgf("%s: closeWrite error: %s", side, err)
}

}
wg.Done()
log.Debug().Msgf("done proxing %s -> %s: %d bytes", src.RemoteAddr(), dst.RemoteAddr(), n)
}

go transfer("remote to local", conn, destConn)
go transfer("local to remote", destConn, conn)
}
}
func (c *clientCmd) startTunnel() error {
tunnelClient := client.NewTunnelClient(c.tunnel)
remoteAddress := fmt.Sprintf("%s:%d", c.host, c.port)
Expand Down
Loading

0 comments on commit cea397b

Please sign in to comment.