From d462bb5dc0e9e0b65a2110bdddfd95bd5e88e01e Mon Sep 17 00:00:00 2001 From: Ed Asriyan Date: Wed, 25 Dec 2024 15:09:41 -0800 Subject: [PATCH] Generate client configs --- roles/users-configs/tasks/main.yml | 14 +- .../tasks/render_users_configs.yml | 25 ++++ .../templates/client-xray-config.json.j2 | 120 ++++++++++++++++++ .../templates/{config.j2 => hiddify.j2} | 0 roles/users-configs/templates/index.html.j2 | 4 +- .../users-configs/templates/redirect.json.j2 | 3 - .../users-configs/templates/urls-map.json.j2 | 7 + roles/users-configs/vars/main.yml | 6 +- 8 files changed, 160 insertions(+), 19 deletions(-) create mode 100644 roles/users-configs/tasks/render_users_configs.yml create mode 100644 roles/users-configs/templates/client-xray-config.json.j2 rename roles/users-configs/templates/{config.j2 => hiddify.j2} (100%) delete mode 100644 roles/users-configs/templates/redirect.json.j2 create mode 100644 roles/users-configs/templates/urls-map.json.j2 diff --git a/roles/users-configs/tasks/main.yml b/roles/users-configs/tasks/main.yml index 210d565..9f4cc89 100644 --- a/roles/users-configs/tasks/main.yml +++ b/roles/users-configs/tasks/main.yml @@ -40,21 +40,9 @@ dest: "{{ users_configs_static_root_local }}/robots.txt" - name: Render configs - template: - src: config.j2 - dest: "{{ users_configs_static_root_local }}/{{ item.key }}" with_items: "{{ users_configs_users | dict2items }}" loop_control: index_var: loop_index vars: users_configs_user_uuid: "{{ item.key }}" - users_configs_default_server: "{{ users_configs_servers[users_configs_default_server_uuid] }}" - users_configs_default_server_uuid: "{{ item.value.preferred_server_uuid if 'preferred_server_uuid' in item.value else (users_configs_servers | dict2items)[loop_index % (users_configs_servers | length)].key }}" - -- name: Render redirect configs - template: - src: redirect.json.j2 - dest: "{{ users_configs_static_root_local }}/{{ item.key }}.json" - with_items: "{{ users_configs_users | dict2items }}" - vars: - users_configs_user_uuid: "{{ item.key }}" + include_tasks: render_users_configs.yml diff --git a/roles/users-configs/tasks/render_users_configs.yml b/roles/users-configs/tasks/render_users_configs.yml new file mode 100644 index 0000000..080b44f --- /dev/null +++ b/roles/users-configs/tasks/render_users_configs.yml @@ -0,0 +1,25 @@ + +- name: Create user's directory + file: + path: "{{ users_configs_static_root_local }}/{{ users_configs_user_uuid }}" + state: directory + +- name: Render hiddify config + template: + src: hiddify.j2 + dest: "{{ users_configs_static_root_local }}/{{ users_configs_user_uuid }}/{{ user_configs_hiddify_filename }}" + +- name: Render hiddify config (index.html for backward compatibility) + template: + src: hiddify.j2 + dest: "{{ users_configs_static_root_local }}/{{ users_configs_user_uuid }}/index.html" + +- name: Render client configs + template: + src: client-xray-config.json.j2 + dest: "{{ users_configs_static_root_local }}/{{ users_configs_user_uuid }}/{{ user_configs_client_xray_config_filename }}" + +- name: Render urls map + template: + src: urls-map.json.j2 + dest: "{{ users_configs_static_root_local }}/{{ users_configs_user_uuid }}/{{ users_configs_urls_map_filename }}" diff --git a/roles/users-configs/templates/client-xray-config.json.j2 b/roles/users-configs/templates/client-xray-config.json.j2 new file mode 100644 index 0000000..593c2ed --- /dev/null +++ b/roles/users-configs/templates/client-xray-config.json.j2 @@ -0,0 +1,120 @@ +{ + "inbounds": [ + { + "tag": "inbound", + "listen": "0.0.0.0", + "port": 1080, + "protocol": "socks", + "settings": { + "auth": "noauth", + "udp": false + }, + "streamSettings": { + "network": "raw", + "security": "none" + } + } + ], + "outbounds": [ + {% for server_item in users_configs_servers | dict2items %} + {% set users_configs_server_uuid = server_item.key %} + {% set users_configs_server = server_item.value %} + {% set server_index = loop.index %} + + {% for sni, fingerprint in users_configs_server.supported_snis | product(users_configs_server.fingerprints) %} + { + "protocol": "vless", + "sendThrough": "0.0.0.0", + "settings": { + "vnext": [ + { + "address": "{{ users_configs_server.host }}", + "port": {{ users_configs_server.port }}, + "users": [ + { + "id": "{{ users_configs_user_uuid }}", + "encryption": "none", + "flow": "{{ users_configs_server.flow }}", + "level": 0 + } + ] + } + ] + }, + "tag": "outbound-vless-{{ server_index }}-{{ loop.index }}-{{ users_configs_server.name | urlencode }}", + "streamSettings": { + "network": "tcp", + "security": "reality", + "realitySettings": { + "fingerprint": "{{ fingerprint }}", + "serverName": "{{ sni }}", + "publicKey": "{{ users_configs_server.public_key }}", + "spiderX": "/&", + "shortId": "" + } + } + }, + {% endfor %} + {% endfor %} + { + "tag": "outbound-freedom", + "protocol": "freedom" + } + ], + "routing": { + "domainStrategy": "AsIs", + "domainMatcher": "hybrid", + "rules": [ + { + "inboundTag": [ + "inbound" + ], + "outboundTag": "outbound-freedom", + "domain": [ + "geosite:category-ru", + "geosite:microsoft" + ] + }, + { + "inboundTag": [ + "inbound" + ], + "outboundTag": "outbound-freedom", + "ip": [ + "geoip:private", + "geoip:ru" + ] + }, + { + "inboundTag": [ + "inbound" + ], + "balancerTag": "balancer" + } + ], + "balancers": [ + { + "tag": "balancer", + "selector": [ + "outbound-vless" + ], + "strategy": { + "type": "leastLoad", + "settings": {} + } + } + ] + }, + "burstObservatory": { + "subjectSelector":[ + "outbound" + ], + "pingConfig": { + "destination": "https://connectivitycheck.gstatic.com/generate_204", + "connectivity": "", + "interval": "1h", + "sampling": 3, + "timeout": "30s" + } + } +} diff --git a/roles/users-configs/templates/config.j2 b/roles/users-configs/templates/hiddify.j2 similarity index 100% rename from roles/users-configs/templates/config.j2 rename to roles/users-configs/templates/hiddify.j2 diff --git a/roles/users-configs/templates/index.html.j2 b/roles/users-configs/templates/index.html.j2 index ff80537..2b44049 100644 --- a/roles/users-configs/templates/index.html.j2 +++ b/roles/users-configs/templates/index.html.j2 @@ -13,11 +13,11 @@ const hash = window.location.hash; if (hash) { - const url= "/" + hash.slice(1) + ".json"; + const url= "/" + hash.slice(1) + "/{{ users_configs_urls_map_filename }}"; fetch(url) .then(response => response.json()) .then(data => { - const redirectUrl = data["{{ instruction_url_field_name }}"]; + const redirectUrl = data["{{ user_configs_instruction_url_field_name }}"]; if (redirectUrl) { redirect(redirectUrl); } else { diff --git a/roles/users-configs/templates/redirect.json.j2 b/roles/users-configs/templates/redirect.json.j2 deleted file mode 100644 index 1eefa34..0000000 --- a/roles/users-configs/templates/redirect.json.j2 +++ /dev/null @@ -1,3 +0,0 @@ -{ - "{{ instruction_url_field_name }}": "/{{ users_configs_instruction_filename }}#{{ users_configs_user_uuid }}" -} diff --git a/roles/users-configs/templates/urls-map.json.j2 b/roles/users-configs/templates/urls-map.json.j2 new file mode 100644 index 0000000..69787e2 --- /dev/null +++ b/roles/users-configs/templates/urls-map.json.j2 @@ -0,0 +1,7 @@ +{ + {% set instruction_url = '/' + users_configs_instruction_filename + '#' + users_configs_user_uuid %} + "{{ user_configs_instruction_url_field_name }}": "{{ instruction_url }}", + "instruction_url": "{{ instruction_url }}", + "xray_client_config_url": "/{{ users_configs_user_uuid }}/{{ user_configs_client_xray_config_filename }}", + "hiddify_url": "/{{ users_configs_user_uuid }}/{{ user_configs_hiddify_filename }}" +} diff --git a/roles/users-configs/vars/main.yml b/roles/users-configs/vars/main.yml index ed5acd0..f67db43 100644 --- a/roles/users-configs/vars/main.yml +++ b/roles/users-configs/vars/main.yml @@ -1,3 +1,7 @@ -instruction_url_field_name: admin_login_url +user_configs_instruction_url_field_name: admin_login_url user_configs_frontend_html_url: https://github.com/ed-asriyan/xray-server-frontend/releases/latest/download/index.html + +users_configs_urls_map_filename: urls-map.json +user_configs_hiddify_filename: hiddify +user_configs_client_xray_config_filename: client-xray-config.json