echo.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package main
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/tls"
  6. "crypto/x509"
  7. "encoding/pem"
  8. "fmt"
  9. "io"
  10. "log"
  11. "math/big"
  12. quic "github.com/lucas-clemente/quic-go"
  13. )
  14. const addr = "localhost:4242"
  15. const message = "foobar"
  16. // We start a server echoing data on the first stream the client opens,
  17. // then connect with a client, send the message, and wait for its receipt.
  18. func main() {
  19. go func() { log.Fatal(echoServer()) }()
  20. err := clientMain()
  21. if err != nil {
  22. panic(err)
  23. }
  24. }
  25. // Start a server that echos all data on the first stream opened by the client
  26. func echoServer() error {
  27. listener, err := quic.ListenAddr(addr, generateTLSConfig(), nil)
  28. if err != nil {
  29. return err
  30. }
  31. sess, err := listener.Accept()
  32. if err != nil {
  33. return err
  34. }
  35. stream, err := sess.AcceptStream()
  36. if err != nil {
  37. panic(err)
  38. }
  39. // Echo through the loggingWriter
  40. _, err = io.Copy(loggingWriter{stream}, stream)
  41. return err
  42. }
  43. func clientMain() error {
  44. session, err := quic.DialAddr(addr, &tls.Config{InsecureSkipVerify: true}, nil)
  45. if err != nil {
  46. return err
  47. }
  48. stream, err := session.OpenStreamSync()
  49. if err != nil {
  50. return err
  51. }
  52. fmt.Printf("Client: Sending '%s'\n", message)
  53. _, err = stream.Write([]byte(message))
  54. if err != nil {
  55. return err
  56. }
  57. buf := make([]byte, len(message))
  58. _, err = io.ReadFull(stream, buf)
  59. if err != nil {
  60. return err
  61. }
  62. fmt.Printf("Client: Got '%s'\n", buf)
  63. return nil
  64. }
  65. // A wrapper for io.Writer that also logs the message.
  66. type loggingWriter struct{ io.Writer }
  67. func (w loggingWriter) Write(b []byte) (int, error) {
  68. fmt.Printf("Server: Got '%s'\n", string(b))
  69. return w.Writer.Write(b)
  70. }
  71. // Setup a bare-bones TLS config for the server
  72. func generateTLSConfig() *tls.Config {
  73. key, err := rsa.GenerateKey(rand.Reader, 1024)
  74. if err != nil {
  75. panic(err)
  76. }
  77. template := x509.Certificate{SerialNumber: big.NewInt(1)}
  78. certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
  79. if err != nil {
  80. panic(err)
  81. }
  82. keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
  83. certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
  84. tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
  85. if err != nil {
  86. panic(err)
  87. }
  88. return &tls.Config{Certificates: []tls.Certificate{tlsCert}}
  89. }