Skip to content

Commit 2f2673a

Browse files
function update_cloudflare_config update for correct path via manual rule
1 parent 8d331a5 commit 2f2673a

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

dockflare/app/core/tunnel_manager.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,25 @@ def update_cloudflare_config():
9999
with state_lock:
100100
logging.info("Constructing desired Cloudflare tunnel configuration from managed rules...")
101101
desired_dockflare_rules = []
102-
for hostname, rule_details in managed_rules.items():
102+
for rule_key, rule_details in managed_rules.items():
103103
if rule_details.get("status") == "active":
104104
service_str = rule_details.get("service")
105-
path = rule_details.get("path")
106105

107-
if service_str:
106+
actual_hostname_for_cf = rule_details.get("hostname_for_dns")
107+
actual_path_for_cf = rule_details.get("path")
108+
109+
if not actual_hostname_for_cf:
110+
parts = rule_key.split('|', 1)
111+
actual_hostname_for_cf = parts[0]
112+
if not actual_path_for_cf and len(parts) > 1 and parts[1]:
113+
actual_path_for_cf = parts[1]
114+
115+
if service_str and actual_hostname_for_cf:
108116
no_tls_verify_flag = rule_details.get("no_tls_verify", False)
109-
rule_config = {"hostname": hostname, "service": service_str}
117+
rule_config = {"hostname": actual_hostname_for_cf, "service": service_str}
110118

111-
if path and path.strip():
112-
processed_path = path.strip()
119+
if actual_path_for_cf and actual_path_for_cf.strip():
120+
processed_path = actual_path_for_cf.strip()
113121
if not processed_path.startswith('/'):
114122
processed_path = '/' + processed_path
115123
if len(processed_path) > 1 and processed_path.endswith('/'):
@@ -120,11 +128,13 @@ def update_cloudflare_config():
120128
(service_str.lower().startswith("http://") or service_str.lower().startswith("https://")):
121129
rule_config["originRequest"] = {"noTLSVerify": True}
122130
elif no_tls_verify_flag:
123-
logging.debug(f"Rule for {hostname} has no_tls_verify=true, but service '{service_str}' is not HTTP/HTTPS. 'noTLSVerify' will be ignored by Cloudflare for this service type.")
131+
logging.debug(f"Rule for {rule_key} has no_tls_verify=true, but service '{service_str}' is not HTTP/HTTPS. 'noTLSVerify' will be ignored by Cloudflare for this service type.")
124132

125133
desired_dockflare_rules.append(rule_config)
126-
else:
127-
logging.warning(f"Rule {hostname} is active but missing 'service'. Skipping.")
134+
elif not service_str:
135+
logging.warning(f"Rule {rule_key} is active but missing 'service'. Skipping.")
136+
elif not actual_hostname_for_cf:
137+
logging.warning(f"Rule {rule_key} is active but could not determine a valid hostname for Cloudflare. Skipping.")
128138

129139
try:
130140
current_api_config_ruleset = cloudflare_api.get_current_cf_config(tunnel_state["id"])
@@ -138,7 +148,6 @@ def update_cloudflare_config():
138148
return False
139149

140150
current_api_ingress_rules = current_api_config_ruleset.get("ingress", [])
141-
142151
preserved_api_rules = []
143152
catch_all_rule_template = {"service": "http_status:404"}
144153

@@ -153,10 +162,18 @@ def update_cloudflare_config():
153162

154163
is_actively_managed_by_dockflare = False
155164
if not is_catch_all :
156-
for df_hostname, df_rule_details in managed_rules.items():
165+
for df_rule_key, df_rule_details in managed_rules.items():
166+
df_hostname_for_cf = df_rule_details.get("hostname_for_dns")
167+
df_path_for_cf = df_rule_details.get("path")
168+
if not df_hostname_for_cf:
169+
parts = df_rule_key.split('|',1)
170+
df_hostname_for_cf = parts[0]
171+
if not df_path_for_cf and len(parts) > 1 and parts[1]:
172+
df_path_for_cf = parts[1]
173+
157174
if df_rule_details.get("status") == "active" and \
158-
df_hostname == api_hostname and \
159-
(df_rule_details.get("path") or None) == (api_path or None):
175+
df_hostname_for_cf == api_hostname and \
176+
(df_path_for_cf or None) == (api_path or None):
160177
is_actively_managed_by_dockflare = True
161178
break
162179

