createWebsocketStream will create a WebSocket connection to stream data over It also handles redirects from Access and will present that flow if the token is not present on the request
(options *StartOptions, log *zerolog.Logger)
| 45 | // It also handles redirects from Access and will present that flow if |
| 46 | // the token is not present on the request |
| 47 | func createWebsocketStream(options *StartOptions, log *zerolog.Logger) (*cfwebsocket.GorillaConn, error) { |
| 48 | req, err := http.NewRequest(http.MethodGet, options.OriginURL, nil) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | req.Header = options.Headers |
| 53 | if options.Host != "" { |
| 54 | req.Host = options.Host |
| 55 | } |
| 56 | |
| 57 | dump, err := httputil.DumpRequest(req, false) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | log.Debug().Msgf("Websocket request: %s", string(dump)) |
| 62 | |
| 63 | dialer := &websocket.Dialer{ |
| 64 | TLSClientConfig: options.TLSClientConfig, |
| 65 | Proxy: http.ProxyFromEnvironment, |
| 66 | } |
| 67 | wsConn, resp, err := clientConnect(req, dialer) |
| 68 | defer closeRespBody(resp) |
| 69 | |
| 70 | if err != nil && IsAccessResponse(resp) { |
| 71 | // Only get Access app info if we know the origin is protected by Access |
| 72 | originReq, err := http.NewRequest(http.MethodGet, options.OriginURL, nil) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | |
| 77 | appInfo, err := token.GetAppInfo(originReq.URL) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | options.AppInfo = appInfo |
| 82 | |
| 83 | wsConn, err = createAccessAuthenticatedStream(options, log) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | } else if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | |
| 91 | return &cfwebsocket.GorillaConn{Conn: wsConn}, nil |
| 92 | } |
| 93 | |
| 94 | var stripWebsocketHeaders = []string{ |
| 95 | "Upgrade", |
no test coverage detected