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

Some Improvements (Outlined Below) #30

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

nvv1d
Copy link

@nvv1d nvv1d commented Apr 25, 2023

UPDATE:
Here's an outline of the changes made in both the config.ini and gfw_resist_tls_proxy.py files:

  1. config.ini file:

    • Updated the Cloudflare_IP section to include a comma-separated list of IP addresses instead of a single IP address.
  2. .py file:

    • Imported the random module to enable random selection of IP addresses.
    • Modified the load_config() function to:
      • Read the Cloudflare_IP section as a comma-separated list of IP addresses and store them in the Cloudflare_IPs global variable.
    • Added a new function get_next_backend_ip() to:
      • Implement a round-robin load balancing algorithm by randomly selecting an IP address from the list and rotating the list.
    • Modified the my_upstream() function to:
      • Call the get_next_backend_ip() function to get the next backend IP address for each request.
      • Updated the my_upstream() function to print the selected backend IP address when it's connected.
    • Updated the main() function to:
      • Print the list of IP addresses in the output message.

These changes allow the proxy server to accept a list of IP addresses and use a round-robin load balancing algorithm to randomly select and rotate IP addresses for each request.
(#12 (comment))

  1. Added support for a configuration file:
    • Imported configparser library to read settings from a configuration file.
    • Created a config.ini file with the necessary settings.
    • Added parse_args() function to handle command-line arguments.
    • Added load_config() function to load settings from the configuration file.
    • Updated the main() function to use the new configuration handling functions.
  2. Added support for command-line arguments:
    • Imported argparse library to handle command-line arguments.
    • Updated the parse_args() function to accept a --config argument for specifying a custom configuration file.
  3. Added command line argument parsing using the argparse module.
  4. Added configuration file support using the configparser module.
  5. Replaced the ThreadedServer class with a ThreadPoolExecutor to handle multiple client connections concurrently.
  6. Moved the send_data_in_fragment function outside of the ThreadedServer class and made it a standalone function.
  7. Removed unnecessary imports and variables.
  8. Added logging support using the logging module.
  9. Refactored the my_upstream and my_downstream functions to use the "with" statement for better resource management.
  10. Changed print statements to logging statements for better readability and maintainability.
  11. Added docstrings to functions for better documentation.

@GFW-knocker
Copy link
Owner

dear nvv1d
i highly appreciate your effort
i made a branch "LoadBalancer" for you
please pull to that.

@GFW-knocker
Copy link
Owner

6 month ago i extensively optimized this code to work in low-ram-vps
i found that ThreadPoolExecutor is much resource hungry ,
even worse when using asyncio
its obvious when +15000 simultaneous connection need to be handle

@byehack
Copy link

byehack commented Apr 26, 2023

Asyncio brings efficient resource usage and better code safety.
Do you need to update codebase to async? maybe I could help.
or if this script is just POW and need to re-work in low-level, let keep this simple without major changes.

@GFW-knocker
Copy link
Owner

@byehack
in theory asyncio is faster
i waste two weeks , finally found that is faster in IO but use more CPU and RAM.

yes we finally need to add into xray-core
so we may switch to c++ or go-lang perhaps

Copy link

@H320 H320 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just correct a mistake

from logging.handlers import TimedRotatingFileHandler

import configparser
import random
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import itertools

global listen_PORT, Cloudflare_IPs, Cloudflare_port, L_fragment, fragment_sleep, my_socket_timeout, first_time_sleep, accept_time_sleep
listen_PORT = int(config.get('settings', 'listen_PORT'))
Cloudflare_IPs = [ip.strip() for ip in config.get('settings', 'Cloudflare_IP').split(',')]
Cloudflare_port = int(config.get('settings', 'Cloudflare_port'))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloudflare_IPs = itertools.cycle(Cloudflare_IPs)

global Cloudflare_IPs
selected_ip = random.choice(Cloudflare_IPs)
Cloudflare_IPs = Cloudflare_IPs[1:] + [selected_ip]
return selected_ip
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def get_next_backend_ip():
global Cloudflare_IPs
return next(Cloudflare_IPs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants