Socket.IO connects but is not emitting - amazon-web-services

To prefice this is a MERN socket.io application unfortunately I can't share my source since it's also connected to my aws account.
When on localhost my entire app works perfectly, however when deploying to AWS the back end fails. There are no errors, and when looking at the network it does show up with a successful link to socket.io. I've changed and updated the server for nginx so that it doesn't give off a bad gateway error. However after doing this there is no other error, I just can't register or login, client side validation seems to work however back end validation does not. If you'd like to take a look it's currently on http://3.145.119.85/
I currently connect to socket.io like so
const [socket] = useState(() => io(":8000"));
My server side instantiating
> const express = require("express");
const app = express();
const cors = require("cors");
const fileUpload = require("express-fileupload");
const PORT = process.env.PORT || 8000;
require("./components/mongoose.config");
app.use(cors());
app.use(fileUpload());
app.use(express.json(), express.urlencoded({extended:true}));
const server = app.listen(PORT, () => console.log("listening at port " + PORT));
require("./components/puzzleio.sockets") (server);
Any help would be much appreciated

Ensure that you have changed the host address from localhost to the real aws address. It seemed natural that socket.io should connect to the root from where you serve your content.
Since you are using AWS and after each restart you get different DNS address you have sent to the page where you are initializing the socket.io then correct the req.headers.host (using express-expose).

Related

Cannot reach the localhost of the server when using ssh port forwarding

I am using Flask and Vue to develop a single page app. So far I have been developing and running both Flask and Vue locally on my laptop. I have now moved both to a server and want to run them there long term. For now, I have kept the original "localhost:5000" setting. Both Flask and Vue work and the axios-requests from Vue arrive at the Flask app. However, to make the latter work not only on the server environment but also from my client laptop, I used ssh port forwarding. The actual problem now is: My website is displayed to me in my local browser and i can click-around, but the axios-requests don't work. This is because the requests are trying to reach the localhost of my laptop, but not the server's localhost (when I start my local Flask app, the requests work). I'm sure I'm just missing something important here - I don't have much experience with client-server architectures.
One of my Requests in Vue.js:
methods: {
getProjects() {
const path = 'http://localhost:5000/projects';
axios.get(path)
.then((res) => {
this.projects = res.data;
this.rows = this.projects.length;
this.setColumns();
})
.catch((error) => {
console.error(error);
});
},
...
The Flask app (server-side) is running on:
http://0.0.0.0:5000/
My ssh port forwarding:
ssh -o GatewayPorts=true -L 8081:0.0.0.0:8080 me_the_user#the_server
Any suggestions welcomed.
Ok, I got it working now. The problem was simply that I only forwarded the frontend/Vue port (8080:0.0.0.0:8080), but not the backend/Flask port (5000:0.0.0.0:5000).

EC2 instance refusing connection to localhost from itself

I'm trying to get a Flask app to run on an EC2 instance. I have a few JS functions that send requests to the localhost in the backend to retrieve data from an API.
For instance:
if(topicQueryString != null && topicQueryString != ''){
$.ajax({
url: 'http://0.0.0.0:5000/search/t/'+topicQueryString,
type: 'GET',
dataType: 'json',
success: function(data) { //do something }
})
}
However, when deploying the app on my EC2 instance, these requests fail with ERR_CONNECTION_REFUSED on localhost:5000/search/t/topics
Is there a way to allow the EC2 instance to make requests to itself in this way?
You're trying to connect to 0.0.0.0, not localhost. The address 0.0.0.0 doesn't mean localhost. You should replace that with localhost or 127.0.0.1 (which is what localhost means, most of the time).
EDIT: actually, I'm not even sure that I understood the problem correctly. Is that JS code running in a browser? You want that code to connect to a back-end service, presumably not running on localhost? If so, you should use the address of the back-end service, rather than 0.0.0.0 (which doesn't make sense in any context as a destination address to connect to) or localhost.

Frontend code directly access http instead of https and cause mix content error

As an academic project, we host our web application with Amazon S3 (For Angular) and Apache Server (For Django). We have made both sites https (for both frontend and backend). We can access the backend successfully on our localhost using ng serve. However, for the production site, it always gives us a mixed content error (try to connect HTTP for our backend). But we actually put https in our angular code. Are there any suggestions on that?
Attached is our frontend code
export class AuthenticationService {
private ip: string = 'https://sunrise.99jhs.com';
authenticate(username: string, password: string) {
const headers = new HttpHeaders();
headers.append('Access-Control-Allow-Origin', '*');
return this.http.post<any>(this.ip + '/api-token-auth/', {username, password}, {headers});
}
Attached is error message
Mixed Content: The page at 'https://sunrise.4jhs.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://sunrise.99jhs.com/api-token-auth'. This request has been blocked; the content must be served over HTTPS.
We build the angular code using
ng build --prod
Try not to specificity the protocol at all
private ip: string = '//sunrise.99jhs.com';
The problem is not with the private IP value. It specifies the https protocol.
The problem is with this code:
return this.http.post(this.ip + '/api-token-auth/', {
The URL resulting from that code is the problem. If you notice, this is the URL being referenced in the error message.
Add an https specifier there. That should resolve the issue.

Websocket connection to Rabbitmq over SSL through ELB

I have installed a rabbitmq server on my AWS instance that is behind a load balancer. I am currently able to connect, send and receive messages using js from an http url. However when I attempt the same feat from an https url I cannot successfully maintain a connection.
The following snippet shows the several tacks I have taken so far:
var ws = null;
if (window.location.protocol.indexOf("https") >= 0){
//1 ws = new WebSocket('wss://myelbpublicdns.com:15674/ws');
//2 ws = new WebSocket('wss://myec2publicdns.com:15674/ws');
//3 ws = new WebSocket('wss://myurl.com:15674/ws');
} else {
ws = new WebSocket('ws://' + awshost + ':15674/ws');
}
var client = Stomp.over(ws);
client.connect(username, password, on_connect, on_error, '/');
So the connect via ws: works perfectly fine.
Connect of option 1 to my ELB gets an SSL_ERROR_BAD_CERT_DOMAIN stating that the cert is for myurl.com, ok makes sense
Connect of option 2 to the ec2 instance gets an SSL_ERROR_RX_RECORD_TOO_LONG
Connect of option 3 directly to myurl.com gets a 400 bad request.
port 15674 is open on my ELB with an ssl cert attached.
All ideas on how to get my wss: connection working are appreciated!
Can you check the below and confirm so that I can try to help this one.
Is your certificate generated with AWS ACM?
Have you tried new WebSocket('wss://www.myurl.com:15674/ws');

Connect to dev server in Flask for unittest

I am trying to write unittests for my Flask API endpoints. I want the test cases to connect to the dev server which is running on a different port 5555.
Here is what I am doing in setUp() to make a test_client.
import flask_app
flask_app.app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqldb://root:#localhost/mvp_test_db'
flask_app.app.config['TESTING'] = True
flask_app.app.config['SERVER_NAME'] = '192.168.2.2' //the IP of the dev server
flask_app.app.config['SERVER_PORT'] = 5555
self.app_client = flask_app.app.test_client()
Then when I make a request using app_client like -
r = self.app_client.post('/API/v1/dummy_api', data = {'user_id' : 1})
I get a 404 when I print r and the request never comes to the dev server (no logs printed). I am not able to inspect the URL to which the connection is being attempted above. Any ideas?
It (app.test_client) does not send requests through network interfaces. All requests are simulated. These are processed inside werkzeug routing system. To process "/API/v1/dummy_api" url you need register a view for. If it is registered, connect it in the import section. Application settings and test settings almost always have almost equal settings.