// ============================================================================ // The-Tired-AGI: Remote Server Bridge // Connects local system to remote AGI server over IPv6 // ============================================================================ package main import ( "crypto/tls" "encoding/json" "fmt" "io" "log" "net/http" "sync" "time" ) const ( remoteServer = "https://[2600:8803:bf0a:e000:4eea:41ff:fe69:2998]/s" authToken = "[credential removed] rIkvuojBgWpPzxNBEZnYGw0yrj31BWRuZ7L5Sm17sXUXN4oCEjUgh-v1DkiTcKVJ" bridgePort = ":8009" ) // RemoteState represents the remote AGI server state type RemoteState struct { Now string `json:"now"` Tick int `json:"tick"` NodeID string `json:"node_id"` LastSync time.Time Error string } var ( remoteState RemoteState stateMutex sync.RWMutex httpClient *http.Client ) func init() { // Create HTTP client with IPv6 support and insecure TLS httpClient = &http.Client{ Timeout: 5 * time.Second, Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, ForceAttemptHTTP2: true, }, } } func main() { log.Printf("🌉 AGI Remote Bridge starting") log.Printf("Remote: %s", remoteServer) log.Printf("Local: http://localhost%s", bridgePort) // Start background sync go syncLoop() // HTTP endpoints with CORS http.HandleFunc("/remote/status", corsMiddleware(remoteStatusHandler)) http.HandleFunc("/remote/stats", corsMiddleware(remoteStatsHandler)) http.HandleFunc("/health", corsMiddleware(healthHandler)) log.Printf("✅ Endpoints: /remote/status /remote/stats /health") log.Fatal(http.ListenAndServe(bridgePort, nil)) } // Sync loop - polls remote server every 2 seconds func syncLoop() { ticker := time.NewTicker(2 * time.Second) defer ticker.Stop() // Initial sync fetchRemoteState() for range ticker.C { fetchRemoteState() } } // Fetch state from remote server func fetchRemoteState() { req, err := http.NewRequest("GET", remoteServer, nil) if err != nil { updateStateError(fmt.Sprintf("Request creation failed: %v", err)) return } req.Header.Set("Authorization", authToken) resp, err := httpClient.Do(req) if err != nil { updateStateError(fmt.Sprintf("Connection failed: %v", err)) log.Printf("⚠️ Remote connection failed: %v", err) return } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { updateStateError(fmt.Sprintf("HTTP %d", resp.StatusCode)) log.Printf("⚠️ Remote returned HTTP %d", resp.StatusCode) return } body, err := io.ReadAll(resp.Body) if err != nil { updateStateError(fmt.Sprintf("Read failed: %v", err)) return } var state RemoteState if err := json.Unmarshal(body, &state); err != nil { updateStateError(fmt.Sprintf("Parse failed: %v", err)) return } // Update state stateMutex.Lock() remoteState = state remoteState.LastSync = time.Now() remoteState.Error = "" stateMutex.Unlock() log.Printf("✅ Remote sync: tick=%d, node=%s", state.Tick, state.NodeID) } // Update state with error func updateStateError(errMsg string) { stateMutex.Lock() remoteState.Error = errMsg remoteState.LastSync = time.Now() stateMutex.Unlock() } // Remote status handler - returns JSON func remoteStatusHandler(w http.ResponseWriter, r *http.Request) { stateMutex.RLock() state := remoteState stateMutex.RUnlock() w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(state) } // Remote stats handler - returns plain text (compatible with gossip format) func remoteStatsHandler(w http.ResponseWriter, r *http.Request) { stateMutex.RLock() state := remoteState stateMutex.RUnlock() w.Header().Set("Content-Type", "text/plain") if state.Error != "" { fmt.Fprintf(w, "Status: Error\n") fmt.Fprintf(w, "Error: %s\n", state.Error) fmt.Fprintf(w, "LastSync: %s\n", state.LastSync.Format(time.RFC3339)) return } fmt.Fprintf(w, "NodeID: %s\n", state.NodeID) fmt.Fprintf(w, "Tick: %d\n", state.Tick) fmt.Fprintf(w, "Time: %s\n", state.Now) fmt.Fprintf(w, "LastSync: %s\n", state.LastSync.Format(time.RFC3339)) } // Health check func healthHandler(w http.ResponseWriter, r *http.Request) { stateMutex.RLock() lastSync := remoteState.LastSync hasError := remoteState.Error != "" stateMutex.RUnlock() healthy := !hasError && time.Since(lastSync) < 10*time.Second w.Header().Set("Content-Type", "application/json") if healthy { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]interface{}{ "status": "healthy", "last_sync": lastSync.Format(time.RFC3339), }) } else { w.WriteHeader(http.StatusServiceUnavailable) json.NewEncoder(w).Encode(map[string]interface{}{ "status": "unhealthy", "last_sync": lastSync.Format(time.RFC3339), "error": remoteState.Error, }) } } // CORS middleware func corsMiddleware(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") if r.Method == "OPTIONS" { w.WriteHeader(http.StatusOK) return } next(w, r) } }