github.com/Unheilbar/quorum@v1.0.0/p2p/qlight_transport.go (about)

     1  package p2p
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"crypto/tls"
     6  	"net"
     7  
     8  	"github.com/ethereum/go-ethereum/log"
     9  	"github.com/ethereum/go-ethereum/p2p/rlpx"
    10  )
    11  
    12  var qlightTLSConfig *tls.Config
    13  
    14  func SetQLightTLSConfig(config *tls.Config) {
    15  	qlightTLSConfig = config
    16  }
    17  
    18  type tlsErrorTransport struct {
    19  	err error
    20  }
    21  
    22  func (tr *tlsErrorTransport) doEncHandshake(prv *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
    23  	return nil, tr.err
    24  }
    25  func (tr *tlsErrorTransport) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) {
    26  	return nil, tr.err
    27  }
    28  func (tr *tlsErrorTransport) ReadMsg() (Msg, error) { return Msg{}, tr.err }
    29  func (tr *tlsErrorTransport) WriteMsg(Msg) error    { return tr.err }
    30  func (tr *tlsErrorTransport) close(err error)       {}
    31  
    32  func NewQlightClientTransport(conn net.Conn, dialDest *ecdsa.PublicKey) transport {
    33  	log.Info("Setting up qlight client transport")
    34  	if qlightTLSConfig != nil {
    35  		tlsConn := tls.Client(conn, qlightTLSConfig)
    36  		err := tlsConn.Handshake()
    37  		if err != nil {
    38  			log.Error("Failure setting up qlight client transport", "err", err)
    39  			return &tlsErrorTransport{err}
    40  		}
    41  		log.Info("Qlight client tls transport established successfully")
    42  		return &rlpxTransport{conn: rlpx.NewConn(tlsConn, dialDest)}
    43  	}
    44  	return &rlpxTransport{conn: rlpx.NewConn(conn, dialDest)}
    45  }
    46  
    47  func NewQlightServerTransport(conn net.Conn, dialDest *ecdsa.PublicKey) transport {
    48  	log.Info("Setting up qlight server transport")
    49  	if qlightTLSConfig != nil {
    50  		tlsConn := tls.Server(conn, qlightTLSConfig)
    51  		err := tlsConn.Handshake()
    52  		if err != nil {
    53  			log.Error("Failure setting up qlight server transport", "err", err)
    54  			return &tlsErrorTransport{err}
    55  		}
    56  		log.Info("Qlight server tls transport established successfully")
    57  		return &rlpxTransport{conn: rlpx.NewConn(tlsConn, dialDest)}
    58  	}
    59  	return &rlpxTransport{conn: rlpx.NewConn(conn, dialDest)}
    60  }