Skip to content

Commit

Permalink
Merge pull request #1 from Eyezuhk/test
Browse files Browse the repository at this point in the history
Version 1.2
  • Loading branch information
Eyezuhk authored Apr 12, 2024
2 parents 39c0ee4 + 9d4d846 commit d76a113
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 229 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Ignore Cython-generated files
*.c
*.cpp
*.so
*.pyd

# Ignore Python cache files
__pycache__/
*.pyc

# Ignore build directories
build/
dist/

# Ignore log files
*.log

# Ignore temporary and backup files
*.tmp
*.bak
*~
.*.swp

#Others
2brain.md
5 changes: 5 additions & 0 deletions 2brain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Testing cpython to improve performance, working for linux, testing compiling with MinGW for windows.
Ferramenta de build do visual studio 2022.
Path: C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64 | C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64

pyinstaller --onefile --icon=FNLocalCloud.ico FNLocal.py
10 changes: 10 additions & 0 deletions Cython_FNCloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3

from setuptools import setup
from Cython.Build import cythonize

setup(
ext_modules=cythonize("FNCloud.pyx")
)

#python3 Cython_FNCloud.py build_ext --inplace
8 changes: 8 additions & 0 deletions Cython_FNLocal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from setuptools import setup
from Cython.Build import cythonize

setup(
ext_modules=cythonize("FNLocal.pyx")
)

#python3 Cython_FNCloud.py build_ext --inplace
142 changes: 6 additions & 136 deletions FNCloud.py
Original file line number Diff line number Diff line change
@@ -1,140 +1,10 @@
#!/usr/bin/env python3

import socket
import logging
import select
import signal
import os
try:
import FNCloud

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Server configuration
SERVER_ADDRESS = '0.0.0.0' # Listens on all interfaces. You can specify if needed.
CLIENT_PORT = 443 # Port for client connections. You can change.
AGENT_PORT = 80 # Port for agent connections. You can change. If you change this, make sure to update the FNLocal file as well.
BUFFER_SIZE = 256 * 1024 # Reduced buffer size for better performance and security
TIMEOUT = 5 # Timeout in seconds for idle connections (5 seconds)

def handle_connection(client_socket, agent_socket):
"""
Forwards data between a client and an agent until the connection is closed or idle.
Args:
client_socket (socket.socket): The socket for the client connection.
agent_socket (socket.socket): The socket for the agent connection.
"""

try:
client_socket.settimeout(TIMEOUT)
agent_socket.settimeout(TIMEOUT)

client_buffer = bytearray(BUFFER_SIZE)
agent_buffer = bytearray(BUFFER_SIZE)

while True:
# Check for incoming data from either the client or agent
readable, _, _ = select.select([client_socket, agent_socket], [], [])

for sock in readable:
try:
if sock is client_socket:
# Receive data from the client into the client buffer
bytes_received = client_socket.recv_into(client_buffer)
if bytes_received == 0:
# If no data, close the connection
client_socket.close()
agent_socket.close()
return

# Forward data from client to agent
agent_socket.sendall(client_buffer[:bytes_received])

else:
# Receive data from the agent into the agent buffer
bytes_received = agent_socket.recv_into(agent_buffer)
if bytes_received == 0:
# If no data, close the connection
client_socket.close()
agent_socket.close()
return

# Forward data from agent to client
client_socket.sendall(agent_buffer[:bytes_received])

except socket.timeout:
# Close idle connections after the timeout
client_socket.close()
agent_socket.close()
logging.info('Connection closed due to inactivity')
return

except Exception as e:
logging.error(f'Error handling connection: {e}')
finally:
# Close the client and agent sockets
client_socket.close()
agent_socket.close()

def signal_handler(signal, frame):
"""
Signal handler to gracefully exit the program.
Args:
signal (int): The signal number.
frame (frame): The current stack frame.
"""
logging.info('Server shutting down.')
os._exit(0)

def main():
"""
Sets up server sockets for clients and agents, listens for connections,
and establishes communication between them using handle_connection().
"""

# Register signal handler for graceful shutdown
signal.signal(signal.SIGINT, signal_handler)

# Create the server socket for the client
client_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client_server_socket.bind((SERVER_ADDRESS, CLIENT_PORT))
client_server_socket.listen()
logging.info(f'Server started on port {CLIENT_PORT} for clients')

# Create the server socket for the agent
agent_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
agent_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
agent_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) # Allow multiple sockets to bind to the same port
agent_server_socket.bind((SERVER_ADDRESS, AGENT_PORT))
agent_server_socket.listen()
logging.info(f'Server started on port {AGENT_PORT} for agents')

