Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create online vnc client #1320

Closed
hmadadian opened this issue Nov 8, 2019 · 4 comments
Closed

create online vnc client #1320

hmadadian opened this issue Nov 8, 2019 · 4 comments

Comments

@hmadadian
Copy link

hi
I am working on a project that gets host, port and password params from an HTTP request and then runs websocketify command and connects to the mention vnc host and then returns the novnc URL.
But this does not work. Can anyone please help to solve this issue or tell me an alternative way?

@juanjoDiaz
Copy link
Contributor

You are probably looking for what's discussed on #1159

@hmadadian
Copy link
Author

Yes, something like that. But I did it another way.
I use python in my backend so I could use it to establish the proxy.
so first I install websockify with
pip install websockify

Then I import the websockify Lib and use WebSocketProxy class. With using multiprocessing I could handle what I want, and the result is something like this:

import socket, threading
from multiprocessing import Process
from websockify.websocketproxy import WebSocketProxy
import os

# Handle Requests
class ClientThread(threading.Thread):
    def __init__(self,clientAddress,clientsocket):
        threading.Thread.__init__(self)
        self.csocket = clientsocket

    def run(self):
        msg = ''
        while True:
            try:
                data = self.csocket.recv(65536)
                msg = data.decode()
            except Exception as e:
                exit()
            break
        try:
            # Get Requested host and port
            vnchst = msg.split(":")
            # if already port is busy kill it
            getpid = os.popen('lsof -ti tcp:' + str(vnchst[1])).read()
            if (getpid != ""):
                pids = getpid.split("\n")
                for i in pids:
                    if (i != ''):
                        os.popen('kill -9 ' + str(i)).read()
            # Establish the proxy for requested host and port
            p = P(str(vnchst[0]),int(vnchst[1]))
            p.start()
            p.join()
        except Exception as e:
            exit()

# MultiProcess Part
class P(Process):
    def __init__(self , host , port):
        super(P, self).__init__()
        self.host = host
        self.port = port

    def run(self):
        server = WebSocketProxy(target_host=str(self.host) , target_port=int(self.port) , listen_port=int(self.port) , web="/root/noVNC/")
        server.start_server()

LOCALHOST = "localhost"
PORT = 8081
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536)
server.bind((LOCALHOST, PORT))
while True:
    server.listen(1)
    clientsock, clientAddress = server.accept()
    newthread = ClientThread(clientAddress, clientsock)
    newthread.start()

@samhed
Copy link
Member

samhed commented Nov 12, 2019

Closing as duplicate of #1159

@samhed samhed closed this as completed Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@juanjoDiaz @samhed @hmadadian and others