Skip to content

Commit 9484b86

Browse files
committed
re-register
1 parent edbb5ed commit 9484b86

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

blox/kubo_proxy.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"net/http"
9+
"net/url"
810
"strings"
911
"time"
1012
)
@@ -35,7 +37,21 @@ var defaultProtocols = []p2pProtocol{
3537

3638
// registerKuboProtocols registers p2p protocol listeners on kubo via its HTTP API.
3739
// Each protocol maps a libp2p stream protocol to a local TCP address.
40+
// It first closes ALL existing p2p listeners to clear any stale state from
41+
// previous go-fula processes, then registers fresh listeners.
3842
func registerKuboProtocols(kuboAPI string) error {
43+
// Close all existing p2p listeners first to clear stale state.
44+
// This is safe because go-fula owns all p2p protocol registrations on kubo.
45+
closeAllURL := fmt.Sprintf("http://%s/api/v0/p2p/close?all=true", kuboAPI)
46+
closeResp, err := http.Post(closeAllURL, "", nil)
47+
if err != nil {
48+
log.Warnw("Could not close existing p2p listeners", "err", err)
49+
} else {
50+
body, _ := io.ReadAll(closeResp.Body)
51+
closeResp.Body.Close()
52+
log.Debugw("Closed existing p2p listeners", "status", closeResp.StatusCode, "response", string(body))
53+
}
54+
3955
for _, p := range defaultProtocols {
4056
if err := registerSingleProtocol(kuboAPI, p.name, p.target); err != nil {
4157
return fmt.Errorf("failed to register protocol %s: %w", p.name, err)
@@ -46,21 +62,9 @@ func registerKuboProtocols(kuboAPI string) error {
4662
}
4763

4864
func registerSingleProtocol(kuboAPI, protocol, target string) error {
49-
// First close any existing listener for this protocol (idempotent).
50-
// Use only arg=<protocol> so it matches regardless of listen/target address.
51-
closeURL := fmt.Sprintf("http://%s/api/v0/p2p/close?arg=%s",
52-
kuboAPI, protocol)
53-
closeResp, err := http.Post(closeURL, "", nil)
54-
if err != nil {
55-
log.Debugw("Could not close existing p2p listener (may not exist)", "protocol", protocol, "err", err)
56-
} else {
57-
closeResp.Body.Close()
58-
}
59-
60-
// Register the protocol
61-
url := fmt.Sprintf("http://%s/api/v0/p2p/listen?arg=%s&arg=%s&allow-custom-protocol=true",
62-
kuboAPI, protocol, target)
63-
resp, err := http.Post(url, "", nil)
65+
listenURL := fmt.Sprintf("http://%s/api/v0/p2p/listen?arg=%s&arg=%s&allow-custom-protocol=true",
66+
kuboAPI, url.QueryEscape(protocol), url.QueryEscape(target))
67+
resp, err := http.Post(listenURL, "", nil)
6468
if err != nil {
6569
return fmt.Errorf("kubo API request failed: %w", err)
6670
}
@@ -70,7 +74,6 @@ func registerSingleProtocol(kuboAPI, protocol, target string) error {
7074
body := make([]byte, 512)
7175
n, _ := resp.Body.Read(body)
7276
bodyStr := string(body[:n])
73-
// "listener already registered" means the protocol is active — not an error
7477
if strings.Contains(bodyStr, "listener already registered") {
7578
log.Debugw("Protocol already registered, treating as success", "protocol", protocol)
7679
return nil

mobile/device_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fulamobile
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"testing"
@@ -108,6 +109,24 @@ func TestRealDeviceConnection(t *testing.T) {
108109
time.Sleep(20 * time.Second)
109110
}
110111

112+
// --- Step 0: Verify libp2p connectivity to kubo ---
113+
t.Run("ConnectToBlox", func(t *testing.T) {
114+
t.Logf("Establishing libp2p connection to kubo...")
115+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
116+
defer cancel()
117+
peerInfo := client.h.Peerstore().PeerInfo(client.bloxPid)
118+
t.Logf("Peerstore addresses for blox: %v", peerInfo.Addrs)
119+
err := client.h.Connect(ctx, peerInfo)
120+
require.NoError(t, err, "Failed to establish libp2p connection to kubo — check IP/relay/firewall")
121+
t.Logf("Connected to kubo successfully")
122+
123+
// Log the connection details
124+
conns := client.h.Network().ConnsToPeer(client.bloxPid)
125+
for i, conn := range conns {
126+
t.Logf(" Connection %d: local=%s remote=%s", i, conn.LocalMultiaddr(), conn.RemoteMultiaddr())
127+
}
128+
})
129+
111130
// --- Test 1: PoolList (read-only, safe to call) ---
112131
t.Run("PoolList", func(t *testing.T) {
113132
t.Logf("Calling PoolList...")

0 commit comments

Comments
 (0)