Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server does not work from public IP, but localhost or other SDKs are fine #1434

Open
fxfire opened this issue May 28, 2020 · 9 comments
Open

Server does not work from public IP, but localhost or other SDKs are fine #1434

fxfire opened this issue May 28, 2020 · 9 comments

Comments

@fxfire
Copy link

@fxfire fxfire commented May 28, 2020

When running the http_listener

http_listener listener(L"http://*:45050");

listener.support([](web::http::http_request request) {
...
});
try {
	listener.open().then([&listener]() {LOG("Starting to listen"); }).wait();
	}
	catch (web::http::http_exception& e)
	{
		LOG("HTTP Exception: %s, Error: %d, %s\n", e.what(), e.error_code().value(), e.error_code().message().c_str());
	}
	catch (std::exception ex) {
		LOG("http_listener exception: %s",ex.what());
	}
	catch (...)
	{
		LOG("http_listener error");
	}

while (true) {
..
}

This simple code works fine, when I use localhost as url. However, when I try to access the above from the public IP, nothing is received.
The connection goes through a router where the port 45050 is correctly forwarded to the machine (yes, I've tried also others and also made sure the port is not yet used). The forwarding is correctly setup, since it works fine when I use another restapisdk for the same purpose.

The ports are open and in theory it should work...but it does not.
grafik

And yes, I've started the program with explicite admin rights. This is all the info I can get from the connection:
grafik

Anyone has an idea what might be the cause or how to make that thing work?

@fxfire
Copy link
Author

@fxfire fxfire commented May 29, 2020

This is on Windows.
I've also tried the machine's IP instead of the * in the url/address.

@fxfire
Copy link
Author

@fxfire fxfire commented May 29, 2020

I did another test with NetCat, listening on my local machine and sending a request from outside through the public IP to that same port 45050 and it all works fine.
So the problem is 100% coming from cpprestsdk.

Is there anyway to get more verbose debug infos or anything useful out of it?

@abhaypg
Copy link

@abhaypg abhaypg commented May 29, 2020

I am running into similar issue.Host name set to 'LocalHost' works fine. setting host={machinename} starts listener but request doesn't happen and instead get error 'connect ECONNREFUSED'.
If Host is set to an IP address exception occurs while opening listener.
Getting this issue fixed quickly is crucial else it makes all the efforts halted.

@fxfire
Copy link
Author

@fxfire fxfire commented May 30, 2020

	uri_builder uri;
	uri.set_scheme(U("http"));
	uri.set_host(U("+"));
	uri.set_port(U("80"));
	uri.set_path(U("/api/"));

http_listener listener(uri.to_uri());

This, and ONLY this is working. I cannot explain it myself, but that's how it is.
ONLY the strong wildcard works, nothing else. https://docs.microsoft.com/de-de/windows/win32/http/urlprefix-strings
It ONLY works when I use port 80. I can forward from my router from whatever port I want at the exernal IP to that 80 on the machine that runs the server and only then it succeeds.
it ONLY works when there is a path set up. If I choose to just use "/" , it does not work.

@fxfire
Copy link
Author

@fxfire fxfire commented May 30, 2020

Oh and it ofcourse only works on a freshly setup windows server 2019, on my windows 10 I get a nice exception from the HttpAddUrlToUrlGroup call:

HTTP Exception: Error adding url to url group, 
Error: 1214, The format of the specified network name is invalid.

why 🤷 ?

@fxfire
Copy link
Author

@fxfire fxfire commented Jun 1, 2020

So regarding the error on my win10 system, I fixed it by checking
netsh http show iplisten
and then removing the one which was listed there.

@abhaypg
Copy link

@abhaypg abhaypg commented Jun 1, 2020

Thank you fxfire that worked for me as well.

@fxfire
Copy link
Author

@fxfire fxfire commented Jun 1, 2020

After 3 days of wasting my time on something so basic, I finally found a solution in some dark area of the internet:

Since the HTTP Server API internally uses a kernel module http.sys, the C++ REST SDK service program is not the same as the normal TCP service program through the Windows Firewall. It is invalid to directly use the executable file of the program to establish the firewall rules. The correct way is to create a new inbound rule, set the program name to system, and set the local port to the port number you want to listen to.

grafik

This information is so important for Windows users, it should literally be NAILED ontop of the github wiki.

@Tazmaniann1
Copy link

@Tazmaniann1 Tazmaniann1 commented Nov 27, 2020

@fxfire Hi there. I just encounter this problem but this code

web::uri_builder uri;
    uri.set_scheme(U("http"));
    uri.set_host(U("13.59.255.62"));
    uri.set_port(U("80"));
    uri.set_path(U("/api/"));

not working for me, and i create inbound rule but i dont realy understand why you need to change name of the app ?
this method dont working either. Can you explain a bit more clear what you do for make things work ?
I trying to open server on the amazon ec2 btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.