HEX
Server: Apache/2.4.63 (Unix)
System: Linux Synopilou92 4.4.302+ #72806 SMP Mon Jul 21 23:16:00 CST 2025 x86_64
User: pilou92 (1026)
PHP: 8.0.30
Disabled: NONE
Upload Files
File: /volume1/@appstore/VPNCenter/scripts/openvpn.py
#!/usr/bin/env python3

import socket
import sys
import time

# ref: https://www.binarytides.com/receive-full-data-with-the-recv-socket-function-in-python/
def recv_timeout(the_socket, timeout = 2):
    # total data partwise in an array
    total_data = [];
    data = '';
    begin = time.time()

    while 1:
        # if you got some data, then break after timeout
        if total_data and time.time() - begin > timeout:
            break

        # if you got no data at all, wait a little longer, twice the timeout
        elif time.time() - begin > timeout*2:
            break

        # recv something
        try:
            data = the_socket.recv(8192)
            if data:
                total_data.append(data)
                # change the beginning time for measurement
                begin = time.time()
            else:
                # sleep for sometime to indicate a gap
                time.sleep(0.1)
        except:
            pass

    # join all parts to make final string
    return b''.join(total_data)

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server_address = '/var/run/openvpn.sock'

try:
    sock.connect(server_address)
except msg:
    sys.exit(1)
try:
    data = sock.recv(1000)

    message = str(sys.argv[1]) + "\n"
    sock.sendall(message.encode())
    try:
        # make socket non blocking
        sock.setblocking(0)
        data = recv_timeout(sock, 0.5)
    except:
        # fallback
        sock.setblocking(1)
        message = str(sys.argv[1]) + "\n"
        sock.sendall(message.encode())
        data = sock.recv(8192)
    print(data.decode())

finally:
    sock.close()