66 "crypto/ecdsa"
77 "crypto/elliptic"
88 "crypto/rand"
9+ "crypto/tls"
910 "database/sql"
1011 "encoding/base64"
1112 "errors"
@@ -23,6 +24,7 @@ type Config struct {
2324 ILPAuthPrivateKey string
2425 ILPAuthKid string
2526 PGConnStr string
27+ TLSConfig * tls.Config
2628}
2729
2830// Client struct represents a QuestDB client connection. This encompasses the InfluxDB Line
@@ -31,7 +33,7 @@ type Config struct {
3133type Client struct {
3234 config Config
3335 // ilpConn is the TCP connection which allows Client to write data to QuestDB
34- ilpConn * net.TCPConn
36+ ilpConn net.Conn
3537 // pgSqlDB is the Postgres SQL DB connection which allows to read/query data from QuestDB
3638 pgSqlDB * sql.DB
3739}
@@ -57,6 +59,7 @@ func New(config Config) (*Client, error) {
5759
5860var (
5961 ErrILPNetDial = errors .New ("could not dial ilp host" )
62+ ErrILPTLSDial = errors .New ("could not dial tls host" )
6063 ErrILPNetTCPAddrResolve = errors .New ("could not resolve ilp host address" )
6164 ErrPGOpen = errors .New ("could not open postgres db" )
6265)
@@ -69,9 +72,18 @@ func (c *Client) Connect() error {
6972 return fmt .Errorf ("%w: %v" , ErrILPNetTCPAddrResolve , err )
7073 }
7174
72- conn , err := net .DialTCP ("tcp" , nil , tcpAddr )
73- if err != nil {
74- return fmt .Errorf ("%w: %v" , ErrILPNetDial , err )
75+ if c .config .TLSConfig != nil {
76+ conn , err := tls .Dial ("tcp" , c .config .ILPHost , c .config .TLSConfig )
77+ if err != nil {
78+ return fmt .Errorf ("%w: %v" , ErrILPTLSDial , err )
79+ }
80+ c .ilpConn = conn
81+ } else {
82+ conn , err := net .DialTCP ("tcp" , nil , tcpAddr )
83+ if err != nil {
84+ return fmt .Errorf ("%w: %v" , ErrILPNetDial , err )
85+ }
86+ c .ilpConn = conn
7587 }
7688
7789 if c .config .ILPAuthPrivateKey != "" {
@@ -91,8 +103,8 @@ func (c *Client) Connect() error {
91103
92104 // send key ID
93105
94- reader := bufio .NewReader (conn )
95- _ , err = conn .Write ([]byte (c .config .ILPAuthKid + "\n " ))
106+ reader := bufio .NewReader (c . ilpConn )
107+ _ , err = c . ilpConn .Write ([]byte (c .config .ILPAuthKid + "\n " ))
96108 if err != nil {
97109 return fmt .Errorf ("could not write to ilp tcp conn: %w" , err )
98110 }
@@ -114,14 +126,12 @@ func (c *Client) Connect() error {
114126 return fmt .Errorf ("could not ecdsa sign key: %w" , err )
115127 }
116128 stdSig := append (a .Bytes (), b .Bytes ()... )
117- _ , err = conn .Write ([]byte (base64 .StdEncoding .EncodeToString (stdSig ) + "\n " ))
129+ _ , err = c . ilpConn .Write ([]byte (base64 .StdEncoding .EncodeToString (stdSig ) + "\n " ))
118130 if err != nil {
119131 return fmt .Errorf ("could not write to ilp tcp conn: %w" , err )
120132 }
121133 }
122134
123- c .ilpConn = conn
124-
125135 db , err := sql .Open ("postgres" , c .config .PGConnStr )
126136 if err != nil {
127137 return fmt .Errorf ("%w: %v" , ErrPGOpen , err )
0 commit comments