BrowserMobProxy: How to determine IP for proxy server? - java

I have BrowserMobProxy realization in the project. This logic uses the IP address for Proxy connection and test UI web-service (Proxy used for request/response statistic saving). All worked fine before, but we restart docker and the IP address for the proxy was changed. Now I need to found a new IP address for the proxy.
Code where IP address used
public static void startProxyServer(String address) {//address = "172.17.0.2"
if (browserMobProxy.isStarted()) {
browserMobProxy.stop();
}
try {
browserMobProxy.start(9090, Inet4Address.getByName(address)); // {1}
useExclusivePort = browserMobProxy.getPort();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
After docker was restarting the project began fails on the line {1}.
I started the search for new IP on the server. Unfortunately, I can't use ifconfig command from the docker image since this command does not install there. So I determined the address from the server in the following way:
After I use IP 172.17.0.2 the code works well and I hoped I resolved this issue, but as it turned out I lost connection with this proxy - on the UI I got the following result:
So I totally confused:
All works well before the docker image was restarted
Old IP looks like 172.19.0.5 but the code fails with it now.
I "found" new IP 172.17.0.2 and code work with it.
I not sure I determined correct IP since all old IP was started with 172.19.0.{4,5,8}
I have no connection with the new IP
I do not know or I found the correct IP and why it suitable for code but not suitable for connection
The project deploys with Jenkins docker image. Browsers start on the selenium grid
#Question:
How do I need to found the correct IP that I can use for a proxy connection?

there are other commands with which you can find which ip your server is running on. check if you have access to any of the following
first you can try
netstat -an
which will give you all IPs your server is listening to along with what ports, and what IPs are connected on it along with the ports
if this doesn't work try this
https://dev.to/trexinc/netstat-without-netstat-inside-containers-9ak
then try
ip addr
which will give you a similar output as ifconfig
and last you can try with
docker inspect -f '{{ .NetworkSettings.IPAddress }}' containerID
which will give you the network interfaces of your docker

Related

Java ServerSocket binding to port Hamachi

So I've been working on a small application that would allow me to create a server, have the server connect/bind to a hamachi network/port, and allow the client to join the hamachi server, then use the client application to communicate and other such things over the server.
There is no good documentation that I've been able to find in the last 4 hours that specifies how to do this. I have a basic Server application that works fine on the "localhost", but I really want to make it work over hamachi.
code example:
InetAddress addr = InetAddress.getByName("**.***.***.***");
ServerSocket sSocket = new ServerSocket(****, 5, addr);
The error received is:
Error: java.net.BindException: Cannot assign requested address: JVM_Bind
I am using netbeans, and yes, I am 100% positive that the port is NOT in use. Please help!
Just specify null as the third argument to the constructor. That means 'listen at all local IP addresses'.
I suspect the IP address you're trying to bind to isn't local. You can't do that.

Netty server remote connection

Netty server, Fedora. I just can't connect to the server from remote host and no listening socket is displayed via netstat util. However I can establish the connection running client and server on the same machine. That's simply like that:
port = System.getProperty(PORT_PROPERTY);
Preconditions.checkNotNull(port, "Network error, port property is not set");
hostAddress = new InetSocketAddress(Integer.valueOf(port));
...
serverChannel = bootstrap.bind(hostAddress);
I've tried initializing hostAddress with the port only, localhost IP, 0.0.0.0 IP, and IP of my network. Nothing helps. What could be the root of problem?
Here's some suggestions that should help disagnosing the problem:
For clarity (until you resolve this), stick to using
new InetSocketAddress("0.0.0.0", Integer.valueOf(port))
since this will ensure you bind to all interfaces.
Invoke the JVM with -Djava.net.preferIPv4Stack=true to force the JVM into IPV4. I have found it easier to muck with these issues when in IPV4 since is it less complicated than V6.
Get the PID of the JVM and then issue a netstat like this:
sudo netstat -ap --numeric-ports | grep <PID>
This should display all sockets for your JVM instance. (Please post this output if you're still not able to connect remotely. Also post the output of ifconfig)

using loopback for testing environment

I want to run two instance of same server on same physical machine. These two servers will listen to same port, say 12345. I am trying run one server on 127.0.0.1, and the other one on 127.0.0.2. These two servers will basically send and receive messages using same port but running on separate loopback addresses. I do not know if I am on correct path to set this test environment? Somehow, I am not able to setting this testing environment. I am using java to develop the server.
You can bind two servers to the same port if you specify two different interfaces:
ServerSocket s1 = new ServerSocket(port, 500, InetAddress.getByName("127.0.0.1"));
ServerSocket s2 = new ServerSocket(port, 500, InetAddress.getByName("127.0.0.2"));
That's exactly what my fork of NanoHTTPD was intended for:
https://github.com/gitgrimbo/nanohttpd/commit/9535d1b6b4c1bbc927d390327018882d84df959f
Added ability to specify hostname to bind to.
For example:
java NanoHTTPD -d wwwroot1 -h 127.0.0.1
java NanoHTTPD -d wwwroot2 -h 127.0.0.2
java NanoHTTPD -d wwwroot3 -h 127.0.0.3
And hosts file:
127.0.0.1 www1.example.com
127.0.0.2 www2.example.com
127.0.0.3 www3.example.com
Specifically line 225 (in the commit link above):
myServerSocket.bind(new InetSocketAddress(hostname, port));
This code, combined with my instructions above, show how you can run a server on several loopback addresses, all using the same port (80 by default, but could be any of your choice).

tomcat 7 change localhost to ip address/hostname

I have deployed tomcat 7 on unix server. Application is working properly in its internal text mode browser which is elinks.
My server ip is 190.0.0.1 and hostname is test123. In elink i entered the URL http://localhost:9999/Test, then the application is working properly.
But whenever i tried to access the application hitting URL http://190.0.0.1:9999/Test, than it is not working. I checked the logs there is no error message in it.
May be some configuration issue i have to change but dont know which?
Please issue hostname -i in your unix box to get the ip address of your machine. Example:
[ssivan#wsapp403p.prod.ch4 conf]$ hostname -i
10.236.55.42
Then you can try with that ip address. Example:
http://10.236.55.42:9999/Test

SQLException: Communications link failure (Java/mysql)

Can someone explain to me why this line works:
conn = DriverManager.getConnection("jdbc:mysql://myWebsite.com:3306/schemaName?user=userX&password=passwordX");
But this line does not:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?user=userX&password=passwordX");
I get a Communications Link Failure when attempting to access through the localhost (or 127.0.0.1). However, I'm able to access the database via localhost through PHP and the MySQLQuery Browser and MySQL Aministrator.
If needed here's the entire method I'm using:
public Database() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
conn = DriverManager.getConnection("jdbc:mysql://myWebsite.com:3306/schemaName?user=userX&password=passwordX");
// Next line does not work.
// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?user=userX&password=passwordX");
} catch (SQLException ex) {
displaySQLException(ex); // Seperate routine to display errors.
}
}
Thanks for any help,
Richard
It's possible your mysqld is binding specifically to the ethernet interface instead of all interfaces (0.0.0.0) or the localhost interface (127.0.0.1).
On a *nix platform you can check which interface the daemon is listening on with the following command:
$ netstat -ln|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
In second code:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?
user=userX&password=passwordX");
It will try to connect to mysql on machine on localhost (on which the code is running). In your case it might be possible that mysql on your machine or from where you are running the code is not available or stopped or usename/password you are passing are not valid or schemaname does not exist. But on myWebsite.com it is up.
There is nothing wrong in your code. Make sure mySql is installed and running and username/password are valid, a schema with provided schemaname exists on machine on which you run this code with localhost.
This might happen for many reasons, like
MySQL server might be stopped on the target machine
MySQL might be configured not to accept remote connections
The firewall might be blocking remote connections on port 3306
In my case, I faced this error when trying to connect to mysql-server running inside a container on a Ubuntu host VM from the same host.
Example:
If my VM name is abc.company.com, the following jdbc URL would not work:
jdbc:mysql://abc.company.com:3306/dbname
Above jdbc url would work fine from other machines like xyz.company.com but just not abc.company.com.
where as the following jdbc URL would work just fine on abc.company.com machine:
jdbc:mysql://localhost:3306/dbname
which led me to check the /etc/hosts file.
Adding the following line to /etc/hosts fixed this issue:
127.0.1.1 abc.company.com abc
This seems to be an OS bug that requires us to add these on some Ubuntu versions.
Reference: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution
Before trying this, I had tried all other solutions like GRANT ALL.., changing the bind-address line in mysql.cnf.. None of them helped me in this case.

Categories

Resources