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

feat: Simple reverse proxy for China users #795

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions glob/manager_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
cached_config = None
js_path = None

reverse_proxies = {
'ghproxy-mirror': 'https://mirror.ghproxy.com/https://github.com/REPO_NAME',
'hf-mirror': 'https://hf-mirror.com/REPO_NAME'
}

comfy_ui_required_revision = 1930
comfy_ui_required_commit_datetime = datetime(2024, 1, 24, 0, 0, 0)

Expand Down Expand Up @@ -84,6 +89,26 @@ def get_installed_packages():
return pip_map


def try_to_use_reverse_proxy(url):
policy = get_config()['reverse_proxy_policy']
if not policy:
return url

if reverse_proxy := reverse_proxies.get(policy, 'both'):
print(f"Reverse proxy is '{policy}'")
repo_name = url.split("/", 3)[-1]
if policy == 'both':
if url.startswith('https://github.com'):
return reverse_proxies['ghproxy-mirror'].replace("REPO_NAME", repo_name)
elif url.startswith('https://huggingface.co'):
return reverse_proxies['hf-mirror'].replace("REPO_NAME", repo_name)
else:
if ((url.startswith('https://github.com') and policy == 'ghproxy-mirror') or
(url.startswith('https://huggingface.co') and policy == 'hf-mirror')):
return reverse_proxy.replace("REPO_NAME", repo_name)
return url


def clear_pip_cache():
global pip_map
pip_map = None
Expand Down Expand Up @@ -201,6 +226,7 @@ def write_config():
"file_logging": get_config()['file_logging'],
'default_ui': get_config()['default_ui'],
'component_policy': get_config()['component_policy'],
'reverse_proxy_policy': get_config()['reverse_proxy_policy'],
'double_click_policy': get_config()['double_click_policy'],
'windows_selector_event_loop_policy': get_config()['windows_selector_event_loop_policy'],
'model_download_by_agent': get_config()['model_download_by_agent'],
Expand Down Expand Up @@ -236,6 +262,7 @@ def read_config():
'file_logging': default_conf['file_logging'].lower() == 'true' if 'file_logging' in default_conf else True,
'default_ui': default_conf['default_ui'] if 'default_ui' in default_conf else 'none',
'component_policy': default_conf['component_policy'] if 'component_policy' in default_conf else 'workflow',
'reverse_proxy_policy': default_conf['reverse_proxy_policy'] if 'reverse_proxy_policy' in default_conf else 'none',
'double_click_policy': default_conf['double_click_policy'] if 'double_click_policy' in default_conf else 'copy-all',
'windows_selector_event_loop_policy': default_conf['windows_selector_event_loop_policy'].lower() == 'true' if 'windows_selector_event_loop_policy' in default_conf else False,
'model_download_by_agent': default_conf['model_download_by_agent'].lower() == 'true' if 'model_download_by_agent' in default_conf else False,
Expand All @@ -254,6 +281,7 @@ def read_config():
'file_logging': True,
'default_ui': 'none',
'component_policy': 'workflow',
'reverse_proxy_policy': 'none',
'double_click_policy': 'copy-all',
'windows_selector_event_loop_policy': False,
'model_download_by_agent': False,
Expand Down Expand Up @@ -519,6 +547,8 @@ def gitclone_install(files, instant_execution=False, msg_prefix=''):

if url.endswith("/"):
url = url[:-1]

url = try_to_use_reverse_proxy(url)
try:
print(f"Download: git clone '{url}'")
repo_name = os.path.splitext(os.path.basename(url))[0]
Expand Down Expand Up @@ -659,6 +689,8 @@ def gitclone_fix(files, instant_execution=False):

if url.endswith("/"):
url = url[:-1]

url = try_to_use_reverse_proxy(url)
try:
repo_name = os.path.splitext(os.path.basename(url))[0]
repo_path = os.path.join(custom_nodes_path, repo_name)
Expand Down
16 changes: 14 additions & 2 deletions glob/manager_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def set_default_ui_mode(mode):
def set_component_policy(mode):
core.get_config()['component_policy'] = mode

def set_reverse_proxy_policy(policy):
core.get_config()['reverse_proxy_policy'] = policy

def set_double_click_policy(mode):
core.get_config()['double_click_policy'] = mode
Expand Down Expand Up @@ -653,7 +655,6 @@ async def save_snapshot(request):
except:
return web.Response(status=400)


def unzip_install(files):
temp_filename = 'manager-temp.zip'
for url in files:
Expand Down Expand Up @@ -778,7 +779,6 @@ def copy_set_active(files, is_disable, js_path_name='.'):
print(f"{action_name} was successful.")
return True


