diff --git a/pkg/cli/output.go b/pkg/cli/output.go index f59d22f..5600e3a 100644 --- a/pkg/cli/output.go +++ b/pkg/cli/output.go @@ -1,7 +1,6 @@ package cli import ( - "crypto/sha256" "encoding/json" "fmt" "os" diff --git a/pkg/discovery/multicast_test.go b/pkg/discovery/multicast_test.go index 2c8e03c..76e0788 100644 --- a/pkg/discovery/multicast_test.go +++ b/pkg/discovery/multicast_test.go @@ -138,7 +138,7 @@ func TestMulticastDiscovery_IgnoreSelf(t *testing.T) { } defer md.Stop() - time.Sleep(100 * time.Millisecond) + time.Sleep(200 * time.Millisecond) md.SendDiscoveryAnnouncement() diff --git a/pkg/send/send.go b/pkg/send/send.go index aa22f53..78a5f46 100644 --- a/pkg/send/send.go +++ b/pkg/send/send.go @@ -147,25 +147,38 @@ func SendToDevice(ctx context.Context, cfg *config.Config, device *model.Device, client := &http.Client{} scheme := "http" + if device.Protocol == "" { + addr := net.JoinHostPort(device.IP, strconv.Itoa(device.Port)) + dialer := &net.Dialer{Timeout: 2 * time.Second} + conn, err := tls.DialWithDialer(dialer, "tcp", addr, &tls.Config{InsecureSkipVerify: true}) + if err == nil { + conn.Close() + device.Protocol = model.ProtocolTypeHTTPS + } + } + if device.Protocol == model.ProtocolTypeHTTPS { scheme = "https" - expectedFingerprint := device.Fingerprint + tlsConfig := &tls.Config{ + InsecureSkipVerify: true, + } + if device.Fingerprint != "" { + expectedFingerprint := device.Fingerprint + tlsConfig.VerifyConnection = func(state tls.ConnectionState) error { + if len(state.PeerCertificates) == 0 { + return fmt.Errorf("no peer certificates presented") + } + cert := state.PeerCertificates[0] + hash := sha256.Sum256(cert.Raw) + actual := hex.EncodeToString(hash[:]) + if !strings.EqualFold(actual, expectedFingerprint) { + return fmt.Errorf("TLS certificate fingerprint mismatch: expected %s, got %s", expectedFingerprint, actual) + } + return nil + } + } tr := &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, - VerifyConnection: func(state tls.ConnectionState) error { - if len(state.PeerCertificates) == 0 { - return fmt.Errorf("no peer certificates presented") - } - cert := state.PeerCertificates[0] - hash := sha256.Sum256(cert.Raw) - actual := hex.EncodeToString(hash[:]) - if !strings.EqualFold(actual, expectedFingerprint) { - return fmt.Errorf("TLS certificate fingerprint mismatch: expected %s, got %s", expectedFingerprint, actual) - } - return nil - }, - }, + TLSClientConfig: tlsConfig, } client.Transport = tr defer tr.CloseIdleConnections()