Skip to content

Commit

Permalink
fixing cmd entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Omri Eival committed Oct 9, 2020
1 parent ebd7f6f commit 3018cd5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
9 changes: 8 additions & 1 deletion cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ ktunnel client --host ktunnel-server.yourcompany.com -s tcp 8000 8001:8432
})
}()

err := client.RunClient(ctx, &Host, &Port, Scheme, &Tls, &CaFile, &ServerHostOverride, args)
opts := []client.ClientOption{
client.WithServer(Host, Port),
client.WithTunnels(Scheme, args...),
client.WithTLS(CertFile, ServerHostOverride),

}

err := client.RunClient(ctx, opts...)
if err != nil {
log.Fatalf("Failed to run client: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ ktunnel expose redis 6379
log.Fatalf("Failed to run client: %v", err)
}
prt := int(p)
err = client.RunClient(ctx, &Host, &prt, Scheme, &Tls, &CaFile, &ServerHostOverride, args[1:])
opts := []client.ClientOption{
client.WithServer(Host, prt),
client.WithTunnels(Scheme, args[1:]...),
}
if Tls {
opts = append(opts, client.WithTLS(CertFile, ServerHostOverride))
}
err = client.RunClient(ctx, opts...)
if err != nil {
log.Fatalf("Failed to run client: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ ktunnel inject deploymeny mydeployment 3306 6379
log.Fatalf("Failed to run client: %v", err)
}
prt := int(p)
err = client.RunClient(ctx, &Host, &prt, Scheme, &Tls, &CaFile, &ServerHostOverride, args[1:])
opts := []client.ClientOption{
client.WithServer(Host, prt),
client.WithTunnels(Scheme, args[1:]...),
}
if Tls {
opts = append(opts, client.WithTLS(CertFile, ServerHostOverride))
}
err = client.RunClient(ctx, opts...)
if err != nil {
log.Fatalf("Failed to run client: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ ktunnel server -p 8181
cancel()
})
}()
err := server.RunServer(ctx, &Port, &Tls, &KeyFile, &CertFile)
config := []server.ServerOption{server.WithPort(Port)}
if CertFile != "" && KeyFile != "" {
config = append(config, server.WithTLS(CertFile, KeyFile))
}
err := server.RunServer(ctx, config...)
if err != nil {
log.Fatalf("Error running server: %v", err)
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package client
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strings"
"time"

Expand Down Expand Up @@ -150,7 +150,11 @@ func SendData(ctx context.Context, conf *ClientConfig, stream pb.Tunnel_InitTunn
session.Lock.Lock()
bys := session.Buf.Len()
bytes := make([]byte, bys)
session.Buf.Read(bytes)
_, err := session.Buf.Read(bytes)
if err != nil {
conf.log.WithError(err).Errorf("failed reading stream from session %v, exiting", err)
return
}

conf.log.WithField("session", session.Id).Debugf("read %d from buffer out of %d available", len(bytes), bys)

Expand All @@ -165,7 +169,7 @@ func SendData(ctx context.Context, conf *ClientConfig, stream pb.Tunnel_InitTunn
"session": session.Id,
"close": resp.ShouldClose,
}).Debugf("sending %d bytes to server", len(bytes))
err := stream.Send(resp)
err = stream.Send(resp)
if err != nil {
conf.log.WithError(err).Errorf("failed sending message to tunnel stream, exiting")
return
Expand Down Expand Up @@ -250,7 +254,7 @@ func processArgs(opts []ClientOption) (*ClientConfig, error) {
// default arguments
opt := &ClientConfig{
log: &log.Logger{
Out: ioutil.Discard,
Out: os.Stdout,
},
scheme: "tcp",
TLS: false,
Expand Down
5 changes: 3 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"net"
"os"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -243,7 +244,7 @@ func RunServer(ctx context.Context, opts ...ServerOption) error {
// handle context cancellation, shut down the server
go func() {
<-ctx.Done()
lis.Close()
_ = lis.Close()
}()

grpcServer := grpc.NewServer(grpcOpts...)
Expand All @@ -257,7 +258,7 @@ func processArgs(opts []ServerOption) (*ServerConfig, error) {
opt := &ServerConfig{
port: 5000,
log: &log.Logger{
Out: ioutil.Discard,
Out: os.Stdout,
},
TLS: false,
}
Expand Down

0 comments on commit 3018cd5

Please sign in to comment.