tlsclient.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // File: pkg/discovery/tlsclient.go
  2. package discovery
  3. import (
  4. "crypto/tls"
  5. "sync/atomic"
  6. )
  7. // clientCertFunc is the callback used to present a TLS client certificate
  8. // when probing other devices over HTTPS.
  9. var clientCertFunc atomic.Pointer[func(*tls.CertificateRequestInfo) (*tls.Certificate, error)]
  10. // SetClientCertificate sets the callback used to present our certificate as a
  11. // TLS client certificate when contacting other devices (see
  12. // crypto.StoredSecurityContext.ClientCertificateFunc). LocalSend apps speaking
  13. // protocol v2.2 request a client certificate and drop the handshake without
  14. // one, which makes them invisible to HTTP-based discovery. Passing nil
  15. // disables it.
  16. func SetClientCertificate(fn func(*tls.CertificateRequestInfo) (*tls.Certificate, error)) {
  17. if fn == nil {
  18. clientCertFunc.Store(nil)
  19. return
  20. }
  21. clientCertFunc.Store(&fn)
  22. }
  23. // clientTLSConfig returns the TLS config for outgoing discovery requests.
  24. // Peers use self-signed certificates, so they are not verified here.
  25. func clientTLSConfig() *tls.Config {
  26. cfg := &tls.Config{InsecureSkipVerify: true}
  27. if fn := clientCertFunc.Load(); fn != nil {
  28. cfg.GetClientCertificate = *fn
  29. }
  30. return cfg
  31. }