Skip to content

Commit

Permalink
Fix tests and add some other
Browse files Browse the repository at this point in the history
  • Loading branch information
ajabep committed Sep 7, 2024
1 parent 0cafd06 commit d5ae729
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 86 deletions.
2 changes: 1 addition & 1 deletion internal/httpproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func makeHandleHTTP(dest string, tlsConfig *tls.Config, reuseSockets bool) func(
return
}

defer resp.Body.Close() // nolint: errcheck
defer resp.Body.Close()
for k, vv := range resp.Header {
for _, v := range vv {
w.Header().Add(k, v)
Expand Down
28 changes: 21 additions & 7 deletions internal/tcpproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"crypto/tls"
"log"
"net"
"net/url"
"os"
"os/signal"

Expand All @@ -38,12 +39,11 @@ func newProxy(from, to string, tlsConfig *tls.Config) *proxy {

// Start the proxy. Is blocking!
func (p *proxy) start(ctx context.Context) error {

listener, err := net.Listen("tcp", p.from)
if err != nil {
return err
}
defer listener.Close() // nolint
defer listener.Close()

for {
select {
Expand All @@ -60,13 +60,14 @@ func (p *proxy) start(ctx context.Context) error {
}

func (p *proxy) handle(ctx context.Context, connection net.Conn) {
defer connection.Close()

defer connection.Close() // nolint
remote, err := tls.Dial("tcp", p.to, p.tlsConfig)
if err != nil {
connection.Write([]byte(err.Error()))
return
}
defer remote.Close() // nolint
defer remote.Close()

subctx, cancel := context.WithCancel(ctx)
go p.copy(subctx, cancel, remote, connection)
Expand All @@ -76,7 +77,6 @@ func (p *proxy) handle(ctx context.Context, connection net.Conn) {
}

func (p *proxy) copy(ctx context.Context, cancel context.CancelFunc, from, to net.Conn) {

defer cancel()

var n int
Expand All @@ -86,7 +86,6 @@ func (p *proxy) copy(ctx context.Context, cancel context.CancelFunc, from, to ne
select {

default:

for {
n, err = from.Read(buffer)
if err != nil {
Expand All @@ -106,10 +105,25 @@ func (p *proxy) copy(ctx context.Context, cancel context.CancelFunc, from, to ne

// Start starts the proxy
func Start(cfg *configuration.Configuration, tlsConfig *tls.Config) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

parsed, err := url.Parse("tcp://" + cfg.Backend)
if err != nil {
panic(err)
}
if parsed.Host != cfg.Backend {
panic("The backend definition seems to be invalid!")
}

parsed, err = url.Parse("tcp://" + cfg.ListenAddress)
if err != nil {
panic(err)
}
if parsed.Host != cfg.ListenAddress {
panic("The backend definition seems to be invalid!")
}

go func() {
if err := newProxy(cfg.ListenAddress, cfg.Backend, tlsConfig).start(ctx); err != nil {
log.Fatalln("Unable to start proxy:", err)
Expand Down
2 changes: 1 addition & 1 deletion tests/TlsServerCounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (srv *TlsServerCounter) forgeHttpResponse(id uint) string {
response := fmt.Sprintf(
`HTTP/1.1 200 OK
Content-Length: %d
Content-Type: text/plain; utf-8
Content-Type: text/plain; charset=utf-8
%s`,
len(respBody),
Expand Down
Loading

0 comments on commit d5ae729

Please sign in to comment.