|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/tls" |
| 7 | + "crypto/x509" |
| 8 | + "encoding/pem" |
| 9 | + "io" |
| 10 | + "log" |
| 11 | + "math/big" |
| 12 | + "net" |
| 13 | + "sync" |
| 14 | + |
| 15 | + quic "github.com/lucas-clemente/quic-go" |
| 16 | + "golang.org/x/net/context" |
| 17 | + cli "gopkg.in/urfave/cli.v2" |
| 18 | +) |
| 19 | + |
| 20 | +func server(c *cli.Context) error { |
| 21 | + // generate TLS certificate |
| 22 | + key, err := rsa.GenerateKey(rand.Reader, 1024) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + template := x509.Certificate{SerialNumber: big.NewInt(1)} |
| 27 | + certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}) |
| 32 | + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER}) |
| 33 | + tlsCert, err := tls.X509KeyPair(certPEM, keyPEM) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + config := &tls.Config{ |
| 38 | + Certificates: []tls.Certificate{tlsCert}, |
| 39 | + NextProtos: []string{"quicssh"}, |
| 40 | + } |
| 41 | + |
| 42 | + // configure listener |
| 43 | + listener, err := quic.ListenAddr(c.String("bind"), config, nil) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + defer listener.Close() |
| 48 | + log.Printf("Listening at %q...", c.String("bind")) |
| 49 | + |
| 50 | + ctx := context.Background() |
| 51 | + for { |
| 52 | + log.Printf("Accepting connection...") |
| 53 | + session, err := listener.Accept(ctx) |
| 54 | + if err != nil { |
| 55 | + log.Printf("listener error: %v", err) |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + go serverSessionHandler(ctx, session) |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func serverSessionHandler(ctx context.Context, session quic.Session) { |
| 65 | + log.Printf("hanling session...") |
| 66 | + defer session.Close() |
| 67 | + for { |
| 68 | + stream, err := session.AcceptStream(ctx) |
| 69 | + if err != nil { |
| 70 | + log.Printf("session error: %v", err) |
| 71 | + break |
| 72 | + } |
| 73 | + go serverStreamHandler(ctx, stream) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func serverStreamHandler(ctx context.Context, conn io.ReadWriteCloser) { |
| 78 | + log.Printf("handling stream...") |
| 79 | + defer conn.Close() |
| 80 | + |
| 81 | + rConn, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: net.IP{127, 0, 0, 1}, Port: 22}) |
| 82 | + if err != nil { |
| 83 | + log.Printf("dial error: %v", err) |
| 84 | + return |
| 85 | + } |
| 86 | + defer rConn.Close() |
| 87 | + |
| 88 | + ctx, cancel := context.WithCancel(ctx) |
| 89 | + |
| 90 | + var wg sync.WaitGroup |
| 91 | + wg.Add(2) |
| 92 | + c1 := readAndWrite(ctx, conn, rConn, &wg) |
| 93 | + c2 := readAndWrite(ctx, rConn, conn, &wg) |
| 94 | + select { |
| 95 | + case err = <-c1: |
| 96 | + if err != nil { |
| 97 | + log.Printf("readAndWrite error on c1: %v", err) |
| 98 | + return |
| 99 | + } |
| 100 | + case err = <-c2: |
| 101 | + if err != nil { |
| 102 | + log.Printf("readAndWrite error on c2: %v", err) |
| 103 | + return |
| 104 | + } |
| 105 | + } |
| 106 | + cancel() |
| 107 | + wg.Wait() |
| 108 | + log.Printf("Piping finished") |
| 109 | +} |
| 110 | + |
| 111 | +func netCopy(input io.Reader, output io.Writer) (err error) { |
| 112 | + buf := make([]byte, 8192) |
| 113 | + for { |
| 114 | + count, err := input.Read(buf) |
| 115 | + if err != nil { |
| 116 | + if err == io.EOF && count > 0 { |
| 117 | + output.Write(buf[:count]) |
| 118 | + } |
| 119 | + break |
| 120 | + } |
| 121 | + if count > 0 { |
| 122 | + log.Println(buf, count) |
| 123 | + output.Write(buf[:count]) |
| 124 | + } |
| 125 | + } |
| 126 | + return |
| 127 | +} |
0 commit comments