| 1 | |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "context" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net" |
| 11 | "net/http" |
| 12 | "strings" |
| 13 | |
| 14 | "github.com/sirupsen/logrus" |
| 15 | "golang.org/x/crypto/ssh" |
| 16 | ) |
| 17 | |
| 18 | const fakeHostKey = `-----BEGIN OPENSSH PRIVATE KEY----- |
| 19 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW |
| 20 | QyNTUxOQAAACAkXGLzDNnY5+xdAgnt8FlBIZtoFOZEdTUkNxkdSM05PgAAAJg9WMAvPVjA |
| 21 | LwAAAAtzc2gtZWQyNTUxOQAAACAkXGLzDNnY5+xdAgnt8FlBIZtoFOZEdTUkNxkdSM05Pg |
| 22 | AAAEAFvLprhpMPdNsxSwo1Cs5VP5joCh9XLicRqKE0JJzdxCRcYvMM2djn7F0CCe3wWUEh |
| 23 | m2gU5kR1NSQ3GR1IzTk+AAAAEmphc29uQFRyaXBlbC5sb2NhbAECAw== |
| 24 | -----END OPENSSH PRIVATE KEY-----` |
| 25 | |
| 26 | type streamLocalDirect struct { |
| 27 | SocketPath string |
| 28 | Reserved0 string |
| 29 | Reserved1 uint32 |
| 30 | } |
| 31 | |
| 32 | var cancel context.CancelFunc |
| 33 | |
| 34 | func startMockServer() { |
| 35 | sshConfig := &ssh.ServerConfig{ |
| 36 | NoClientAuth: true, |
| 37 | } |
| 38 | |
| 39 | key, err := ssh.ParsePrivateKey([]byte(fakeHostKey)) |
| 40 | if err != nil { |
| 41 | logrus.Errorf("Could not parse key: %s", err) |
| 42 | } |
| 43 | sshConfig.AddHostKey(key) |
| 44 | |
| 45 | listener, err := net.Listen("tcp", "localhost:2134") |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | |
| 50 | var ctx context.Context |
| 51 | ctx, cancel = context.WithCancel(context.Background()) |
| 52 | |
| 53 | go func() { |
| 54 | loop: |
| 55 | for { |
| 56 | select { |
| 57 | case <-ctx.Done(): |
| 58 | break loop |
| 59 | default: |
| 60 | |
| 61 | } |
| 62 | conn, err := listener.Accept() |
| 63 | if err != nil { |
| 64 | panic(err) |
| 65 | } |
| 66 | |
| 67 | |
| 68 | _, chans, reqs, err := ssh.NewServerConn(conn, sshConfig) |
| 69 | if err != nil { |
| 70 | panic(err) |
| 71 | } |
| 72 | |
| 73 | go handleRequests(reqs) |
| 74 | |
| 75 | go handleChannels(chans) |
| 76 | } |
| 77 | listener.Close() |
| 78 | }() |
| 79 | } |
| 80 | |
| 81 | func stopMockServer() { |
| 82 | cancel() |
| 83 | } |
| 84 | |
| 85 | func handleRequests(reqs <-chan *ssh.Request) { |
| 86 | for range reqs { |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func handleChannels(chans <-chan ssh.NewChannel) { |
| 91 | directMsg := streamLocalDirect{} |
| 92 | for newChannel := range chans { |
| 93 | if t := newChannel.ChannelType(); t != "[email protected]" { |
| 94 | err := newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t)) |
| 95 | if err != nil { |
| 96 | logrus.Errorf("could not reject channel: %s", err) |
| 97 | } |
| 98 | continue |
| 99 | } |
| 100 | |
| 101 | if err := ssh.Unmarshal(newChannel.ExtraData(), &directMsg); err != nil { |
| 102 | logrus.Errorf("could not direct-streamlocal data: %s", err) |
| 103 | |
| 104 | err = newChannel.Reject(ssh.Prohibited, "invalid format") |
| 105 | if err != nil { |
| 106 | logrus.Errorf("could not reject channel: %s", err) |
| 107 | } |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | channel, _, err := newChannel.Accept() |
| 112 | if err != nil { |
| 113 | logrus.Errorf("could not accept channel: %s", err) |
| 114 | continue |
| 115 | } |
| 116 | |
| 117 | req, err := http.ReadRequest(bufio.NewReader(channel)) |
| 118 | if err != nil { |
| 119 | logrus.Errorf("could not process http request: %s", err) |
| 120 | } |
| 121 | |
| 122 | resp := http.Response{} |
| 123 | resp.Close = true |
| 124 | switch req.RequestURI { |
| 125 | case "/ping": |
| 126 | resp.StatusCode = 200 |
| 127 | resp.ContentLength = 4 |
| 128 | resp.Body = io.NopCloser(strings.NewReader("pong")) |
| 129 | default: |
| 130 | resp.StatusCode = 404 |
| 131 | resp.ContentLength = 0 |
| 132 | } |
| 133 | err = resp.Write(channel) |
| 134 | if err != nil { |
| 135 | logrus.Errorf("could not write response: %s", err) |
| 136 | } |
| 137 | err = channel.CloseWrite() |
| 138 | if err != nil { |
| 139 | logrus.Errorf("could not close write: %s", err) |
| 140 | } |
| 141 | } |
| 142 | } |