@@ -172,13 +189,11 @@ def update_cloudflare_config():
172189
logging.info(f"Non-DockFlare managed rule found in API (hostname: {api_hostname}, path: {api_path}, service: {api_service}). It will be removed by authoritative update.")
173190

174191
final_ingress_rules_to_put = list(desired_dockflare_rules)
175-
176192
for p_rule in preserved_api_rules:
177193
is_duplicate = False
178194
p_hostname = p_rule.get("hostname")
179195
p_service = p_rule.get("service")
180196
p_path = p_rule.get("path")
181-
182197
for f_rule in final_ingress_rules_to_put:
183198
if f_rule.get("hostname") == p_hostname and \
184199
f_rule.get("service") == p_service and \
@@ -202,9 +217,7 @@ def rule_to_comparable_dict(rule):
202217
comp_dict = {}
203218
if rule.get("hostname") is not None:
204219
comp_dict["hostname"] = rule.get("hostname")
205-
206220
comp_dict["service"] = rule.get("service")
207-
208221
path_val = rule.get("path")
209222
if path_val and path_val.strip():
210223
processed_path_comp = path_val.strip()
@@ -213,7 +226,6 @@ def rule_to_comparable_dict(rule):
213226
if len(processed_path_comp) > 1 and processed_path_comp.endswith('/'):
214227
processed_path_comp = processed_path_comp.rstrip('/')
215228
comp_dict["path"] = processed_path_comp
216-
217229
origin_request = rule.get("originRequest")
218230
if isinstance(origin_request, dict) and origin_request.get("noTLSVerify") is True:
219231
comp_dict["noTLSVerify"] = True
@@ -279,7 +291,7 @@ def rule_to_comparable_dict(rule):
279291
tunnel_state["error"] = f"Failed update tunnel config: {e}"
280292
return False
281293

282-
return True
294+
return True
283295

284296
def get_cloudflared_container():
285297
if not docker_client:

dockflare/app/templates/status_page.html

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,22 @@ <h2 class="card-title text-2xl sm:text-3xl border-b border-base-300 pb-3 mb-6">
226226
{% endif %}
227227
</td>
228228
<td class="p-3 whitespace-nowrap">
229-
<a href="{{ protocol }}://{{ details.hostname_for_dns }}{% if details.path %}{{ details.path }}{% endif %}" target="_blank" rel="noopener noreferrer" title="Open {{ protocol }}://{{ details.hostname_for_dns }}{% if details.path %}{{ details.path }}{% endif %}" class="link link-hover link-primary text-sm">
230-
{{ details.hostname_for_dns }}
231-
</a>
232-
{% if details.hostname_for_dns and details.hostname_for_dns.startswith('*.') %}
233-
<span class="badge badge-info badge-xs ml-2">wildcard</span>
234-
{% endif %}
235-
</td>
229+
{% set display_hostname = details.hostname_for_dns if details.hostname_for_dns else hostname.split('|')[0] %}
230+
{% set display_path = details.path if details.path else (hostname.split('|')[1] if '|' in hostname else None) %}
231+
<a href="{{ protocol }}://{{ display_hostname }}{% if display_path %}{{ display_path }}{% endif %}" target="_blank" rel="noopener noreferrer" title="Open {{ protocol }}://{{ display_hostname }}{% if display_path %}{{ display_path }}{% endif %}" class="link link-hover link-primary text-sm">
232+
{{ display_hostname }}
233+
</a>
234+
{% if display_hostname and display_hostname.startswith('*.') %}
235+
<span class="badge badge-info badge-xs ml-2">wildcard</span>
236+
{% endif %}
237+
</td>
236238
<td class="p-3 whitespace-nowrap">
237-
{% if details.path and details.path.strip() %}
238-
<code class="text-xs opacity-70">{{ details.path }}</code>
239-
{% else %}
240-
<span class="text-xs opacity-50 italic">(root)</span>
241-
{% endif %}
242-
</td>
239+
{% if display_path and display_path.strip() %}
240+
<code class="text-xs opacity-70">{{ display_path }}</code>
241+
{% else %}
242+
<span class="text-xs opacity-50 italic">(root)</span>
243+
{% endif %}
244+
</td>
243245
<td class="p-3 whitespace-nowrap"><code class="text-xs opacity-80">{{ details.service }}</code></td>
244246
<td class="p-3 whitespace-nowrap">
245247
{% if details.source == 'manual' %}

0 commit comments

Comments
 (0)