rename example to cmd, move to a single .go file

This commit is contained in:
Marten Seemann 2019-02-21 17:04:59 +08:00
parent e4b8bb72f3
commit e996c4ac2a
5 changed files with 69 additions and 58 deletions

View File

@ -2,5 +2,5 @@
Run
```bash
go run example/server/main.go
go run cmd/tlsdiag.go server
```

View File

@ -0,0 +1,33 @@
package main
import (
"fmt"
"os"
"github.com/libp2p/go-libp2p-tls/cmd/cmdimpl"
)
func main() {
if len(os.Args) <= 1 {
fmt.Println("missing argument: client / server")
return
}
role := os.Args[1]
// remove the role argument from os.Args
os.Args = append([]string{os.Args[0]}, os.Args[2:]...)
var err error
switch role {
case "client":
err = cmdimpl.StartClient()
case "server":
err = cmdimpl.StartServer()
default:
fmt.Println("invalid argument. Expected client / server")
return
}
if err != nil {
panic(err)
}
}

View File

@ -1,49 +1,24 @@
package main
package cmdimpl
import (
"context"
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"net"
"time"
ic "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
libp2ptls "github.com/libp2p/go-libp2p-tls"
)
func main() {
if err := startClient(); err != nil {
panic(err)
}
}
func startClient() error {
func StartClient() error {
port := flag.Int("p", 5533, "port")
peerIDString := flag.String("id", "", "peer ID")
keyType := flag.String("key", "ecdsa", "rsa, ecdsa, ed25519 or secp256k1")
flag.Parse()
var priv ic.PrivKey
var err error
switch *keyType {
case "rsa":
fmt.Printf("Generated new peer with an RSA key.")
priv, _, err = ic.GenerateRSAKeyPair(2048, rand.Reader)
case "ecdsa":
fmt.Printf("Generated new peer with an ECDSA key.")
priv, _, err = ic.GenerateECDSAKeyPair(rand.Reader)
case "ed25519":
fmt.Printf("Generated new peer with an Ed25519 key.")
priv, _, err = ic.GenerateEd25519Key(rand.Reader)
case "secp256k1":
fmt.Printf("Generated new peer with an Secp256k1 key.")
priv, _, err = ic.GenerateSecp256k1Key(rand.Reader)
default:
return fmt.Errorf("unknown key type: %s", *keyType)
}
priv, err := generateKey(*keyType)
if err != nil {
return err
}

View File

@ -0,0 +1,28 @@
package cmdimpl
import (
"crypto/rand"
"fmt"
ic "github.com/libp2p/go-libp2p-crypto"
)
func generateKey(keyType string) (priv ic.PrivKey, err error) {
switch keyType {
case "rsa":
fmt.Printf("Generated new peer with an RSA key.")
priv, _, err = ic.GenerateRSAKeyPair(2048, rand.Reader)
case "ecdsa":
fmt.Printf("Generated new peer with an ECDSA key.")
priv, _, err = ic.GenerateECDSAKeyPair(rand.Reader)
case "ed25519":
fmt.Printf("Generated new peer with an Ed25519 key.")
priv, _, err = ic.GenerateEd25519Key(rand.Reader)
case "secp256k1":
fmt.Printf("Generated new peer with an Secp256k1 key.")
priv, _, err = ic.GenerateSecp256k1Key(rand.Reader)
default:
return nil, fmt.Errorf("unknown key type: %s", keyType)
}
return
}

View File

@ -1,47 +1,22 @@
package main
package cmdimpl
import (
"context"
"crypto/rand"
"flag"
"fmt"
"net"
"time"
ic "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
libp2ptls "github.com/libp2p/go-libp2p-tls"
)
func main() {
if err := startServer(); err != nil {
panic(err)
}
}
func startServer() error {
func StartServer() error {
port := flag.Int("p", 5533, "port")
keyType := flag.String("key", "ecdsa", "rsa, ecdsa, ed25519 or secp256k1")
flag.Parse()
var priv ic.PrivKey
var err error
switch *keyType {
case "rsa":
fmt.Printf("Generated new peer with an RSA key.")
priv, _, err = ic.GenerateRSAKeyPair(2048, rand.Reader)
case "ecdsa":
fmt.Printf("Generated new peer with an ECDSA key.")
priv, _, err = ic.GenerateECDSAKeyPair(rand.Reader)
case "ed25519":
fmt.Printf("Generated new peer with an Ed25519 key.")
priv, _, err = ic.GenerateEd25519Key(rand.Reader)
case "secp256k1":
fmt.Printf("Generated new peer with an Secp256k1 key.")
priv, _, err = ic.GenerateSecp256k1Key(rand.Reader)
default:
return fmt.Errorf("unknown key type: %s", *keyType)
}
priv, err := generateKey(*keyType)
if err != nil {
return err
}
@ -62,7 +37,7 @@ func startServer() error {
}
fmt.Printf("Listening for new connections on %s\n", ln.Addr())
fmt.Printf("Now run the following command in a separate terminal:\n")
fmt.Printf("\tgo run example/client/main.go -p %d -id %s\n", *port, id.Pretty())
fmt.Printf("\tgo run cmd/tlsdiag.go client -p %d -id %s\n", *port, id.Pretty())
for {
conn, err := ln.Accept()