@@ -24,6 +24,7 @@ type Config struct {
2424 ServerPEM string `json:"server_pem"`
2525 ServerKey string `json:"server_key"`
2626 ClientPEM string `json:"client_pem"`
27+ MTLS * bool `json:"mtls,omitempty"` // Use pointer to distinguish between false and missing
2728 ListenAddr string `json:"listen_addr"`
2829 AuthUsers map [string ]string `json:"auth_users,omitempty"`
2930}
@@ -32,14 +33,16 @@ type Server struct {
3233 serverPEM string
3334 serverKEY string
3435 clientPEM string
36+ mtls bool
3537 authUsers map [string ]string
3638}
3739
38- func NewServer (serverPEM string , serverKEY string , clientPEM string , authUsers map [string ]string ) * Server {
40+ func NewServer (serverPEM string , serverKEY string , clientPEM string , mtls bool , authUsers map [string ]string ) * Server {
3941 return & Server {
4042 serverPEM : serverPEM ,
4143 serverKEY : serverKEY ,
4244 clientPEM : clientPEM ,
45+ mtls : mtls ,
4346 authUsers : authUsers ,
4447 }
4548}
@@ -219,25 +222,32 @@ func (s *Server) ListenAndServe(addr string) error {
219222 return errors .New ("failed to load server certificate and key: " + err .Error ())
220223 }
221224
222- certBytes , err := os .ReadFile (s .clientPEM )
223- if err != nil {
224- return errors .New ("failed to read client CA file: " + err .Error ())
225- }
226-
227- clientCertPool := x509 .NewCertPool ()
228- if ok := clientCertPool .AppendCertsFromPEM (certBytes ); ! ok {
229- return errors .New ("failed to parse client CA certificates" )
230- }
231-
232225 tlsConfig := & tls.Config {
233226 MinVersion : tls .VersionTLS12 ,
234227 Certificates : []tls.Certificate {cert },
235- ClientAuth : tls .RequireAndVerifyClientCert ,
236- ClientCAs : clientCertPool ,
237228 SessionTicketsDisabled : false ,
238229 ClientSessionCache : tls .NewLRUClientSessionCache (128 ),
239230 }
240231
232+ if s .mtls {
233+ certBytes , err := os .ReadFile (s .clientPEM )
234+ if err != nil {
235+ return errors .New ("failed to read client CA file: " + err .Error ())
236+ }
237+
238+ clientCertPool := x509 .NewCertPool ()
239+ if ok := clientCertPool .AppendCertsFromPEM (certBytes ); ! ok {
240+ return errors .New ("failed to parse client CA certificates" )
241+ }
242+
243+ tlsConfig .ClientAuth = tls .RequireAndVerifyClientCert
244+ tlsConfig .ClientCAs = clientCertPool
245+ log .Printf ("[SYSTEM] Server listening on %s (mTLS Enabled)" , addr )
246+ } else {
247+ tlsConfig .ClientAuth = tls .NoClientCert
248+ log .Printf ("[SYSTEM] Server listening on %s (mTLS Disabled)" , addr )
249+ }
250+
241251 listener , err := tls .Listen ("tcp" , addr , tlsConfig )
242252 if err != nil {
243253 return err
@@ -295,7 +305,12 @@ func main() {
295305 log .Fatalf ("Failed to parse configuration file: %v" , err )
296306 }
297307
298- s := NewServer (config .ServerPEM , config .ServerKey , config .ClientPEM , config .AuthUsers )
308+ mtlsEnabled := true
309+ if config .MTLS != nil {
310+ mtlsEnabled = * config .MTLS
311+ }
312+
313+ s := NewServer (config .ServerPEM , config .ServerKey , config .ClientPEM , mtlsEnabled , config .AuthUsers )
299314
300315 if err := s .ListenAndServe (config .ListenAddr ); err != nil {
301316 log .Fatalf ("Server failed: %v" , err )
0 commit comments