File size: 2,577 Bytes
42171e6
4f8e602
 
 
 
 
9a1aec1
b0ba08d
4f8e602
9a1aec1
 
d960654
b0ba08d
 
 
9a1aec1
b0ba08d
9a1aec1
 
 
 
 
42171e6
dfe1108
9a1aec1
b0ba08d
9a1aec1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8e602
9a1aec1
4f8e602
 
bf75b63
 
 
4f8e602
 
 
 
 
 
bf75b63
9a1aec1
bf75b63
4f8e602
9a1aec1
4f8e602
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
import os, requests, json, subprocess, time
from flask import Flask, request, jsonify

app = Flask(__name__)
WORKER_URL = os.getenv("WORKER_URL")
WORKER_SECRET = os.getenv("WORKER_SECRET")
# هام جداً: تأكد من وضع بيانات الهوية في الـ Secrets باسم NODE_DATA
NODE_DATA_STR = os.getenv("NODE_DATA")

def setup_and_force_broadcast():
    print("--- 🛠️ Attempting to Bypass Hugging Face Restrictions ---")
    try:
        base_path = os.path.expanduser("~/gaianet")
        os.makedirs(base_path, exist_ok=True)
        
        # 1. تثبيت الهوية من الـ Secrets لضمان عدم تغير الـ ID
        if NODE_DATA_STR:
            with open(os.path.join(base_path, "nodeid.json"), 'w') as f:
                f.write(NODE_DATA_STR)
            print("✅ Persistent Identity Restored.")

        # 2. تهيئة سريعة
        subprocess.run(["gaianet", "init"], check=False)
        
        # 3. توجيه الإعدادات للووركر
        config_path = os.path.join(base_path, "config.json")
        with open(config_path, 'r') as f:
            config = json.load(f)
        config['chat'] = {"url": "http://localhost:7860/v1/chat/completions"}
        with open(config_path, 'w') as f:
            json.dump(config, f, indent=4)

        # 4. تشغيل العقدة ومحاولة فتح النفق بالقوة
        # سنحاول تشغيله عدة مرات لضمان اختراق جدار الحماية
        print("🚀 Forcing Tunnel Connection...")
        for i in range(3):
            subprocess.Popen(["gaianet", "start"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
            time.sleep(20)
            
        # 5. طباعة المعلومات الرسمية النهائية
        info = subprocess.check_output(["gaianet", "info"], text=True)
        print(f"\n{'='*50}\n{info}\n{'='*50}\n")

    except Exception as e:
        print(f"❌ Setup Failed: {e}")

@app.route('/v1/chat/completions', methods=['POST'])
def proxy():
    data = request.get_json()
    headers = {"Authorization": f"Bearer {WORKER_SECRET}", "Content-Type": "application/json"}
    try:
        response = requests.post(WORKER_URL, json=data, headers=headers, timeout=120)
        return jsonify(response.json()), response.status_code
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/')
def home(): return "Node Status: Fighting for Connection", 200

if __name__ == '__main__':
    setup_and_force_broadcast()
    app.run(host='0.0.0.0', port=7860)