File size: 2,670 Bytes
fc115d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * Cloudflare Worker - Binance Testnet Proxy
 * Bypasses geo-restrictions by proxying requests through Cloudflare's edge network
 *
 * Deploy this to workers.cloudflare.com to get a proxy URL
 */

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  // Only allow specific origins (your HuggingFace Space)
  const allowedOrigins = [
    'https://chen4700-drl-trading-bot-dev.hf.space',
    'https://chen4700-drl-trading-bot.hf.space',
    'http://localhost:8501', // For local testing
    'http://127.0.0.1:8501'
  ]

  const origin = request.headers.get('Origin')
  const corsHeaders = {
    'Access-Control-Allow-Origin': origin && allowedOrigins.includes(origin) ? origin : allowedOrigins[0],
    'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
    'Access-Control-Allow-Headers': 'Content-Type, X-MBX-APIKEY, Authorization',
    'Access-Control-Max-Age': '86400',
  }

  // Handle CORS preflight
  if (request.method === 'OPTIONS') {
    return new Response(null, {
      headers: corsHeaders
    })
  }

  try {
    const url = new URL(request.url)

    // Extract the target path from the request
    // Format: https://your-worker.workers.dev/api/v3/time
    // Maps to: https://testnet.binance.vision/api/v3/time

    const path = url.pathname + url.search
    const targetUrl = `https://testnet.binance.vision${path}`

    console.log(`Proxying: ${request.method} ${targetUrl}`)

    // Build headers for Binance API
    const headers = new Headers()

    // Copy important headers from original request
    const headersToForward = [
      'X-MBX-APIKEY',
      'Content-Type',
      'User-Agent'
    ]

    for (const header of headersToForward) {
      const value = request.headers.get(header)
      if (value) {
        headers.set(header, value)
      }
    }

    // Make request to Binance testnet
    const response = await fetch(targetUrl, {
      method: request.method,
      headers: headers,
      body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : undefined
    })

    // Clone response and add CORS headers
    const newResponse = new Response(response.body, response)

    // Add CORS headers to response
    Object.keys(corsHeaders).forEach(key => {
      newResponse.headers.set(key, corsHeaders[key])
    })

    return newResponse

  } catch (error) {
    return new Response(JSON.stringify({
      error: 'Proxy error',
      message: error.message,
      stack: error.stack
    }), {
      status: 500,
      headers: {
        ...corsHeaders,
        'Content-Type': 'application/json'
      }
    })
  }
}