@@ -65,37 +65,38 @@ func (c *commImpl) SetDialOpts(opts ...grpc.DialOption) {
65
65
}
66
66
67
67
// NewCommInstanceWithServer creates a comm instance that creates an underlying gRPC server
68
- func NewCommInstanceWithServer (port int , idMapper identity.Mapper , peerIdentity api.PeerIdentityType , dialOpts ... grpc.DialOption ) (Comm , error ) {
68
+ func NewCommInstanceWithServer (port int , idMapper identity.Mapper , peerIdentity api.PeerIdentityType ,
69
+ secureDialOpts api.PeerSecureDialOpts , dialOpts ... grpc.DialOption ) (Comm , error ) {
70
+
69
71
var ll net.Listener
70
72
var s * grpc.Server
71
- var secOpt grpc.DialOption
72
73
var certHash []byte
73
74
74
75
if len (dialOpts ) == 0 {
75
76
dialOpts = []grpc.DialOption {grpc .WithTimeout (util .GetDurationOrDefault ("peer.gossip.dialTimeout" , defDialTimeout ))}
76
77
}
77
78
78
79
if port > 0 {
79
- s , ll , secOpt , certHash = createGRPCLayer (port )
80
- dialOpts = append (dialOpts , secOpt )
80
+ s , ll , secureDialOpts , certHash = createGRPCLayer (port )
81
81
}
82
82
83
83
commInst := & commImpl {
84
- selfCertHash : certHash ,
85
- PKIID : idMapper .GetPKIidOfCert (peerIdentity ),
86
- idMapper : idMapper ,
87
- logger : util .GetLogger (util .LoggingCommModule , fmt .Sprintf ("%d" , port )),
88
- peerIdentity : peerIdentity ,
89
- opts : dialOpts ,
90
- port : port ,
91
- lsnr : ll ,
92
- gSrv : s ,
93
- msgPublisher : NewChannelDemultiplexer (),
94
- lock : & sync.RWMutex {},
95
- deadEndpoints : make (chan common.PKIidType , 100 ),
96
- stopping : int32 (0 ),
97
- exitChan : make (chan struct {}, 1 ),
98
- subscriptions : make ([]chan proto.ReceivedMessage , 0 ),
84
+ selfCertHash : certHash ,
85
+ PKIID : idMapper .GetPKIidOfCert (peerIdentity ),
86
+ idMapper : idMapper ,
87
+ logger : util .GetLogger (util .LoggingCommModule , fmt .Sprintf ("%d" , port )),
88
+ peerIdentity : peerIdentity ,
89
+ opts : dialOpts ,
90
+ secureDialOpts : secureDialOpts ,
91
+ port : port ,
92
+ lsnr : ll ,
93
+ gSrv : s ,
94
+ msgPublisher : NewChannelDemultiplexer (),
95
+ lock : & sync.RWMutex {},
96
+ deadEndpoints : make (chan common.PKIidType , 100 ),
97
+ stopping : int32 (0 ),
98
+ exitChan : make (chan struct {}, 1 ),
99
+ subscriptions : make ([]chan proto.ReceivedMessage , 0 ),
99
100
}
100
101
commInst .connStore = newConnStore (commInst , commInst .logger )
101
102
commInst .idMapper .Put (idMapper .GetPKIidOfCert (peerIdentity ), peerIdentity )
@@ -117,9 +118,12 @@ func NewCommInstanceWithServer(port int, idMapper identity.Mapper, peerIdentity
117
118
}
118
119
119
120
// NewCommInstance creates a new comm instance that binds itself to the given gRPC server
120
- func NewCommInstance (s * grpc.Server , cert * tls.Certificate , idStore identity.Mapper , peerIdentity api.PeerIdentityType , dialOpts ... grpc.DialOption ) (Comm , error ) {
121
+ func NewCommInstance (s * grpc.Server , cert * tls.Certificate , idStore identity.Mapper ,
122
+ peerIdentity api.PeerIdentityType , secureDialOpts api.PeerSecureDialOpts ,
123
+ dialOpts ... grpc.DialOption ) (Comm , error ) {
124
+
121
125
dialOpts = append (dialOpts , grpc .WithTimeout (util .GetDurationOrDefault ("peer.gossip.dialTimeout" , defDialTimeout )))
122
- commInst , err := NewCommInstanceWithServer (- 1 , idStore , peerIdentity , dialOpts ... )
126
+ commInst , err := NewCommInstanceWithServer (- 1 , idStore , peerIdentity , secureDialOpts , dialOpts ... )
123
127
if err != nil {
124
128
return nil , err
125
129
}
@@ -139,24 +143,25 @@ func NewCommInstance(s *grpc.Server, cert *tls.Certificate, idStore identity.Map
139
143
}
140
144
141
145
type commImpl struct {
142
- skipHandshake bool
143
- selfCertHash []byte
144
- peerIdentity api.PeerIdentityType
145
- idMapper identity.Mapper
146
- logger * logging.Logger
147
- opts []grpc.DialOption
148
- connStore * connectionStore
149
- PKIID []byte
150
- port int
151
- deadEndpoints chan common.PKIidType
152
- msgPublisher * ChannelDeMultiplexer
153
- lock * sync.RWMutex
154
- lsnr net.Listener
155
- gSrv * grpc.Server
156
- exitChan chan struct {}
157
- stopping int32
158
- stopWG sync.WaitGroup
159
- subscriptions []chan proto.ReceivedMessage
146
+ skipHandshake bool
147
+ selfCertHash []byte
148
+ peerIdentity api.PeerIdentityType
149
+ idMapper identity.Mapper
150
+ logger * logging.Logger
151
+ opts []grpc.DialOption
152
+ secureDialOpts func () []grpc.DialOption
153
+ connStore * connectionStore
154
+ PKIID []byte
155
+ port int
156
+ deadEndpoints chan common.PKIidType
157
+ msgPublisher * ChannelDeMultiplexer
158
+ lock * sync.RWMutex
159
+ lsnr net.Listener
160
+ gSrv * grpc.Server
161
+ exitChan chan struct {}
162
+ stopping int32
163
+ stopWG sync.WaitGroup
164
+ subscriptions []chan proto.ReceivedMessage
160
165
}
161
166
162
167
func (c * commImpl ) createConnection (endpoint string , expectedPKIID common.PKIidType ) (* connection , error ) {
@@ -165,14 +170,18 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
165
170
var stream proto.Gossip_GossipStreamClient
166
171
var pkiID common.PKIidType
167
172
var connInfo * proto.ConnectionInfo
173
+ var dialOpts []grpc.DialOption
168
174
169
175
c .logger .Debug ("Entering" , endpoint , expectedPKIID )
170
176
defer c .logger .Debug ("Exiting" )
171
177
172
178
if c .isStopping () {
173
179
return nil , errors .New ("Stopping" )
174
180
}
175
- cc , err = grpc .Dial (endpoint , append (c .opts , grpc .WithBlock ())... )
181
+ dialOpts = append (dialOpts , c .secureDialOpts ()... )
182
+ dialOpts = append (dialOpts , grpc .WithBlock ())
183
+ dialOpts = append (dialOpts , c .opts ... )
184
+ cc , err = grpc .Dial (endpoint , dialOpts ... )
176
185
if err != nil {
177
186
return nil , err
178
187
}
@@ -257,13 +266,18 @@ func (c *commImpl) isStopping() bool {
257
266
}
258
267
259
268
func (c * commImpl ) Probe (remotePeer * RemotePeer ) error {
269
+ var dialOpts []grpc.DialOption
260
270
endpoint := remotePeer .Endpoint
261
271
pkiID := remotePeer .PKIID
262
272
if c .isStopping () {
263
273
return errors .New ("Stopping" )
264
274
}
265
275
c .logger .Debug ("Entering, endpoint:" , endpoint , "PKIID:" , pkiID )
266
- cc , err := grpc .Dial (remotePeer .Endpoint , append (c .opts , grpc .WithBlock ())... )
276
+ dialOpts = append (dialOpts , c .secureDialOpts ()... )
277
+ dialOpts = append (dialOpts , grpc .WithBlock ())
278
+ dialOpts = append (dialOpts , c .opts ... )
279
+
280
+ cc , err := grpc .Dial (remotePeer .Endpoint , dialOpts ... )
267
281
if err != nil {
268
282
c .logger .Debug ("Returning" , err )
269
283
return err
@@ -276,7 +290,12 @@ func (c *commImpl) Probe(remotePeer *RemotePeer) error {
276
290
}
277
291
278
292
func (c * commImpl ) Handshake (remotePeer * RemotePeer ) (api.PeerIdentityType , error ) {
279
- cc , err := grpc .Dial (remotePeer .Endpoint , append (c .opts , grpc .WithBlock ())... )
293
+ var dialOpts []grpc.DialOption
294
+ dialOpts = append (dialOpts , c .secureDialOpts ()... )
295
+ dialOpts = append (dialOpts , grpc .WithBlock ())
296
+ dialOpts = append (dialOpts , c .opts ... )
297
+
298
+ cc , err := grpc .Dial (remotePeer .Endpoint , dialOpts ... )
280
299
if err != nil {
281
300
return nil , err
282
301
}
@@ -590,13 +609,13 @@ type stream interface {
590
609
grpc.Stream
591
610
}
592
611
593
- func createGRPCLayer (port int ) (* grpc.Server , net.Listener , grpc. DialOption , []byte ) {
612
+ func createGRPCLayer (port int ) (* grpc.Server , net.Listener , api. PeerSecureDialOpts , []byte ) {
594
613
var returnedCertHash []byte
595
614
var s * grpc.Server
596
615
var ll net.Listener
597
616
var err error
598
617
var serverOpts []grpc.ServerOption
599
- var dialOpts grpc.DialOption
618
+ var dialOpts [] grpc.DialOption
600
619
601
620
keyFileName := fmt .Sprintf ("key.%d.pem" , util .RandomUInt64 ())
602
621
certFileName := fmt .Sprintf ("cert.%d.pem" , util .RandomUInt64 ())
@@ -627,17 +646,19 @@ func createGRPCLayer(port int) (*grpc.Server, net.Listener, grpc.DialOption, []b
627
646
Certificates : []tls.Certificate {cert },
628
647
InsecureSkipVerify : true ,
629
648
})
630
- dialOpts = grpc .WithTransportCredentials (& authCreds {tlsCreds : ta })
649
+ dialOpts = append ( dialOpts , grpc .WithTransportCredentials (& authCreds {tlsCreds : ta }) )
631
650
} else {
632
- dialOpts = grpc .WithInsecure ()
651
+ dialOpts = append ( dialOpts , grpc .WithInsecure () )
633
652
}
634
653
635
654
listenAddress := fmt .Sprintf ("%s:%d" , "" , port )
636
655
ll , err = net .Listen ("tcp" , listenAddress )
637
656
if err != nil {
638
657
panic (err )
639
658
}
640
-
659
+ secureDialOpts := func () []grpc.DialOption {
660
+ return dialOpts
661
+ }
641
662
s = grpc .NewServer (serverOpts ... )
642
- return s , ll , dialOpts , returnedCertHash
663
+ return s , ll , secureDialOpts , returnedCertHash
643
664
}
0 commit comments