This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from libp2p/cmd-utilities
add command line client and server
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
ic "github.com/libp2p/go-libp2p-core/crypto" | ||
peer "github.com/libp2p/go-libp2p-core/peer" | ||
libp2pquic "github.com/libp2p/go-libp2p-quic-transport" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 3 { | ||
fmt.Printf("Usage: %s <multiaddr> <peer id>", os.Args[0]) | ||
return | ||
} | ||
if err := run(os.Args[1], os.Args[2]); err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
} | ||
|
||
func run(raddr string, p string) error { | ||
peerID, err := peer.Decode(p) | ||
if err != nil { | ||
return err | ||
} | ||
addr, err := ma.NewMultiaddr(raddr) | ||
if err != nil { | ||
return err | ||
} | ||
priv, _, err := ic.GenerateECDSAKeyPair(rand.Reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t, err := libp2pquic.NewTransport(priv, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("Dialing %s\n", addr.String()) | ||
conn, err := t.Dial(context.Background(), addr, peerID) | ||
if err != nil { | ||
return err | ||
} | ||
str, err := conn.OpenStream() | ||
if err != nil { | ||
return err | ||
} | ||
const msg = "Hello world!" | ||
log.Printf("Sending: %s\n", msg) | ||
if _, err := str.Write([]byte(msg)); err != nil { | ||
return err | ||
} | ||
if err := str.Close(); err != nil { | ||
return err | ||
} | ||
data, err := ioutil.ReadAll(str) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Received: %s\n", data) | ||
return conn.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
ic "github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
tpt "github.com/libp2p/go-libp2p-core/transport" | ||
libp2pquic "github.com/libp2p/go-libp2p-quic-transport" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Printf("Usage: %s <port>", os.Args[0]) | ||
return | ||
} | ||
if err := run(os.Args[1]); err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
} | ||
|
||
func run(port string) error { | ||
addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/udp/%s/quic", port)) | ||
if err != nil { | ||
return err | ||
} | ||
priv, _, err := ic.GenerateECDSAKeyPair(rand.Reader) | ||
if err != nil { | ||
return err | ||
} | ||
peerID, err := peer.IDFromPrivateKey(priv) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t, err := libp2pquic.NewTransport(priv, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ln, err := t.Listen(addr) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Listening. Now run: go run cmd/client/main.go %s %s\n", ln.Multiaddr(), peerID) | ||
for { | ||
conn, err := ln.Accept() | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Accepted new connection from %s (%s)\n", conn.RemotePeer(), conn.RemoteMultiaddr()) | ||
go func() { | ||
if err := handleConn(conn); err != nil { | ||
log.Printf("handling conn failed: %s", err.Error()) | ||
} | ||
}() | ||
} | ||
} | ||
|
||
func handleConn(conn tpt.CapableConn) error { | ||
str, err := conn.AcceptStream() | ||
if err != nil { | ||
return err | ||
} | ||
data, err := ioutil.ReadAll(str) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Received: %s\n", data) | ||
if _, err := str.Write([]byte(data)); err != nil { | ||
return err | ||
} | ||
return str.Close() | ||
} |