github.com/core-coin/go-core/v2@v2.1.9/cmd/devp2p/rlpxcmd.go (about)

     1  // Copyright 2020 by the Authors
     2  // This file is part of go-core.
     3  //
     4  // go-core is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-core is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-core. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	crand "crypto/rand"
    21  	"fmt"
    22  	"net"
    23  
    24  	"gopkg.in/urfave/cli.v1"
    25  
    26  	xcbtest "github.com/core-coin/go-core/v2/cmd/devp2p/internal/xcbtest"
    27  	"github.com/core-coin/go-core/v2/common"
    28  	"github.com/core-coin/go-core/v2/crypto"
    29  	"github.com/core-coin/go-core/v2/p2p"
    30  	"github.com/core-coin/go-core/v2/p2p/rlpx"
    31  	"github.com/core-coin/go-core/v2/rlp"
    32  )
    33  
    34  var (
    35  	rlpxCommand = cli.Command{
    36  		Name:  "rlpx",
    37  		Usage: "RLPx Commands",
    38  		Subcommands: []cli.Command{
    39  			rlpxPingCommand,
    40  			rlpxXcbTestCommand,
    41  		},
    42  	}
    43  	rlpxPingCommand = cli.Command{
    44  		Name:   "ping",
    45  		Usage:  "ping <node>",
    46  		Action: rlpxPing,
    47  	}
    48  	rlpxXcbTestCommand = cli.Command{
    49  		Name:      "xcb-test",
    50  		Usage:     "Runs tests against a node",
    51  		ArgsUsage: "<node> <chain.rlp> <genesis.json>",
    52  		Action:    rlpxXcbTest,
    53  		Flags: []cli.Flag{
    54  			testPatternFlag,
    55  			testTAPFlag,
    56  		},
    57  	}
    58  )
    59  
    60  func rlpxPing(ctx *cli.Context) error {
    61  	n := getNodeArg(ctx)
    62  	fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP()))
    63  	if err != nil {
    64  		return err
    65  	}
    66  	conn := rlpx.NewConn(fd, n.Pubkey())
    67  	ourKey, _ := crypto.GenerateKey(crand.Reader)
    68  	_, err = conn.Handshake(ourKey)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	code, data, _, err := conn.Read()
    73  	if err != nil {
    74  		return err
    75  	}
    76  	switch code {
    77  	case 0:
    78  		var h xcbtest.Hello
    79  		if err := rlp.DecodeBytes(data, &h); err != nil {
    80  			return fmt.Errorf("invalid handshake: %v", err)
    81  		}
    82  		fmt.Printf("%+v\n", h)
    83  	case 1:
    84  		var msg []p2p.DiscReason
    85  		if rlp.DecodeBytes(data, &msg); len(msg) == 0 {
    86  			return fmt.Errorf("invalid disconnect message")
    87  		}
    88  		return fmt.Errorf("received disconnect message: %v", msg[0])
    89  	default:
    90  		return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code)
    91  	}
    92  	return nil
    93  }
    94  
    95  // rlpxXcbTest runs the xcb protocol test suite.
    96  func rlpxXcbTest(ctx *cli.Context) error {
    97  	if ctx.NArg() < 3 {
    98  		exit("missing path to chain.rlp as command-line argument")
    99  	}
   100  	common.DefaultNetworkID = 19763
   101  	suite := xcbtest.NewSuite(getNodeArg(ctx), ctx.Args()[1], ctx.Args()[2])
   102  	return runTests(ctx, suite.AllTests())
   103  }