(ctx context.Context, reader io.ReadCloser, writer io.WriteCloser, tunnels []*latest.PortMapping, stopChan chan struct{}, namespace string, name string, log logpkg.Logger)
| 188 | } |
| 189 | |
| 190 | func StartReverseForward(ctx context.Context, reader io.ReadCloser, writer io.WriteCloser, tunnels []*latest.PortMapping, stopChan chan struct{}, namespace string, name string, log logpkg.Logger) error { |
| 191 | scheme := "TCP" |
| 192 | closeStreams := make([]chan bool, len(tunnels)) |
| 193 | defer func() { |
| 194 | for _, c := range closeStreams { |
| 195 | if c == nil { |
| 196 | continue |
| 197 | } |
| 198 | |
| 199 | close(c) |
| 200 | } |
| 201 | }() |
| 202 | |
| 203 | // Create client |
| 204 | conn, err := util.NewClientConnection(reader, writer) |
| 205 | if err != nil { |
| 206 | return errors.Wrap(err, "new client connection") |
| 207 | } |
| 208 | |
| 209 | client := remote.NewTunnelClient(conn) |
| 210 | logFile := logpkg.GetFileLogger("reverse-portforwarding") |
| 211 | |
| 212 | errorsChan := make(chan error, 2*len(tunnels)+1) |
| 213 | closeStream := make(chan struct{}) |
| 214 | defer close(closeStream) |
| 215 | go func() { |
| 216 | for { |
| 217 | select { |
| 218 | case <-ctx.Done(): |
| 219 | return |
| 220 | case <-closeStream: |
| 221 | return |
| 222 | case <-stopChan: |
| 223 | return |
| 224 | case <-time.After(time.Second * 20): |
| 225 | ctx, cancel := context.WithTimeout(ctx, time.Second*20) |
| 226 | _, err := client.Ping(ctx, &remote.Empty{}) |
| 227 | cancel() |
| 228 | if err != nil { |
| 229 | errorsChan <- errors.Wrap(err, "ping connection") |
| 230 | return |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | }() |
| 235 | |
| 236 | for i, portMapping := range tunnels { |
| 237 | if portMapping.Port == "" { |
| 238 | return fmt.Errorf("local port cannot be undefined") |
| 239 | } |
| 240 | |
| 241 | mappings, err := portforward.ParsePorts([]string{portMapping.Port}) |
| 242 | if err != nil { |
| 243 | return fmt.Errorf("error parsing port %s: %v", portMapping.Port, err) |
| 244 | } |
| 245 | |
| 246 | localPort := mappings[0].Local |
| 247 | remotePort := mappings[0].Remote |
no test coverage detected