Simulate socket.io temporary disconnection in chrome - google-chrome

I have a simple chat app written in javascript(client and node server) with socket.io. Users are permanently connected to server. I need to test how my application behaves when connection with node-socket.io-server being dropped. I need to block socket.io connection for a few seconds and then allow my app to connect again. I need to do it using the browser, without stopping the server.
I know that chrome developer tools has a future of simulating offline mode but this future does not drops/blocks socket.io connections.
So, how can i drop socket.io connection for a few seconds using chrome browser?

It's possible to simulate disconnection using firewall rules on either the backend or the client side.
On the client side you need to drop all outbound packets to the server, for a few seconds
Example using iptables (will work on linux clients):
SERVER_IP="1.1.1.1"
# Append rule
iptables -A OUTPUT -d $SERVER_IP -j DROP
sleep 5
# Delete rule
iptables -D OUTPUT -d $SERVER_IP -j DROP
On the server side you need to drop all inbound packets from_the specific client, for a few seconds
Example using iptables (will work on linux clients):
CLIENT_PUBLIC_IP="2.2.2.2"
# Append rule
iptables -A INPUT -s $CLIENT_PUBLIC_IP -j DROP
sleep 5
# Delete rule
iptables -D INPUT -s $CLIENT_PUBLIC_IP -j DROP

Related

Accidentally expose port?

