storj/cmd/connect-test/connecttest.go
paul cannon dcb73e46ca cmd/connect-test: add connect-test tool
This tool attempts to make TCP+TLS+DRPC and QUIC+DRPC connections to a
specified IP:port. If successful with either, it shows the node ID of
the opposite end of the connection.

This should be useful for SNOs who want to check that their packet
forwarding is working or that their node is listening for both
connection types.

Change-Id: I935dff037dd7f1106941a35567f6445230259d1a
2021-04-22 16:06:45 +00:00

86 lines
2.1 KiB
Go

// 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
}