Can I make postgres only listen on a single hostname? - postgresql

Obviously I could listen on a particular IP address, but is it possible to listen on a particular hostname? Specifically, if example1.com and example2.com both point to my server with IP address (say) 12.34.56.78, is it possible to proxy connections to example1.com:5432 to my postgres db, but have connections to example2.com:5432 be rejected?
I have a feeling the hostname is not passed in the initial connection, so it would be impossible to do what can be done with HTTP(S) (e.g. Nginx server block, Caddy, etc.).

No, that is not possible. The database server never sees the host name the client used. Domain Name Service (DNS) resolution of a host name to an IP address has to happen before the server can be contacted.
You will have to use two different IP addresses, for example by defining a second, virtual IP address for the server. You don't need to have a second network card.

Related

distinguish client connections at server from different subnet

I have a socket server listening on specific port in one subnet. Client are present in different subnet.
Each client can make 2 or 3 connections on the same port.
From second parameter(struct sockaddr) of accept API, I can get the source IP address, but that address is translated by Gateway/Router. and I get same IP address for all client connections.
I need to segregate connections from each client but Checking IP address or Mac address give me same thing for all the connections irrespective of client1 and client2 have different IP address.
Any way to handle such thing?

Socket Address in Computer Networks

I read that in the server site – The local (server) socket address is provided by the OS and the remote (client) socket address is the address of the client that makes the connection. The server can find this socket address when a client tries to connect
to the server but in the Client Site – The local (client) socket address is provided by the OS.
What about the remote (server) socket address?
The client does need some way to find the IP address of the server it wants to connect to; the most common way to find the IP address is by starting with a hostname string (e.g. "stackoverflow.com" or whatever) that was either supplied by the user or hard-coded into the program, and using DNS to look up an IP address that corresponds to that hostname string. The usual API for doing a DNS lookup is getaddrinfo(), although older (or lazier) software might call the older gethostbyname() function instead.
Once the client has the IP address of the server it wants to connect to, it also needs to supply a port number; often the port number just a well-known standard port number for a particular type of service (such as 80 for HTTP, or 22 for SSH). If not, then the client will either have to "just know" what port number to use to contact the server, or it will need some other mechanism to figure out which port number to use.

IP Address of servers

So I am kind of new to networking and I'm just interested in the client/server architecture. Let's say you developed a program and the client version ran on a computer and the server version on the server(obviously). In order for the client to connect to the server, it would have to know the ip address of the server (and the port attached so it can be routed to the correct computer/program). Does that mean that the server's ip address can not change? Would you have to specifically tell your ISP to keep the ip address static? Because if both the client and server ip addresses change, then they would have no way to connect and the program wouldn't work... in other words there has to be one constant. When you sign up for a VPS do they give you a static ip address you can bind to from the client version? Thanks!
In order for the client to connect to the server, it would have to know the ip address of the server (and the port attached so it can be routed to the correct computer/program).
Correct.
Does that mean that the server's ip address can not change?
No. In fact, IPs can change at any time. Most servers that are exposed to the public Internet have a static domain name registered in the Internet's DNS system. A client asks DNS to resolve the desired domain name to its current IP address, and then the client can connect to it. But even in private LANs, most routers act as a local DNS server, allowing machines on the same network to discover each other's IP by machine name.
The OS typically handles DNS for you. A client can simply call gethostbyname() or prefferably getaddrinfo(), and the OS will perform DNS queries as needed on the client's behalf and return back the reported IP(s).
Would you have to specifically tell your ISP to keep the ip address static?
You can, but that usually costs extra. And it is not necessary if your server is registered in DNS. And there are free/cheap DNS systems that work with servers that do not have a static IP.
Because if both the client and server ip addresses change, then they would have no way to connect and the program wouldn't work...
That is where DNS comes into play.
in other words there has to be one constant.
A registered domain name that can be resolved by DNS.
When you sign up for a VPS do they give you a static ip address you can bind to from the client version?
It depends on the VPS service, but a more likely scenario would be you are assigned a static sub-domain within the VPS service's main domain. For example, myserver.thevps.com. Or, if you buy your own domain (which can be done very cheaply from any number of providers), you can usually link it to the DNS server operated by your VPS service.

Stunnel - Specify Outbound IP?

Is there any way to specify a particular outbound IP address for stunnel? Right now, it's always using the main IP of the server, but I'd like it to use a specific outbound IP address.
I actually found my own answer on this one - the "local" directive.
local = host
IP of the outgoing interface is used as source for remote connections.
Use this option to bind a static local IP address, instead.

Connecting to Local Web Server when I am Outside my LAN

I have a web server running out of my home. I have assigned it an address such as 192.168.1.123 on port 80.
I understand that this is running on my local network. If I go to another computer on my network and type in the server's ip address, I can see the server.
Is there a way to access this server from outside my LAN?
Yes, you need to set your router to forward connections to port 80 to your internal IP address (192.168.1.123). Look for Port Forwarding on your router admin screen which I would imagine you access by going to http://192.168.1.1
Keep in mind that your ISP may block port 80 completely in which case you can run your web server on a different port (for example por 8180) and have your router forward connections to port 8180 to your internal IP.
To access your server from outside, you just need to point your browser to your external IP address which you can find out by going to http://www.ipchicken.com
Assuming you have a connection to the internet:
https://github.com/progrium/localtunnel
is a quick way to access your local server from the internet. There might be similar implementations in other languages/platforms. This is just the one I know about.
Remember that security issues need to be carefully considered when opening your local network to the world.
If you use a PHP Webserver you can set it this way:
php -S <YourIPAdresse>:<SomePortNumber> <StartPHPpage>
Example: „php -S 192.168.1.123:9000 index.php"