i want to mirror/route one port to another port

35 Views Asked by At

I am trying to make localhost:4000 show the exact same content and have the exact same functionality as localhost:8000 but its not working

i tried using python i tried using sockets to bind to localhost:4000 and to listen to localhost:8000

i tried doing this with flask too, i tried alot of things but everytime it worked but sockets wouldnt work and i assume there are alot of other cases it wouldnt work

i cant find the sockets implementation but if you need it lmk

here is the flask one:

from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(path):
    url = 'http://localhost:8000/' + path
    headers = {key: value for (key, value) in request.headers if key != 'Host'}
    req = requests.request(
        method=request.method,
        url=url,
        headers=headers,
        data=request.get_data(),
        cookies=request.cookies,
        allow_redirects=False)

    response = app.response_class(
        response=req.content,
        status=req.status_code,
        headers=req.headers.items()
    )
    return response

if __name__ == '__main__':
    app.run(port=4000)

long story short i tried giving this to chatgpt to make sockets work but it didnt work this only works for static and also it only checks for endpoint and not the subdomain but thats smth i can fix on my own

example sockets code:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import threading

app = Flask(__name__)
socketio = SocketIO(app)

def counter_thread():
    count = 0
    while count <= 100:
        socketio.emit('counter_update', {'count': count}, namespace='/counter')
        count += 1
        socketio.sleep(1)

@app.route('/')
def index():
    return "Hello, World!"

@app.route('/time1')
def time1():
    return render_template('counter.html')

@socketio.on('connect', namespace='/counter')
def counter_connect():
    global count_thread
    if 'count_thread' not in globals():
        count_thread = socketio.start_background_task(target=counter_thread)

if __name__ == '__main__':
    socketio.run(app, port=8000)

counter.html:

<!DOCTYPE html>
<html>
<head>
    <title>Real-time Counter</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.3.1/socket.io.js"></script>
</head>
<body>
    <h1>Real-time Counter</h1>
    <div id="counter"></div>

    <script>
        var socket = io.connect("http://"+ document.domain + ':' + location.port + '/counter');

        socket.on('counter_update', function(data) {
            document.getElementById('counter').innerHTML = 'Counter: ' + data.count;
        });
    </script>
</body>
</html>

this works but im trying to tunnel it to port 4000 so i was like whatever let me try using smth like nginx like chatgpt's recommendations i tried using so many configurations but they all only worked for static as soon as it came to sockets.io it just didnt work it would show the header Real-time counter but it wouldnt show the actual counting and there would be tons of errors in the network tab and console tab in devtools

pls help me i dont mind if its code or an app i just want smth that works

here is an example nginx.conf i used:

events {
    worker_connections  1024;
}

http {
    server {
        listen       8000;
        server_name  localhost;

        location / {
            proxy_pass http://127.0.0.1:4000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
-    }
}

but whatever the solution i need to be able to have smth like this

localhost:8000 -> sub1.localhost:4000 localhost:5000 -> sub2.localhost:4000

bec i need to link 2 ports for smth

i am on windows so if someone has a whole new service or working code or a better nginx.conf please let me know

0

There are 0 best solutions below