try:
while True:
# Accept incoming connections from the client and agent
readable, _, _ = select.select([client_server_socket, agent_server_socket], [], [])

for sock in readable:
if sock is client_server_socket:
# Accept incoming connection from the client
client_socket, _ = client_server_socket.accept()
logging.info(f'Connection received from client')

# Accept incoming connection from the agent
agent_socket, _ = agent_server_socket.accept()
logging.info(f'Agent connected')

# Handle the connection between client and agent
handle_connection(client_socket, agent_socket)

except Exception as e:
logging.error(f'Error in main loop: {e}')
finally:
# Close the server sockets
client_server_socket.close()
agent_server_socket.close()

if __name__ == "__main__":
main()

except KeyboardInterrupt:
print("Program closed by the user.")
sys.exit()
139 changes: 139 additions & 0 deletions FNCloud.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/usr/bin/env python3

import socket
import logging
import select
import signal
import os

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Server configuration
SERVER_ADDRESS = '0.0.0.0' # Listens on all interfaces. You can specify if needed.
CLIENT_PORT = 443 # Port for client connections. You can change.
AGENT_PORT = 80 # Port for agent connections. You can change. If you change this, make sure to update the FNLocal file as well.
BUFFER_SIZE = 256 * 1024 # Reduced buffer size for better performance and security
TIMEOUT = 2 # Timeout in seconds for idle connections (2 seconds)

def handle_connection(client_socket, agent_socket):
"""
Forwards data between a client and an agent until the connection is closed or idle.
Args:
client_socket (socket.socket): The socket for the client connection.
agent_socket (socket.socket): The socket for the agent connection.
"""

try:
client_socket.settimeout(TIMEOUT)
agent_socket.settimeout(TIMEOUT)

client_buffer = bytearray(BUFFER_SIZE)
agent_buffer = bytearray(BUFFER_SIZE)

while True:
# Check for incoming data from either the client or agent
readable, _, _ = select.select([client_socket, agent_socket], [], [])

for sock in readable:
try:
if sock is client_socket:
# Receive data from the client into the client buffer
bytes_received = client_socket.recv_into(client_buffer)
if bytes_received == 0:
# If no data, close the connection
client_socket.close()
agent_socket.close()
return

# Forward data from client to agent
agent_socket.sendall(client_buffer[:bytes_received])

else:
# Receive data from the agent into the agent buffer
bytes_received = agent_socket.recv_into(agent_buffer)
if bytes_received == 0:
# If no data, close the connection
client_socket.close()
agent_socket.close()
return

# Forward data from agent to client
client_socket.sendall(agent_buffer[:bytes_received])

except socket.timeout:
# Close idle connections after the timeout
client_socket.close()
agent_socket.close()
logging.info('Connection closed due to inactivity')
return

except Exception as e:
logging.error(f'Error handling connection: {e}')
finally:
# Close the client and agent sockets
client_socket.close()
agent_socket.close()

def signal_handler(signal, frame):
"""
Signal handler to gracefully exit the program.
Args:
signal (int): The signal number.
frame (frame): The current stack frame.
"""
logging.info('Server shutting down.')
os._exit(0)

def main():
"""
Sets up server sockets for clients and agents, listens for connections,
and establishes communication between them using handle_connection().
"""

# Register signal handler for graceful shutdown
signal.signal(signal.SIGINT, signal_handler)

# Create the server socket for the client
client_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client_server_socket.bind((SERVER_ADDRESS, CLIENT_PORT))
client_server_socket.listen()
logging.info(f'Server started on port {CLIENT_PORT} for clients')

# Create the server socket for the agent
agent_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
agent_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
agent_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) # Allow multiple sockets to bind to the same port
agent_server_socket.bind((SERVER_ADDRESS, AGENT_PORT))
agent_server_socket.listen()
logging.info(f'Server started on port {AGENT_PORT} for agents')

try:
while True:
# Accept incoming connections from the client and agent
readable, _, _ = select.select([client_server_socket, agent_server_socket], [], [])

for sock in readable:
if sock is client_server_socket:
# Accept incoming connection from the client
client_socket, _ = client_server_socket.accept()
logging.info(f'Connection received from client')

# Accept incoming connection from the agent
agent_socket, _ = agent_server_socket.accept()
logging.info(f'Agent connected')

# Handle the connection between client and agent
handle_connection(client_socket, agent_socket)

except Exception as e:
logging.error(f'Error in main loop: {e}')
finally:
# Close the server sockets
client_server_socket.close()
agent_server_socket.close()

main()
Loading

0 comments on commit d76a113

Please sign in to comment.