Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect an octet stream in Apache using PHP or Django

I have a webserver which serves the client with an octet stream on port 20000 (it's actually a socket.io server hosted with node.js). This is running on a shared hosting account with a regular Apache server running on port 80 (this cannot be turned off, hence the socket.io server is on port 20000). Due to firewalls and such, I cannot expect the user to be able to connect to port 20000 (or any other than 80). So, how can I serve the client with the octet stream produced by the socket.io server from the Apache server (sort of like a reverse proxy)? Unfortunately I cannot use mod_proxy on my Apache server given restraints of my hosting plan. I was thinking I could do this with a PHP page that opens a socket somehow.

Update: I also have Django for Python 3 installed on my server which could be useful. Please note that the proxy cannot simply request the target page and serve it back to the client since the data has to be transferred in real time.

like image 430
Parker Hoyes Avatar asked May 26 '15 17:05

Parker Hoyes


1 Answers

Re "it cannot simply serve the target page back" ... this is not true because this is all an HTTP proxy is. Most protocol proxies use a socket (versus a file or pipe) and simply copy the data from socket to socket. An HTTP proxy does the same thing except every HTTP request requires a handshake, so the proxy will require a few packets back and forth before reaching the payload. You can create an HTTP GET proxy very easily in django. A POST proxy will need extra footwork.

I am not familiar with Socket.IO, but upon researching ... how does socket.io work? ... it appears it uses only "old" HTTP features and runs everything as REST. The REST is encapsulated as a transport inside of a persistent socket.

If you were looking for an TCP or IP-level proxy within Django, its not going to happen. Your apache server, and then WSGI/CGI/whatever closes the TCP socket off from you. The only way you'll be able to access it is with sufficient permissions to those parts of the server.

Here's what I'd do ... In django make a url pattern that captures the socket.io api, and have it connect to a view that does something like the following (untested pseudo code):

import urllib2, mimetypes
from django.http import HttpResponse

def ForwardToSocketIO(request):
    # Capture the URL pattern
    path = request.get_full_path()

    # Create a URL opener
    response = urllib2.urlopen('http://localhost:20000%s' % path)

    # Capture and return response
    django_response = HttpResponse(response.read())
    django_response['Content-Type'] = 'octet-stream'

    return django_response

Hope this helps. I don't have an octet stream available so apologies for not testing.

like image 189
Chase Adams Avatar answered Oct 13 '22 20:10

Chase Adams