@PromptServer.instance.routes.post("/customnode/install")
async def install_custom_node(request):
if not is_allowed_security_level('middle'):
Expand Down Expand Up @@ -999,6 +999,8 @@ async def install_model(request):
model_url = json_data['url']
if not core.get_config()['model_download_by_agent'] and (
model_url.startswith('https://github.com') or model_url.startswith('https://huggingface.co') or model_url.startswith('https://heibox.uni-heidelberg.de')):
model_url = core.try_to_use_reverse_proxy(model_url)

model_dir = get_model_dir(json_data)
download_url(model_url, model_dir, filename=json_data['filename'])
if model_path.endswith('.zip'):
Expand Down Expand Up @@ -1092,6 +1094,16 @@ async def component_policy(request):

return web.Response(status=200)

@PromptServer.instance.routes.get("/manager/reverse_proxy/policy")
async def reverse_proxy_policy(request):
if "value" in request.rel_url.query:
set_reverse_proxy_policy(request.rel_url.query['value'])
core.write_config()
else:
return web.Response(text=core.get_config()['reverse_proxy_policy'], status=200)

return web.Response(status=200)


@PromptServer.instance.routes.get("/manager/dbl_click/policy")
async def dbl_click_policy(request):
Expand Down
25 changes: 24 additions & 1 deletion js/comfyui-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { OpenArtShareDialog } from "./comfyui-share-openart.js";
import { CustomNodesManager } from "./custom-nodes-manager.js";
import { SnapshotManager } from "./snapshot.js";
import { ModelInstaller } from "./model-downloader.js";
import { manager_instance, setManagerInstance, install_via_git_url, install_pip, rebootAPI, free_models, show_message } from "./common.js";
import { manager_instance, setManagerInstance, install_via_git_url, install_pip, rebootAPI, free_models, show_message, set_reverse_proxy_policy } from "./common.js";
import { ComponentBuilderDialog, load_components, set_component_policy, getPureName } from "./components-manager.js";
import { set_double_click_policy } from "./node_fixer.js";

Expand Down Expand Up @@ -920,6 +920,28 @@ class ManagerMenuDialog extends ComfyDialog {
set_component_policy(event.target.value);
});

// reverse-proxy policy
let reverse_proxy_combo = document.createElement("select");
reverse_proxy_combo.setAttribute("title", "If you are in China, you can use this option to enable reverse-proxy to download custom nodes and models.");
reverse_proxy_combo.className = "cm-menu-combo";
reverse_proxy_combo.appendChild($el('option', { value: 'none', text: 'Reverse Proxy: None' }, []));
reverse_proxy_combo.appendChild($el('option', { value: 'ghproxy-mirror', text: 'Reverse Proxy: GHProxy Mirror to GitHub' }, []));
reverse_proxy_combo.appendChild($el('option', { value: 'hf-mirror', text: 'Reverse Proxy: HF-Mirror to HuggingFace' }, []));
reverse_proxy_combo.appendChild($el('option', { value: 'both', text: 'Reverse Proxy: Both' }, []));

api.fetchApi('/manager/reverse_proxy/policy')
.then(response => response.text())
.then(data => {
reverse_proxy_combo.value = data;
set_reverse_proxy_policy(data);
})

reverse_proxy_combo.addEventListener('change', function (event) {
api.fetchApi(`/manager/reverse_proxy/policy?value=${event.target.value}`);
set_reverse_proxy_policy(event.target.value);
})

// double-click policy
let dbl_click_policy_combo = document.createElement("select");
dbl_click_policy_combo.setAttribute("title", "Sets the behavior when you double-click the title area of a node.");
dbl_click_policy_combo.className = "cm-menu-combo";
Expand Down Expand Up @@ -970,6 +992,7 @@ class ManagerMenuDialog extends ComfyDialog {
default_ui_combo,
share_combo,
component_policy_combo,
reverse_proxy_combo,
dbl_click_policy_combo,
$el("br", {}, []),

Expand Down
11 changes: 11 additions & 0 deletions js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ export async function install_pip(packages) {
}
}

let reverse_proxy_policy = "none"
try {
api.fetchApi('/manager/reverse_proxy/policy')
.then(response => response.text())
.then(data => { reverse_proxy_policy = data; });
}
catch {}
export function set_reverse_proxy_policy(v) {
reverse_proxy_policy = v;
}

export async function install_via_git_url(url, manager_dialog) {
if(!url) {
return;
Expand Down