I'm a beginner in both docker and mysql, and I use below command to run a mysql container
docker container run --publish 3306:3306 --name mysqlDB -d --env MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
Now it run successfully and in order to grab the generated password, I run below command
docker container logs [containerID]
Within the logs I can find my GENERATED ROOT PASSWORD, but as I try to read the logs I noticed the below log
[System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
May I know what is this means? Is there by any chance I opened a port 33060? And how do I verify it?
This seems to be a MySQL plugin that adds document-oriented APIs to MySQL. Here you can find some more info: https://www.percona.com/blog/2019/01/07/understanding-mysql-x-all-flavors/
That port number seems to be unrelated to your bindings, that's just adefault port number for that plugin.
Also, that port number is not exposed, so, there is nothing to fear, attack surface is still the same.
And if you want to disable that thing, here are the instructions: https://dev.mysql.com/doc/refman/8.0/en/x-plugin-disabling.html (command line option is probably your best bet -- considering docker environment).
To make sure port is not exposed you can run container and do docker ps, you'll see something like this:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43dd96119ded lb_geo-api "/bin/sh -c 'exec sh…" 6 months ago Up 7 days 80/tcp, 0.0.0.0:4203->8080/tcp lb_geo-api_1_a86ebad528fc
Last column -- "PORTS" -- is the list of ports and their bindings on your host:
80/tcp -- port 80 can is exposed from inside container but not mapped to host port, so, nobody from outside can connect there
0.0.0.0:4203->8080/tcp -- port 8080 is exposed and is mapped to port 4203 on all network adapters, and it can be connected from outside
So, if there is no port 33060 in your output, or if it is there but not mapped -- you're safe. In any case only you can map it when you start the container, so, if you did not do that, then it is not mapped.
I was surprised by a MySQL log entry equivalent to yours, #Isaac, which led me to your question, although I'm not working with Docker. Here is what I think I've learned and what I've done.
MySQL's "X plugin" extends MySQL to be able to function as a document store. See MySQL manual section on server plugins, manual section on document store features, and April 2018 document store availability announcement.
By default, for its X plugin features, MySQL listens on port 33060, bound to all IP addresses. See manual section on X plugin options and system variables (indicating default values for "mysqlx_port" and "mysqlx_bind_address"), and X plugin option and variable reference. For its traditional features, MySQL still uses port 3306 by default.
I believe the default X plugin port and network address are what are reflected in the log entry you posted. In particular, I believe the excerpt X Plugin ... bind-address: '::' indicates MySQL's default wildcard ip address binding for X plugin connections.
If you'd like to use the X plugin features but refrain from listening to all IP addresses for them, you can specify the address(es) to which it listens for TCP/IP connections with the mysqlx_bind_address option. The command line format would be
--mysqlx-bind-address=addr
Alternatively, you could set that system variable in a MySQL option file, like this for example:
[mysqld]
<... other mysqld option group settings>
mysqlx_bind_address = 127.0.0.1
The MySQL manual provides helpful general information about specifying options on the command line or in an option file. Here is some information about setting MySQL options in a Docker container, although I have never tried it.
It seems there are distinct settings for the network addresses listened to by MySQL's X-plugin-enabled features and MySQL's traditional features. You set the network address(es) for the traditional features with the bind_address option. So if you want to limit both sets of features to listening for TCP/IP connections from localhost, you could, for example, put this in your MySQL options file, which is what I've just tried in mine:
[mysqld]
bind_address = 127.0.0.1
mysqlx_bind_address = 127.0.0.1
In contrast, it appears, you could set a single system variable -- skip_networking -- to permit only local, non-TCP/IP connections (e.g., Unix sockets, or Windows named pipes or shared memory) for both traditional and X Plugin features.
If you don't want to use the X plugin features at all, you could disable them as #alx suggested.
To verify which network addresses and ports MySQL is listening on, you have a variety of options. In my non-docker Linux environment, I found
netstat -l | grep tcp
and
sudo lsof -i | grep mysql
helpful.
You have published your port. That --publish 3306:3306 actually publishes your container port to host port and now your host port 3306 is occupied by mysql. If you do not want that you can just remove --published 3306:3306 and container port will not be bound to host port.

How to close this ssh tunnel? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I opened a ssh tunnel as described in this post: Zend_Db: How to connect to a MySQL database over SSH tunnel?
But now I don't know what I actually did. Does this command affect anything on the server?
And how do I close this tunnel, because now I can't use my local mysql properly.
I use OSX Lion and the server runs on Ubuntu 11.10.
Assuming you ran this command: ssh -f user#mysql-server.com -L 3306:mysql-server.com:3306 -N as described in the post you linked.
A breakdown of the command:
ssh: that's pretty self-explanatory. Invokes ssh.
-f: (From the man ssh page)
Requests ssh to go to background just before command execution.
This is useful if ssh is going to ask for passwords or
passphrases, but the user wants it in the background.
Essentially, send ssh to background once you've entered any passwords to establish the connection; it gives the shell prompt back to you at localhost rather than logging you in to remote-host.
user#mysql-server.com: the remote server you'd like to log into.
-L 3306:mysql-server.com:3306: This is the interesting bit. -L (from the man ssh page):
[bind_address:]port:host:hostport
Specifies that the given port on the local (client) host is to be
forwarded to the given host and port on the remote side.
So -L 3306:mysql-server.com:3306 binds the local port 3306 to the remote port 3306 on host mysql-server.com.
When you connect to local port 3306, the connection is forwarded over the secure channel to mysql-server.com. The remote host, mysql-server.com then connects to mysql-server.com on port 3306.
-N: don't execute a command. This is useful for "just forwarding ports" (quoting the man page).
Does this command affect anything on the server?
Yes, it establishes a connection between localhost and mysql-server.com on port 3306.
And how do I close this tunnel...
If you've used -f, you'll notice that the ssh process you've opened heads into the background. The nicer method of closing it is to run ps aux | grep 3306, find the pid of the ssh -f ... -L 3306:mysql-server.com:3306 -N, and kill <pid>. (Or maybe kill -9 <pid>; I forget if just kill works). That has the beautiful benefit of not killing all your other ssh connections; if you've got more than one, re-establishing them can be a slight ... pain.
... because now I can't use my local mysql properly.
This is because you've effectively "captured" the local mysql process and forwarded any traffic that attempts to connect to it, off to the remote mysql process. A much nicer solution would be to not use local port 3306 in the port-forward. Use something that's not used, like 33060. (Higher numbers are generally less used; it's pretty common to port-forward a combination like this: "2525->25", "8080->80", "33060->3306" or similar. Makes remembering slightly easier).
So, if you used ssh -f user#mysql-server.com -L 33060:mysql-server.com:3306 -N, you'd then point your Zend connect-to-mysql function to localhost on port 33060, which would connect to mysql-server.com on port 3306. You can obviously still connect to localhost on port 3306, so you can still use the local mysql server.
This will kill all ssh sessions that you have open from the terminal.
sudo killall ssh
Note: adding as answer since comments don't support code blocks.
In my opinion it is better to NOT use -f and instead just background the process as normal with &. That will give you the exact pid you need to kill:
ssh -N -L1234:other:1234 server &
pid=$!
echo "waiting a few seconds to establish tunnel..."
sleep 5
... do yer stuff... launch mysql workbench whatever
echo "killing ssh tunnel $pid"
kill $pid
Or better yet, just create this as a wrapper script:
# backend-tunnel <your cmd line, possibly 'bash'>
ssh -N -L1234:other:1234 server &
pid=$!
echo "waiting a few seconds to establish tunnel..."
sleep 5
"$#"
echo "killing ssh tunnel $pid"
kill $pid
backend-tunnel mysql-workbench
backend-tunnel bash

Can't start MySQL, port 3306 busy

I'm trying to start MySQL from XAMPP (under Windows Vista), but it's saying that's port 3306 is busy.
What would be the best way with check what application is using that port and how to free it?
Just Open task manager and Kill MySql service.
In a command shell, run:
netstat -b -p TCP
or
netstat -an | grep -i listen | grep -E 3306
The first command will output a list that you will need to look through for the line that displays localhost:3306 in the second column. Below this is the application's name using the port.
The secondary command will find find the exact port you are looking for and looks something like this:
<example-name>:user <example-name>$ netstat -an | grep -i listen | grep -E 3306
tcp46 0 0 *.3306 *.* LISTEN
I had the same problem and was stuck on this thing for a day and I couldn't find a perfect answer anywhere.
So I gave it a shot on my own and it worked. This solution is for Windows users. I use Windows 7.
My xampp control panel was displaying an error that port 3306 is busy and in use by some file (name was specified).. say "filename.de".
Now follow the following steps:
press Ctrl+Alt+Del and open Task Manager.
Open the "Processes" list and Check for "show all processes" under the list of processes. If you don't see any such option, don't worry! as sometimes administrator permission is required to show some processes.
Now, when you click on "show all processes" button, all the process will be displayed.
Now, switch to "services" tab in the task manager, and a list of services will be displayed. Now look for a service named "filename.de" <-- filename that was diplayed in the error message in xampp.
When you find that service, 'right-click', and then click in option.. GoTo Process.
You will be redirected to the "Processes" tab with focus on a process corresponding to that service. 'Right-Click' and then click on "end process tree".
Now, the issue has been solved! But might have to do the same thing again when you restart your PC. So it is best to keep your PC in sleep-mode.
Otherwise, to solve this issue permanently, open "msconfig" and uncheck that particular process from the services list and click on apply. and you can restart your system.
I've been having trouble for hours on this error. I was trying to run MySQL from XAMPP after quite some time. It gave errors, similar to yours, it said that port 3306 is in use. If you:
are running on Windows 10
are avoiding to change the port number of MySQL from 3306
can't see any program using the 3306 port from netstat
reinstalling and deleting everything yet it still give the same error
are enabling and using Hyper-V
all of the other solutions didn't work
This is the solution that worked for me:
Go to the most right of the taskbar and right-click the connection icon, click Open Network & Internet settings
Click Change adapter options
Right-click and Disable everything that relates to Hyper-V
I ran MySQL again at now it works.
As Mentioned By #Segun Emmanuel Run the Following Command:
netstat -a -b
You will get a list of Applications that are using different PORTS. Press Ctrl + F and write 3306 to find out which Application is using PORT 3306.
After this, Go to Task Manager via Search Bar or by pressing CTRL + ALT + DEL. Then Under the Background Processes, find out mysqld.exe, right-click on it and you will find an option to close it, namely "End Task".
Then go to your Xampp Control Panel and start the MySQL service.
If mysql is not starting in xampp, it might be a port conflict issue. Mysql run by default on port 3306. you need to check if another application is occupying that port. use following command to check app occupying a port
Linux: netstat -tulpn | grep 3306
Window: netstat -a -b
Mac: lsof -nP -i4TCP:3306
if you find an application occupying that port, stop the application and restart xampp. As an alternative, you can go to php.ini file or click configure in the xampp for mysql and change the mysql port to 3307
In my case it was javaw.exe which was starting on port 3306. This exe does not cause problem if I am logged in using single user in my Windows 10. But if I have multiple logins, it starts this exe for each user and blocks MySQL to start on 3306 port.
Going to task manager and killing this exe for the other user fixed the issue and MySQl could start.
Windows icon -> Open cmd.exe.
Type netstat -a -b.
Find what's using it. In my case it was this:
So, I went to task manager. There were no process called so. The I went to services and disabled these two:
Now everything works fine.
I had this problem (slight variation as I was using MAMP)
I found this problem was due to having MySQL Workbench installed, MySQL Workbench started the mySQL service on bootup which in turn stopped MAMP being able to use the port.
To fix this I had 2 options,
Uninstall MySQL Workbench
Open Task, click services tab, kill the current MySQL service
This then allowed MAMP to use port 3306
This error occurs if you have installed mysql two times. mysql by default uses the port 3306. If you have installed it twice then already there is a mysql at your port number 3306. So you will have to change your port.
If you are using xampp then you can easily change your port. Steps to change port:
Step 1: Open your xampp as administrator.
Step 2: Click on 'Config' at the top right corner of your xampp.
Step 3: Click on 'Service and Port Settings' and after that change the main port of mysql from 3306 to 3307 and the click on save.
Step 4: Then click on 'config' which is in front of mysql and open 'my.ini' file which will be a text file.
Step 5: Now wherever in the text file you see the port number mentioned as 3306 change it to 3307 and then save the file.
After doing this again start your mysql server and it will start running on port 3307.
This worked for me and I hope it will work for anyone else who encounters the same issue.
This command kills the existing mysql process and perhaps one can start it afresh
sudo pkill mysql
It has helped me solve this challenge most of times
1. Turn off the application which is using this port, open terminal and run "lsof -n -P -i | grep 3306" to figure out.
2. Use different ports, right click on the server -> Edit.
For this problem, a simpler way on Windows is:-
Go to Task Manager
Go to Services
There will be a services named MySQl80 right click on it and then select
"Stop"
Try and start the MySQl module on XAMPP server again
If you where not able to find any application or process listening on port 3306, you might need to check your network adapters.
Disable adapters you do not use.
Pay also attention to the Hyper-V generated network adapter, disable it if you don't need it. Sometimes it can reserve some ports and commands like netstat will not be able to find it out
Run XAMPP with Administrator
If you already installed MySQL Service, Uninstall it and install again. If you not already install it install MySQL Service.

Is it possible to list MySQL Server closed connections?

To list active connections we do:
show processlist;
What to do to list old closed connections?
The simple answer is that there's nothing provided by MySQL to list old connections. But you could capture the data flowing between your web front end and your database:
tcpdump -i eth0 -s 0 -w /tmp/mysql.cap tcp port 3306
run your PHP, have it fail, stop the capture and use Wireshark to see exactly what was going on.
I could obtain this nice answer from serverfault.

What's the best way to allow MySQL on one server to listen to requests from two other different servers?

I have my MySQL database server on Server 1. I want to have my Rails apps on two other servers - say A and B to be able to connect to this Server 1. What's the best way to do this?
In the my.cnf file it appears I can use the bind-address to bind to one and only one IP address. I can't specify the IP addresses of both A and B in my.cnf.
On the other hand, if I comment skip-networking, the gates are wide open.
Is there a golden mean? What are you folks doing to allow a DB server to listen to requests from multiple app servers and still stay secure?
If MySQL is running on Linux:
I am very biased towards using iptables (a.k.a. netfilter, the Linux firewall) to control incoming traffic to various ports. It's simple to use and very robust.
iptables -A INPUT -p tcp -s server1address/32 --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp -s server2address/32 --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j DROP
The bind address is the local IP address of the server, not the allowable client addresses. In your situation, you can provide the static address of your server (in place of localhost) or, if your IP might change, just comment it out.
Again, to clarify: the bind-address is the address on which the server listens for client connections (you could have multiple NICs, or multiple IP addresses, etc.). It is also possible to change the port you want mysql to listen to.
You will want to make sure you configure the root password if you haven't already:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('yourpassword');
You would then use other means to restrict access to MySql to something like the local network (i.e. your firewall).
More info about iptables:
The iptables commands above must either be inserted in the existing iptables tables, or else you must delete the existing stuff and start from scratch with the commands above.
Insertion is not hard, but it depends a little bit on the Linux distribution you use, so I'm not sure what to recommend.
To start from scratch, you need to Flush and eXpunge the existing tables first:
iptables -F
iptables -X
Then insert the iptables firewall rules that you need to use, following the model indicated in my previous answer.
Then save the iptables rules. This is again distribution-dependent. On most Red Hat derivatives (Red Hat, Fedora, CentOS), it's enough to run:
service iptables save
Voila, your custom rules are saved. If the iptables service is enabled (check with "chkconfig --list iptables", it must be ":on" on runlevels 3 and 5, depending on your situation, but it's safe to set it ":on" on both 3 and 5 in any case) then your rules will survive the reboot.
At any time, you can check the current running iptables rules. Here's a few commands that do that, with various levels of verbosity:
iptables -L
iptables -L -n
iptables -L -n -v
Without -n, it will try to lookup the domain names and display them instead of IP addresses - this may not be desirable if DNS is not working 100% perfect.
So that's why I almost always use -n.
-v means "verbose", a bit harder to read but it gives more information.
NOTE: If you start from scratch, other services running on that machine may not be protected by iptables anymore. Spend some time and figure out how to insert the MySQL rules in the existing tables. It's better for your system's security.
In addition to getting the bind address right you'll need to open the correct port, create or configure the users and some other details. This explains it pretty clearly.
A DB server will listen to an indefinite number of clients.
Each client Rails app identifies the DB server.
The DB server waits patiently for connections. It has no idea how many clients there are or where the connections come from.
Edit
"how do you securely configure the DB wrt what servers to accept requests from?"
That's what networks, firewalls and routers are for.
That's why the database requires credentials from the Rail apps.