@@ -63,13 +63,7 @@ type config struct {
6363 serverAddress string
6464 // tlsEnabled indicates whether to use TLS when connecting to the server
6565 tlsEnabled bool
66- // tlsDomain is the domain to use for TLS when connecting to the server
67- // If empty, automatically extracts the hostname/IP from serverAddress
68- tlsDomain string
69- // tlsCAFile is the path to the CA file to use for TLS
70- tlsCAFile string
71- // tlsValidateCertificate indicates whether to validate the server's TLS certificate
72- tlsValidateCertificate bool
66+ tls tlsConfig
7367 // autoLogin indicates whether to automatically login user after establishing connection.
7468 autoLogin AutoLogin
7569 // reconnection indicates whether to automatically reconnect when disconnected
@@ -80,14 +74,12 @@ type config struct {
8074
8175func defaultTcpClientConfig () config {
8276 return config {
83- serverAddress : "127.0.0.1:8090" ,
84- tlsEnabled : false ,
85- tlsDomain : "" ,
86- tlsCAFile : "" ,
87- tlsValidateCertificate : true ,
88- autoLogin : AutoLogin {},
89- reconnection : defaultTcpClientReconnectionConfig (),
90- noDelay : false ,
77+ serverAddress : "127.0.0.1:8090" ,
78+ tlsEnabled : false ,
79+ tls : defaultTLSConfig (),
80+ autoLogin : AutoLogin {},
81+ reconnection : defaultTcpClientReconnectionConfig (),
82+ noDelay : false ,
9183 }
9284}
9385
@@ -107,6 +99,24 @@ func defaultTcpClientReconnectionConfig() tcpClientReconnectionConfig {
10799 }
108100}
109101
102+ type tlsConfig struct {
103+ // tlsDomain is the domain to use for TLS when connecting to the server
104+ // If empty, automatically extracts the hostname/IP from serverAddress
105+ tlsDomain string
106+ // tlsCAFile is the path to the CA file to use for TLS
107+ tlsCAFile string
108+ // tlsValidateCertificate indicates whether to validate the server's TLS certificate
109+ tlsValidateCertificate bool
110+ }
111+
112+ func defaultTLSConfig () tlsConfig {
113+ return tlsConfig {
114+ tlsDomain : "" ,
115+ tlsCAFile : "" ,
116+ tlsValidateCertificate : true ,
117+ }
118+ }
119+
110120type AutoLogin struct {
111121 enabled bool
112122 credentials Credentials
@@ -145,32 +155,40 @@ func WithServerAddress(address string) Option {
145155 }
146156}
147157
148- // WithTLS enables or disables TLS for the TCP client.
149- func WithTLS (enabled bool ) Option {
158+ // TLSOption is a functional option for configuring TLS settings.
159+ type TLSOption func (cfg * tlsConfig )
160+
161+ // WithTLS enables TLS for the TCP client and applies the given TLS options.
162+ func WithTLS (tlsOpts ... TLSOption ) Option {
150163 return func (opts * Options ) {
151- opts .config .tlsEnabled = enabled
164+ opts .config .tlsEnabled = true
165+ for _ , tlsOpt := range tlsOpts {
166+ if tlsOpt != nil {
167+ tlsOpt (& opts .config .tls )
168+ }
169+ }
152170 }
153171}
154172
155173// WithTLSDomain sets the TLS domain for server name indication (SNI).
156- // If empty , the domain will be automatically extracted from the server address.
157- func WithTLSDomain (domain string ) Option {
158- return func (opts * Options ) {
159- opts . config .tlsDomain = domain
174+ // If not provided , the domain will be automatically extracted from the server address.
175+ func WithTLSDomain (domain string ) TLSOption {
176+ return func (cfg * tlsConfig ) {
177+ cfg .tlsDomain = domain
160178 }
161179}
162180
163181// WithTLSCAFile sets the path to the CA certificate file for TLS verification.
164- func WithTLSCAFile (path string ) Option {
165- return func (opts * Options ) {
166- opts . config .tlsCAFile = path
182+ func WithTLSCAFile (path string ) TLSOption {
183+ return func (cfg * tlsConfig ) {
184+ cfg .tlsCAFile = path
167185 }
168186}
169187
170188// WithTLSValidateCertificate enables or disables TLS certificate validation.
171- func WithTLSValidateCertificate (validate bool ) Option {
172- return func (opts * Options ) {
173- opts . config .tlsValidateCertificate = validate
189+ func WithTLSValidateCertificate (validate bool ) TLSOption {
190+ return func (cfg * tlsConfig ) {
191+ cfg .tlsValidateCertificate = validate
174192 }
175193}
176194
@@ -392,11 +410,11 @@ func (c *IggyTcpClient) connect() error {
392410
393411func (c * IggyTcpClient ) createTLSConfig () (* tls.Config , error ) {
394412 tlsConfig := & tls.Config {
395- InsecureSkipVerify : ! c .config .tlsValidateCertificate ,
413+ InsecureSkipVerify : ! c .config .tls . tlsValidateCertificate ,
396414 }
397415
398416 // Set server name for SNI
399- serverName := c .config .tlsDomain
417+ serverName := c .config .tls . tlsDomain
400418 if serverName == "" {
401419 // Extract hostname from server address (format: "host:port")
402420 host := c .currentServerAddress
@@ -412,8 +430,8 @@ func (c *IggyTcpClient) createTLSConfig() (*tls.Config, error) {
412430 tlsConfig .ServerName = serverName
413431
414432 // Load CA certificate if provided
415- if c .config .tlsCAFile != "" {
416- caCert , err := os .ReadFile (c .config .tlsCAFile )
433+ if c .config .tls . tlsCAFile != "" {
434+ caCert , err := os .ReadFile (c .config .tls . tlsCAFile )
417435 if err != nil {
418436 return nil , ierror .ErrInvalidTlsCertificatePath
419437 }
0 commit comments