|
| 1 | +package redirclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "os/exec" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/deckhouse/virtualization/api/client/generated/clientset/versioned/typed/core/v1alpha2" |
| 12 | +) |
| 13 | + |
| 14 | +const usbRedirectClient string = "usbredirect" |
| 15 | + |
| 16 | +type ClientConnectFn func(ctx context.Context, device, address string) error |
| 17 | + |
| 18 | +type Client struct { |
| 19 | + // To connect local USB device buffer to the remote VM using the websocket. |
| 20 | + inputReader *io.PipeReader |
| 21 | + inputWriter *io.PipeWriter |
| 22 | + outputReader *io.PipeReader |
| 23 | + outputWriter *io.PipeWriter |
| 24 | + |
| 25 | + listener *net.TCPListener |
| 26 | + |
| 27 | + // channels |
| 28 | + done chan struct{} |
| 29 | + stream chan error |
| 30 | + local chan error |
| 31 | + remote chan error |
| 32 | + |
| 33 | + ctx context.Context |
| 34 | + |
| 35 | + ClientConnect ClientConnectFn |
| 36 | +} |
| 37 | + |
| 38 | +func NewUSBRedirClient(ctx context.Context, address string, stream v1alpha2.StreamInterface) (*Client, error) { |
| 39 | + inReader, inWriter := io.Pipe() |
| 40 | + outReader, outWriter := io.Pipe() |
| 41 | + k := &Client{ |
| 42 | + ctx: ctx, |
| 43 | + inputReader: inReader, |
| 44 | + inputWriter: inWriter, |
| 45 | + outputReader: outReader, |
| 46 | + outputWriter: outWriter, |
| 47 | + ClientConnect: clientConnect, |
| 48 | + } |
| 49 | + |
| 50 | + // Create local TCP server for usbredir client to connect |
| 51 | + if err := k.withLocalTCPClient(address); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + // Start stream with remote usbredir endpoint |
| 56 | + k.withRemoteVMIStream(stream) |
| 57 | + |
| 58 | + // Connects data from local usbredir data to remote usbredir endpoint |
| 59 | + k.proxyUSBRedir() |
| 60 | + |
| 61 | + return k, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (k *Client) withRemoteVMIStream(usbredirStream v1alpha2.StreamInterface) { |
| 65 | + k.stream = make(chan error) |
| 66 | + |
| 67 | + go func() { |
| 68 | + defer k.outputWriter.Close() |
| 69 | + select { |
| 70 | + case k.stream <- usbredirStream.Stream( |
| 71 | + v1alpha2.StreamOptions{ |
| 72 | + In: k.inputReader, |
| 73 | + Out: k.outputWriter, |
| 74 | + }, |
| 75 | + ): |
| 76 | + case <-k.ctx.Done(): |
| 77 | + } |
| 78 | + }() |
| 79 | +} |
| 80 | + |
| 81 | +func (k *Client) withLocalTCPClient(address string) error { |
| 82 | + lnAddr, err := net.ResolveTCPAddr("tcp", address) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("Can't resolve the address: %s", err.Error()) |
| 85 | + } |
| 86 | + |
| 87 | + // The local tcp server is used to proxy between remote websocket and local USB |
| 88 | + k.listener, err = net.ListenTCP("tcp", lnAddr) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("Can't listen: %s", err.Error()) |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func (k *Client) proxyUSBRedir() { |
| 97 | + // forward data to/from websocket after usbredir client connects. |
| 98 | + k.done = make(chan struct{}, 1) |
| 99 | + k.remote = make(chan error) |
| 100 | + go func() { |
| 101 | + defer k.inputWriter.Close() |
| 102 | + start := time.Now() |
| 103 | + |
| 104 | + usbredirConn, err := k.listener.Accept() |
| 105 | + if err != nil { |
| 106 | + fmt.Printf("Failed to accept connection: %s\n", err.Error()) |
| 107 | + k.remote <- err |
| 108 | + return |
| 109 | + } |
| 110 | + defer usbredirConn.Close() |
| 111 | + |
| 112 | + fmt.Printf("Connected to %s at %v\n", usbRedirectClient, time.Now().Sub(start)) |
| 113 | + |
| 114 | + stream := make(chan error) |
| 115 | + // write to local usbredir from pipeOutReader |
| 116 | + go func() { |
| 117 | + _, err := io.Copy(usbredirConn, k.outputReader) |
| 118 | + stream <- err |
| 119 | + }() |
| 120 | + |
| 121 | + // read from local usbredir towards pipeInWriter |
| 122 | + go func() { |
| 123 | + _, err := io.Copy(k.inputWriter, usbredirConn) |
| 124 | + stream <- err |
| 125 | + }() |
| 126 | + |
| 127 | + select { |
| 128 | + case <-k.done: // Wait for local usbredir to complete |
| 129 | + case err = <-stream: // Wait for remote connection to close |
| 130 | + if err == nil { |
| 131 | + // Remote connection closed, report this as error |
| 132 | + err = fmt.Errorf("Remote connection has closed.") |
| 133 | + } |
| 134 | + // Wait for local usbredir to complete |
| 135 | + k.remote <- err |
| 136 | + case <-k.ctx.Done(): |
| 137 | + } |
| 138 | + }() |
| 139 | +} |
| 140 | + |
| 141 | +func clientConnect(ctx context.Context, device, address string) error { |
| 142 | + bin := usbRedirectClient |
| 143 | + args := []string{} |
| 144 | + args = append(args, "--device", device, "--to", address) |
| 145 | + |
| 146 | + fmt.Printf("port_arg: '%s'\n", address) |
| 147 | + fmt.Printf("args: '%v'\n", args) |
| 148 | + fmt.Printf("Executing commandline: '%s %v'\n", bin, args) |
| 149 | + |
| 150 | + command := exec.CommandContext(ctx, bin, args...) |
| 151 | + output, err := command.CombinedOutput() |
| 152 | + |
| 153 | + fmt.Printf("%v output: %v", bin, string(output)) |
| 154 | + |
| 155 | + return err |
| 156 | +} |
| 157 | + |
| 158 | +func (k *Client) Redirect(device string) error { |
| 159 | + // execute local usbredir binary |
| 160 | + address := k.listener.Addr().String() |
| 161 | + k.local = make(chan error) |
| 162 | + go func() { |
| 163 | + defer close(k.done) |
| 164 | + k.local <- k.ClientConnect(k.ctx, device, address) |
| 165 | + }() |
| 166 | + |
| 167 | + var err error |
| 168 | + select { |
| 169 | + case err = <-k.stream: |
| 170 | + case err = <-k.local: |
| 171 | + case err = <-k.remote: |
| 172 | + case <-k.ctx.Done(): |
| 173 | + err = k.ctx.Err() |
| 174 | + |
| 175 | + } |
| 176 | + return err |
| 177 | +} |
0 commit comments