@@ -9,14 +9,10 @@ import (
9
9
"text/tabwriter"
10
10
11
11
cmds "github.com/ipfs/go-ipfs/commands"
12
- core "github.com/ipfs/go-ipfs/core"
13
- corenet "github.com/ipfs/go-ipfs/corenet"
12
+ "github.com/ipfs/go-ipfs/core"
14
13
cnet "github.com/ipfs/go-ipfs/corenet/net"
15
14
16
- net "gx/ipfs/QmRscs8KxrSmSv4iuevHv8JfuUzHBMoqiaHzxfDRiksd6e/go-libp2p-net"
17
- peerstore "gx/ipfs/QmXZSd1qR5BxZkPyuwfT5jpqQFScZccoZvDneXsKzCNHWX/go-libp2p-peerstore"
18
15
ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
19
- manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
20
16
)
21
17
22
18
// CorenetAppInfoOutput is output type of ls command
@@ -188,6 +184,12 @@ var corenetStreamsCmd = &cmds.Command{
188
184
var corenetListenCmd = & cmds.Command {
189
185
Helptext : cmds.HelpText {
190
186
Tagline : "Create application protocol listener and proxy to network multiaddr." ,
187
+ ShortDescription : `
188
+ Register a p2p connection handler and proxies the connections to a specified
189
+ address.
190
+
191
+ Note that the connections originate from the ipfs daemon process.
192
+ ` ,
191
193
},
192
194
Arguments : []cmds.Argument {
193
195
cmds .StringArg ("Protocol" , true , false , "Protocol identifier." ),
@@ -212,7 +214,7 @@ var corenetListenCmd = &cmds.Command{
212
214
}
213
215
214
216
proto := "/app/" + req .Arguments ()[0 ]
215
- if checkProtoExists ( n . PeerHost . Mux (). Protocols () , proto ) {
217
+ if cnet . CheckProtoExists ( n , proto ) {
216
218
res .SetError (errors .New ("protocol handler already registered" ), cmds .ErrNormal )
217
219
return
218
220
}
@@ -223,25 +225,12 @@ var corenetListenCmd = &cmds.Command{
223
225
return
224
226
}
225
227
226
- listener , err : = cnet .Listen (n , proto )
228
+ _ , err = cnet .NewListener (n , proto , addr )
227
229
if err != nil {
228
230
res .SetError (err , cmds .ErrNormal )
229
231
return
230
232
}
231
233
232
- app := corenet.AppInfo {
233
- Identity : n .Identity ,
234
- Protocol : proto ,
235
- Address : addr ,
236
- Closer : listener ,
237
- Running : true ,
238
- Registry : & n .Corenet .Apps ,
239
- }
240
-
241
- go acceptStreams (n , & app , listener )
242
-
243
- n .Corenet .Apps .Register (& app )
244
-
245
234
// Successful response.
246
235
res .SetOutput (& CorenetAppInfoOutput {
247
236
Protocol : proto ,
@@ -250,66 +239,17 @@ var corenetListenCmd = &cmds.Command{
250
239
},
251
240
}
252
241
253
- func checkProtoExists (protos []string , proto string ) bool {
254
- for _ , p := range protos {
255
- if p != proto {
256
- continue
257
- }
258
- return true
259
- }
260
- return false
261
- }
262
-
263
- func acceptStreams (n * core.IpfsNode , app * corenet.AppInfo , listener cnet.Listener ) {
264
- for app .Running {
265
- remote , err := listener .Accept ()
266
- if err != nil {
267
- listener .Close ()
268
- break
269
- }
270
-
271
- local , err := manet .Dial (app .Address )
272
- if err != nil {
273
- remote .Close ()
274
- continue
275
- }
276
-
277
- stream := corenet.StreamInfo {
278
- Protocol : app .Protocol ,
279
-
280
- LocalPeer : app .Identity ,
281
- LocalAddr : app .Address ,
282
-
283
- RemotePeer : remote .Conn ().RemotePeer (),
284
- RemoteAddr : remote .Conn ().RemoteMultiaddr (),
285
-
286
- Local : local ,
287
- Remote : remote ,
288
-
289
- Registry : & n .Corenet .Streams ,
290
- }
291
-
292
- n .Corenet .Streams .Register (& stream )
293
- startStreaming (& stream )
294
- }
295
- n .Corenet .Apps .Deregister (app .Protocol )
296
- }
297
-
298
- func startStreaming (stream * corenet.StreamInfo ) {
299
- go func () {
300
- io .Copy (stream .Local , stream .Remote )
301
- stream .Close ()
302
- }()
303
-
304
- go func () {
305
- io .Copy (stream .Remote , stream .Local )
306
- stream .Close ()
307
- }()
308
- }
309
-
310
242
var corenetDialCmd = & cmds.Command {
311
243
Helptext : cmds.HelpText {
312
244
Tagline : "Dial to an application service." ,
245
+
246
+ ShortDescription : `
247
+ Establish a new connection to a peer service.
248
+
249
+ When a connection is made to a peer service the ipfs daemon will setup one time
250
+ TCP listener and return it's bind port, this way a dialing application can
251
+ transparently connect to a corenet service.
252
+ ` ,
313
253
},
314
254
Arguments : []cmds.Argument {
315
255
cmds .StringArg ("Peer" , true , false , "Remote peer to connect to" ),
@@ -351,47 +291,12 @@ var corenetDialCmd = &cmds.Command{
351
291
}
352
292
}
353
293
354
- lnet , _ , err := manet . DialArgs ( bindAddr )
294
+ app , err := cnet . Dial ( n , addr , peer , proto , bindAddr )
355
295
if err != nil {
356
296
res .SetError (err , cmds .ErrNormal )
357
297
return
358
298
}
359
299
360
- app := corenet.AppInfo {
361
- Identity : n .Identity ,
362
- Protocol : proto ,
363
- }
364
-
365
- n .Peerstore .AddAddr (peer , addr , peerstore .TempAddrTTL )
366
-
367
- remote , err := cnet .Dial (n , peer , proto )
368
- if err != nil {
369
- res .SetError (err , cmds .ErrNormal )
370
- return
371
- }
372
-
373
- switch lnet {
374
- case "tcp" , "tcp4" , "tcp6" :
375
- listener , err := manet .Listen (bindAddr )
376
- if err != nil {
377
- res .SetError (err , cmds .ErrNormal )
378
- if err := remote .Close (); err != nil {
379
- res .SetError (err , cmds .ErrNormal )
380
- }
381
- return
382
- }
383
-
384
- app .Address = listener .Multiaddr ()
385
- app .Closer = listener
386
- app .Running = true
387
-
388
- go doAccept (n , & app , remote , listener )
389
-
390
- default :
391
- res .SetError (errors .New ("unsupported protocol: " + lnet ), cmds .ErrNormal )
392
- return
393
- }
394
-
395
300
output := CorenetAppInfoOutput {
396
301
Protocol : app .Protocol ,
397
302
Address : app .Address .String (),
@@ -401,33 +306,6 @@ var corenetDialCmd = &cmds.Command{
401
306
},
402
307
}
403
308
404
- func doAccept (n * core.IpfsNode , app * corenet.AppInfo , remote net.Stream , listener manet.Listener ) {
405
- defer listener .Close ()
406
-
407
- local , err := listener .Accept ()
408
- if err != nil {
409
- return
410
- }
411
-
412
- stream := corenet.StreamInfo {
413
- Protocol : app .Protocol ,
414
-
415
- LocalPeer : app .Identity ,
416
- LocalAddr : app .Address ,
417
-
418
- RemotePeer : remote .Conn ().RemotePeer (),
419
- RemoteAddr : remote .Conn ().RemoteMultiaddr (),
420
-
421
- Local : local ,
422
- Remote : remote ,
423
-
424
- Registry : & n .Corenet .Streams ,
425
- }
426
-
427
- n .Corenet .Streams .Register (& stream )
428
- startStreaming (& stream )
429
- }
430
-
431
309
var corenetCloseCmd = & cmds.Command {
432
310
Helptext : cmds.HelpText {
433
311
Tagline : "Closes an active stream listener or client." ,
@@ -464,15 +342,15 @@ var corenetCloseCmd = &cmds.Command{
464
342
465
343
useHandlerID := false
466
344
467
- if ! closeAll && len (req .Arguments ()) == 0 {
468
- res .SetError (errors .New ("no handlerID nor stream protocol specified" ), cmds .ErrNormal )
469
- return
345
+ if ! closeAll {
346
+ if len (req .Arguments ()) == 0 {
347
+ res .SetError (errors .New ("no handlerID nor stream protocol specified" ), cmds .ErrNormal )
348
+ return
349
+ }
470
350
471
- } else if ! closeAll {
472
351
handlerID , err = strconv .ParseUint (req .Arguments ()[0 ], 10 , 64 )
473
352
if err != nil {
474
353
proto = "/app/" + req .Arguments ()[0 ]
475
-
476
354
} else {
477
355
useHandlerID = true
478
356
}
0 commit comments