Skip to content

Commit

Permalink
Add SSL support (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
tw1nk committed Jan 12, 2023
1 parent 87e7b8e commit d6eed4c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
13 changes: 10 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ func (c *client) initProtocols(proto protocols.MessageProtocol) {
}

func (c *client) initTransports(addr string, config *Config) (err error) {
c.transport, err = transports.NewSocket(addr, config.Timeout)
if err != nil {
return err
if config.TLSConfig == nil {
c.transport, err = transports.NewSocket(addr, config.Timeout)
if err != nil {
return err
}
} else {
c.transport, err = transports.NewSSLSocket(addr, config.TLSConfig)
if err != nil {
return err
}
}

if config.BufferSize > 0 {
Expand Down
25 changes: 20 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package avroipc

import "time"
import (
"crypto/tls"
"time"
)

// Config provides a configuration for the client. Use the NewConfig method
// to create an instance of the Config and set all necessary parameters of
Expand Down Expand Up @@ -36,6 +39,11 @@ type Config struct {
//
// Defaults to zero which means that the compression will be disabled.
CompressionLevel int

// Use TLS Config
//
// Defaults to false
TLSConfig *tls.Config
}

// NewConfig returns a pointer to a new Config instance that is used to
Expand All @@ -44,11 +52,13 @@ type Config struct {
// options in a single command. A NewConfig call may be also chained with
// other methods to inline config creations.
//
// config := NewConfig()
// config.WithTimeout(3*time.Second)
// client, err := NewClientWithConfig(config)
// config := NewConfig()
// config.WithTimeout(3*time.Second)
// client, err := NewClientWithConfig(config)
//
// or just
// client, err := NewClientWithConfig(NewConfig().WithTimeout(3*time.Second))
//
// client, err := NewClientWithConfig(NewConfig().WithTimeout(3*time.Second))
func NewConfig() *Config {
return &Config{}
}
Expand Down Expand Up @@ -76,3 +86,8 @@ func (c *Config) WithCompressionLevel(l int) *Config {
c.CompressionLevel = l
return c
}

func (c *Config) WithTLSConfig(cfg *tls.Config) *Config {
c.TLSConfig = cfg
return c
}
32 changes: 32 additions & 0 deletions transports/sslsocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package transports

import (
"crypto/tls"
"fmt"
"net"
)

type sslsocket struct {
net.Conn
}

var _ Transport = new(sslsocket)

func NewSSLSocket(hostPort string, tlsConfig *tls.Config) (Transport, error) {
addr, err := net.ResolveTCPAddr("tcp", hostPort)
if err != nil {
return nil, err
}

s := &sslsocket{}
s.Conn, err = tls.Dial(addr.Network(), addr.String(), tlsConfig)
if err != nil {
return nil, fmt.Errorf("TLS connection failed: %w", err)
}

return s, nil
}

func (s *sslsocket) Flush() error {
return nil
}

0 comments on commit d6eed4c

Please sign in to comment.