Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pkg/cli/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"crypto/sha256"
"encoding/json"
"fmt"
"os"
Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/multicast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
45 changes: 29 additions & 16 deletions pkg/send/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading