Skip to content

Tunnel Token parsed incorrectly #46

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

Closed
mrchi opened this issue May 17, 2025 · 1 comment
Closed

Tunnel Token parsed incorrectly #46

mrchi opened this issue May 17, 2025 · 1 comment

Comments

@mrchi
Copy link

mrchi commented May 17, 2025

Here's the agent container config screenshot. the tunnel token is incorrect and it's a json string now.

Image
@ChrispyBacon-dev
Copy link
Owner

ChrispyBacon-dev commented May 17, 2025

Hi @mrchi,
Thanks for the PR and for bringing this to my attention!

I've investigated this on my end. When I test the tunnel token retrieval (even by completely removing the existing tunnel in Cloudflare, deleting the Docker image and state.json, and starting a fresh DockFlare instance), my instance correctly fetches the raw token string directly from the Cloudflare API endpoint (/accounts/.../cfd_tunnel/.../token). This means the cloudflared agent starts correctly with the raw token, not a JSON string.

The evidence you provided in issue #46 (the Cmd array showing a JSON string for the token) is very clear, so something is definitely different in how the token was obtained or processed in that scenario.

A few thoughts and questions to help us narrow this down:

  • API Response Variation: It's possible Cloudflare's API might be returning different formats for this specific token endpoint to different users/regions, or it might have been a temporary behavior.
  • Local Environment: Could there be anything in your specific setup (e.g., custom scripts, environment variables that might alter API interaction, or even a proxy if you're using one) that might cause the API response to be interpreted or wrapped as JSON before DockFlare's get_tunnel_token_via_api function processes it?

for debug:


# Temporary debug code within get_tunnel_token_via_api,

import requests
import json

CF_API_TOKEN = "your_actual_token" # From your .env
CF_ACCOUNT_ID = "your_account_id" # From your .env
TUNNEL_ID_TO_TEST = "an_existing_tunnel_id" # From your CF dash or your app's state

endpoint_url = f"https://api.cloudflare.com/client/v4/accounts/{CF_ACCOUNT_ID}/cfd_tunnel/{TUNNEL_ID_TO_TEST}/token"
headers = {
    "Authorization": f"Bearer {CF_API_TOKEN}",
    "Content-Type": "application/json", # Usually included, though for a GET token, maybe not strictly needed by CF
}

try:
    response = requests.get(endpoint_url, headers=headers, timeout=10)
    response.raise_for_status() # Check for HTTP errors

    print(f"Status Code: {response.status_code}")
    print(f"Content-Type Header: {response.headers.get('Content-Type')}")
    print("Raw Response Text:")
    print(response.text)

    print("\nTrying to parse as JSON:")
    try:
        json_data = response.json()
        print("Successfully parsed as JSON:")
        print(json.dumps(json_data, indent=2))
    except json.JSONDecodeError:
        print("Failed to parse as JSON. Response is likely raw text.")

except requests.exceptions.RequestException as e:
    print(f"Request failed: {e}")
    if e.response is not None:
        print(f"Response content on error: {e.response.text}")

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 a pull request may close this issue.

2 participants