Skip to content

Instantly share code, notes, and snippets.

@thepaul
Created April 21, 2021 17:17
Show Gist options
  • Save thepaul/774e518bc8dff7265019c5b9c73012be to your computer and use it in GitHub Desktop.
Save thepaul/774e518bc8dff7265019c5b9c73012be to your computer and use it in GitHub Desktop.
// Copyright (C) 2021 Storj, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"os"
"golang.org/x/sync/errgroup"
"storj.io/common/identity"
"storj.io/common/peertls/tlsopts"
"storj.io/common/rpc"
"storj.io/common/storj"
"storj.io/storj/pkg/quic"
)
func main() {
destAddr := os.Args[1]
ctx := context.Background()
ident, err := identity.NewFullIdentity(ctx, identity.NewCAOptions{
Difficulty: 0,
Concurrency: 1,
})
if err != nil {
log.Fatalf("could not generate an identity: %v", err)
}
tlsOptions, err := tlsopts.NewOptions(ident, tlsopts.Config{}, nil)
if err != nil {
log.Fatalf("could not get tls options: %v", err)
}
unverifiedClientConfig := tlsOptions.UnverifiedClientTLSConfig()
var (
group errgroup.Group
quicNodeID storj.NodeID
quicErr error
tcpNodeID storj.NodeID
tcpErr error
)
group.Go(func() error {
quicNodeID, quicErr = tryConnect(ctx, unverifiedClientConfig, quic.NewDefaultConnector(nil), destAddr)
return nil
})
group.Go(func() error {
tcpNodeID, tcpErr = tryConnect(ctx, unverifiedClientConfig, rpc.NewDefaultTCPConnector(nil), destAddr)
return nil
})
err = group.Wait()
if err != nil {
log.Fatalf("failed to perform checks: %v", err)
}
if quicErr != nil {
fmt.Printf("QUIC\tfail\t%v\n", quicErr)
} else {
fmt.Printf("QUIC\tsuccess\t%s\n", quicNodeID.String())
}
if tcpErr != nil {
fmt.Printf("TCP\tfail\t%v\n", tcpErr)
} else {
fmt.Printf("TCP\tsuccess\t%s\n", tcpNodeID.String())
}
if quicErr == nil && tcpErr == nil && quicNodeID.Compare(tcpNodeID) != 0 {
fmt.Printf("(warning: node IDs do not match)\n")
}
}
func tryConnect(ctx context.Context, tlsConfig *tls.Config, dialer rpc.Connector, destAddr string) (storj.NodeID, error) {
conn, err := dialer.DialContext(ctx, tlsConfig, destAddr)
if err != nil {
return storj.NodeID{}, err
}
defer func() { _ = conn.Close() }()
nodeID, err := identity.PeerIdentityFromChain(conn.ConnectionState().PeerCertificates)
if err != nil {
return storj.NodeID{}, fmt.Errorf("could not get node ID from peer certificates: %v", err)
}
return nodeID.ID, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment