switchbrew / libnx

Library for Switch Homebrew
https://switchbrew.github.io/libnx/
ISC License
1.27k stars 174 forks source link

web applet fails to connect to local server running on the switch #577

Closed Slluxx closed 3 years ago

Slluxx commented 3 years ago

I am running a simple socket server on a thread and using the main thread to open the web applet.

The applet opens but cant connect to the server on 127.0.0.1, localhost or 192.168.x.x. It shows the error message "The site could not be shown" (its translated from german to english, so it might differ) with the error code "2800-1007".

While the web applet shows an error, i can still connect to and refresh the server running on the switch via its lan address on my pc. So i am 100% sure the server works fine.

The server code can be found here (not running in a thread) The code to open the web applet is this:

void startWebBrowser(){
    Result rc;
    WebCommonConfig conf;
    WebCommonReply out;
    rc = webPageCreate(&conf, "http://127.0.0.1:1234/index.html");
    if (R_FAILED(rc))
        printf("Error starting Browser");
    webConfigSetJsExtension(&conf, true);
    webConfigSetPageCache(&conf, true);
    webConfigSetBootLoadingIcon(&conf, true);
    webConfigSetWhitelist(&conf, ".*");
    rc = webConfigShow(&conf, &out);
    if (R_FAILED(rc))
        printf("Error starting Browser\n");
}

I am not using 90DNS, the browser can connect to other webpages (google etc) and is also able to connect to other servers in the local network. My firmware is 13.0.0, i am using Atmosphere 1.1.0, no other weird internet settings.

Talking to a few people lead me here to open this issue and i hope i can get some help. Really appreciate the work!

yellows8 commented 3 years ago

Use the whitelist string from the example. https://github.com/switchbrew/switch-examples/blob/master/applet/libapplets/web/source/main.c

Did you try to log anything from your server?

Slluxx commented 3 years ago

That whitelist doesn't have any effect either. Yeah, i am logging connections but i just don't get any. connecting from pc does show logs. Its as if i can not establish a connection to it whatsoever.

yellows8 commented 3 years ago

Tried using port 80?

Slluxx commented 3 years ago

no, 1234 but it should not matter, since i can specify a port in the url for the webapplet and thats working just fine. ill try anyway.

Edit: I cant bind port 80. (errno 13) Edit2: I could provide a repository if that makes things easier.

yellows8 commented 3 years ago

Edit2: I could provide a repository if that makes things easier. sure

Slluxx commented 3 years ago

Edit2: I could provide a repository if that makes things easier. sure

https://github.com/Slluxx/serverxbrowser

For testing purposes, i changed the thread to open the webbrowser instead of running the webserver, due to printf's inside a thread are leading to crashes. the current solution is also not great because it should use locked_printf but right now it works.

yellows8 commented 3 years ago

FWIW you can use standard threads - you don't have to directly use libnx threads. Large buffers like char buffer[8192]; should really be moved to non-stack as well.

Slluxx commented 3 years ago

Ah, didnt know. i was just using the threads that were in all the switchbrew examples. is there any disadvantage by using those? And what does moved to non-stack as well. mean "in code"? I understand C/++ mostly by looking at code and people saying things like that usually just pops up a big questionmark above my head. Also, why is it benefitial to move it there?

yellows8 commented 3 years ago

Your process is likely not running while the applet is active. You need to use webSession (see web.h), or if pre-7.0.0 system-version support is really required, use appletSetFocusHandlingMode.

Normally you should use standard threads, unless you really want cooperative thread scheduling. static char buffer[...] would be good enough, would move it from stack to .data (large buffers doesn't really belong on stack). Using fsInitialize/fsExit isn't needed, that's used automatically by libnx for apps.

Slluxx commented 3 years ago

Your process is likely not running while the applet is active.

But why can i still access the webserver on my pc while the applet is active? Shouldn't that mean that the process is in fact running? To serve content, it has to run in a loop for polling, evaluating and responding - which it obviously does. I will write file logs to confirm that. Pretty sure the issue lies somewhere else.

Using fsInitialize/fsExit isn't needed, that's used automatically by libnx for apps.

understood, will be removed.

static char buffer[...] would be good enough

thanks for the clarification!

Edit: Logs show that the current way of threading and opening the applet does not stop the main loop from executing.

yellows8 commented 3 years ago

What exactly is the use-case for this?

The applet opens but cant connect to the server on 127.0.0.1, localhost or {Switch IP addr}. ... with the error code "2800-1007". Likewise here without a server running.

localhost and 127.0.0.1 are explicitly blacklisted by the web-applet itself AFAICT.

Slluxx commented 3 years ago

What exactly is the use-case for this?

i am experimenting with different kind of servers and webpages to create html/css/js homebrew guis, instead of using sdl or something else.

localhost and 127.0.0.1 are explicitly blacklisted by the web-applet itself AFAICT.

due to this info, i tried accessing the server via atmospheres dns mitm service. using my switches lan adress and a random domain name worked perfectly fine. Its not really a solution i want to use but it works to experiment with things.

Is there a way to remove those adresses from the blacklist somehow?

yellows8 commented 3 years ago

You could use webOffline for that, with webSession if needed.

Is there a way to remove those adresses from the blacklist somehow? That requires patching the web-applet codebin.

Slluxx commented 3 years ago

i am trying to use weboffline as suggested but my switch just crashes with a 0x4a2 which doesnt help a lot.

            WebCommonConfig conf;
            WebCommonReply out;
            webOfflineCreate(&conf, WebDocumentKind_OfflineHtmlPage, 0 , "/");
            webConfigSetWhitelist(&conf, "^http*");
            webConfigShow(&conf, &out);

It crashes upon executing webConfigShow. I thought it may be that i have to use /index.html instead of a path? i am also confused about where to put the index file. From the docs i am understanding romfs:/html-document/index.html but not quite sure if thats right.

yellows8 commented 3 years ago

Path is relative to SD /atmosphere/hbl_html/html-document/ - you should probably include your app-name or similar in the path.

Slluxx commented 3 years ago

and no way of having it in romfs of the homebrew?

yellows8 commented 3 years ago

AMS only loads user-specified-content for that romfs from that /atmosphere/hbl_html/ dir.