Skip to content

Modify function browser / argument url (options) to allow proxied requests #275

Closed

Description

@benz0li

Is your feature request related to a problem? Please describe.

On code-server it is currently impossible to access HTML help or view Shiny Apps.

This is because the url points to 127.0.0.1 (localhost) instead of the URL at which code-server ist running. Related to Issue #192.

Describe the solution you'd like

At Coder they are working on a HTTP proxy, which will enable port forwarding using path /proxy/<number>.

Is there a way to modify function browser / argument url (options) to use the proxied address instead of 127.0.0.1?

It needs to be clarified how a local (VS Code) installation can be differentiated from a server (code-server) installation.

Describe alternatives you've considered

One could check the port used by the httpd help server or Shiny app, open a new browser window and fill in the URL to be proxied.

Additional context

Maybe it is worth looking on how RStudio handles url differently for Shiny Apps on the Desktop and Server version.

Activity

  1. kevinykuo commented on Jan 30, 2021

    @kevinykuo

    Seems like they’ve got the /proxy/<number> scheme working on the code-server side. Where in the codebase should we need to make the proposed change? This is blocking both help docs and Shiny apps for code-server deployments (in that users would have to manually replace 127.0.0.1 with the URL of the code-server instance).

  2. benz0li commented on Jun 12, 2021

    @benz0li
    ContributorAuthor

    @renkun-ken I'm going to ask the Coder guys about the environment variable VSCODE_HTTP_REFERER (or CODE_SERVER_HTTP_REFERER?) again.

  3. benz0li commented on Jun 14, 2021

    @benz0li
    ContributorAuthor

    @renkun-ken I'm going to ask the Coder guys about the environment variable VSCODE_HTTP_REFERER (or CODE_SERVER_HTTP_REFERER?) again.

    I'm trying to get VSCODE_HTTP_REFERER accepted as environment variable.
    👉 The name of their ancestor; a common name to implement the functionality for Ikuyadeu/vscode-R in all of VS Code derivatives (e.g. Eclipse Theia and code-server) - if their collaborators decide to do so.


    Alternaively HTTP_REFERER; neutral, not mentioning a product name.

  4. benz0li commented on Sep 16, 2021

    @benz0li
    ContributorAuthor

    PR coder/code-server#3621 should be part of the next code-server release.

    The environment variable will be named VSCODE_PROXY_URI.

  5. benz0li commented on Sep 28, 2021

    @benz0li
    ContributorAuthor

    @renkun-ken Applying patch

    diff --git a/R/session/vsc.R b/R/session/vsc.R
    index cc17286..90a8bf7 100644
    --- a/R/session/vsc.R
    +++ b/R/session/vsc.R
    @@ -494,11 +494,23 @@ request_browser <- function(url, title, ..., viewer) {
     
     show_browser <- function(url, title = url, ...,
                              viewer = getOption("vsc.browser", "Active")) {
    +  if (nzchar(Sys.getenv("VSCODE_PROXY_URI"))) {
    +    is.base_path <- grepl("\\:\\d+$", url)
    +    url <- sub("^https?\\://(127\\.0\\.0\\.1|localhost)(\\:)?",
    +      sub("\\{port\\}", "", Sys.getenv("VSCODE_PROXY_URI")), url)
    +    if (is.base_path) {
    +      url <- paste0(url, "/")
    +    }
    +  }
       if (grepl("^https?\\://(127\\.0\\.0\\.1|localhost)(\\:\\d+)?", url)) {
         request_browser(url = url, title = title, ..., viewer = viewer)
       } else if (grepl("^https?\\://", url)) {
         message(
    -      "VSCode WebView only supports showing local http content.\n",
    +      if (nzchar(Sys.getenv("VSCODE_PROXY_URI"))) {
    +        "VSCode is not running on localhost but on a remote server.\n"
    +      } else {
    +        "VSCode WebView only supports showing local http content.\n"
    +      },
           "Opening in external browser..."
         )
         request_browser(url = url, title = title, ..., viewer = FALSE)
    @@ -535,11 +547,23 @@ show_webview <- function(url, title, ..., viewer) {
           stop("Invalid object")
         }
       }
    +  if (nzchar(Sys.getenv("VSCODE_PROXY_URI"))) {
    +    is.base_path <- grepl("\\:\\d+$", url)
    +    url <- sub("^https?\\://(127\\.0\\.0\\.1|localhost)(\\:)?",
    +      sub("\\{port\\}", "", Sys.getenv("VSCODE_PROXY_URI")), url)
    +    if (is.base_path) {
    +      url <- paste0(url, "/")
    +    }
    +  }
       if (grepl("^https?\\://(127\\.0\\.0\\.1|localhost)(\\:\\d+)?", url)) {
         request_browser(url = url, title = title, ..., viewer = viewer)
       } else if (grepl("^https?\\://", url)) {
         message(
    -      "VSCode WebView only supports showing local http content.\n",
    +      if (nzchar(Sys.getenv("VSCODE_PROXY_URI"))) {
    +        "VSCode is not running on localhost but on a remote server.\n"
    +      } else {
    +        "VSCode WebView only supports showing local http content.\n"
    +      },
           "Opening in external browser..."
         )
         request_browser(url = url, title = title, ..., viewer = FALSE)

    will open shiny apps, htmlwidgets, etc. in separate browser windows on code-server if environment variable VSCODE_PROXY_URI is set. Should options(vsc.helpPanel = FALSE), this is also true for the help panel when called from R terminal.

    This does not affect the behaviour of the 'Help viewer' from the R extension itself, though. And 'R Help Panel: Open in external browser' will still use http://localhost:${this.port}/${requestPath}.
    👉 Unfortunately, I can't tell you which modifications have to be applied to the helpViewer scripts.

  6. benz0li commented on Sep 28, 2021

    @benz0li
    ContributorAuthor

    options(vsc.use_httpgd = TRUE) (the SVG plot viewer based on httpgd) works well on code-server.

    'Open in external Browser' currently fails because httpgd.ts does not consider VSCODE_PROXY_URI.
    ℹ️ Even if VSCODE_PROXY_URI could be considered, the request would fail throwing error Uncaught DOMException: The operation is insecure. with my setup, as code-server is served via JupyterHub and neither VSCODE_PROXY_URI nor JUPYTERHUB_API_TOKEN currently being considered when requesting the httpgd API endpoints.


    Using the SVG plot viewer based on httpgd without being able to open it in an external Browser is absolutely OK.
    👉 I don't expect all features of the R extension to properly work with a code-server deployment.

  7. benz0li commented on Sep 28, 2021

    @benz0li
    ContributorAuthor

    @renkun-ken You may test on https://vscode-r.jupyter.b-data.ch, which has environment variable VSCODE_PROXY_URI set and the modified vsc.R in place.
    ℹ️ Sign in with your GitHub account which is whitelisted.

  8. renkun-ken commented on Sep 28, 2021

    @renkun-ken
    Member

    Thanks for digging into this.

    I tried the testing environment and it looks awesome!

    The following features work:

    • httpgd plot viewer: plot(rnorm(100))
    • help viewer: ?get
    • htmlwidget viewer: DT::datatable(mtcars)

    The following do not work at the moment:

    • data viewer (vscode webview resource uri not correctly substituted?): View(mtcars)
    Error: Unable to read file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-grid-community.min.noStyle.js"}' (Error: Unable to resolve non-existing file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-grid-community.min.noStyle.js"}')
        _ files.ts:1017
        doReadFileStream fileService.ts:541
    resourceLoading.ts:82:10
    Error: Unable to read file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-grid.min.css"}' (Error: Unable to resolve non-existing file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-grid.min.css"}')
        _ files.ts:1017
        doReadFileStream fileService.ts:541
    resourceLoading.ts:82:10
    Error: Unable to read file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-theme-balham.min.css"}' (Error: Unable to resolve non-existing file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-theme-balham.min.css"}')
        _ files.ts:1017
        doReadFileStream fileService.ts:541
    resourceLoading.ts:82:10
    Error: Unable to read file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-theme-balham-dark.min.css"}' (Error: Unable to resolve non-existing file 'vscode-remote://vscode-r.jupyter.b-data.ch/vscode-resource?{"requestResourcePath":"/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-theme-balham-dark.min.css"}')
        _ files.ts:1017
        doReadFileStream fileService.ts:541
    resourceLoading.ts:82:10
    Loading failed for the <script> with source “https://vscode-remote+vscode-002dr-002ejupyter-002eb-002ddata-002ech.vscode-resource.vscode-webview.net/opt/code-server/extensions/ikuyadeu.r-2.3.0/dist/resources/ag-grid-community.min.noStyle.js”. index.html:211:1
    Uncaught ReferenceError: agGrid is not defined
        <anonymous> https://vscode-r.jupyter.b-data.ch/user/renkun-ken/code-server/webview/index.html?id=329d9612-2771-457b-8021-b1e95e16086e&swVersion=2&extensionId=Ikuyadeu.r&platform=browser&vscode-resource-base-authority=vscode-resource.vscode-webview.net&parentOrigin=https://vscode-r.jupyter.b-data.ch:278
    
    • All "View in external browser" still uses internal url for browsing.
  9. benz0li commented on Sep 28, 2021

    @benz0li
    ContributorAuthor

    Thanks for the prompt feedback.

    I've just recently updated the R extension from v1.6.8 to v2.3.0. Might be related to #708 as the data viewer worked with v1.6.8.

  10. benz0li commented on Sep 28, 2021

    @benz0li
    ContributorAuthor
  11. benz0li commented on Sep 28, 2021

    @benz0li
    ContributorAuthor

    @renkun-ken Should I prepare a pull request for the changes in vsc.R?

  12. renkun-ken commented on Sep 28, 2021

    @renkun-ken
    Member

    @benz0li Sure, please.

  13. benz0li commented on Sep 29, 2021

    @benz0li
    ContributorAuthor

    @renkun-ken This issue may be closed, as I do not consider possible modifications to the helpViewer scripts and plotViewer scripts to be relevant to this issue.

  14. benz0li commented on Dec 3, 2021

    @benz0li
    ContributorAuthor

    @renkun-ken View(mtcars) works now. Not sure why, though.

    Updated this extension from v2.3.0 to v2.3.3. code-server hasn't been updated in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions