|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/functionland/go-fula/wap/pkg/config" |
| 12 | + wifi "github.com/functionland/go-fula/wap/pkg/wifi" |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/libp2p/go-libp2p/core/network" |
| 15 | + "github.com/libp2p/go-libp2p/core/peer" |
| 16 | +) |
| 17 | + |
| 18 | +// handleAutoPinPair handles the auto-pin-pair action on the device side. |
| 19 | +// It stores pinning credentials in box_props.json and returns a pairing secret. |
| 20 | +func (bl *FxBlockchain) handleAutoPinPair(from peer.ID, w http.ResponseWriter, r *http.Request) { |
| 21 | + log := log.With("action", actionAutoPinPair, "from", from) |
| 22 | + defer r.Body.Close() |
| 23 | + |
| 24 | + var req AutoPinPairRequest |
| 25 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 26 | + log.Debugw("cannot parse request body", "err", err) |
| 27 | + http.Error(w, "", http.StatusBadRequest) |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + if req.PinningToken == "" || req.PinningEndpoint == "" { |
| 32 | + http.Error(w, `{"message":"pinning_token and pinning_endpoint are required"}`, http.StatusBadRequest) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + // Read current properties |
| 37 | + props, err := config.ReadProperties() |
| 38 | + if err != nil { |
| 39 | + // File may not exist yet; start fresh |
| 40 | + props = make(map[string]interface{}) |
| 41 | + } |
| 42 | + |
| 43 | + // Reject if already paired |
| 44 | + if existingToken, ok := props["auto_pin_token"]; ok && existingToken != nil && existingToken != "" { |
| 45 | + http.Error(w, `{"message":"already paired, unpair first"}`, http.StatusConflict) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // Generate pairing secret |
| 50 | + pairingSecret := uuid.New().String() |
| 51 | + |
| 52 | + // Store credentials |
| 53 | + props["auto_pin_token"] = req.PinningToken |
| 54 | + props["auto_pin_endpoint"] = req.PinningEndpoint |
| 55 | + props["auto_pin_pairing_secret"] = pairingSecret |
| 56 | + |
| 57 | + if err := config.WriteProperties(props); err != nil { |
| 58 | + log.Errorw("failed to write properties", "err", err) |
| 59 | + http.Error(w, `{"message":"failed to save pairing config"}`, http.StatusInternalServerError) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + // Get hardware ID for response |
| 64 | + hardwareID, err := wifi.GetHardwareID() |
| 65 | + if err != nil { |
| 66 | + log.Warnw("failed to get hardware ID", "err", err) |
| 67 | + hardwareID = "" |
| 68 | + } |
| 69 | + |
| 70 | + resp := AutoPinPairResponse{ |
| 71 | + Status: "paired", |
| 72 | + PairingSecret: pairingSecret, |
| 73 | + HardwareID: hardwareID, |
| 74 | + } |
| 75 | + |
| 76 | + w.Header().Set("Content-Type", "application/json") |
| 77 | + w.WriteHeader(http.StatusOK) |
| 78 | + json.NewEncoder(w).Encode(resp) |
| 79 | +} |
| 80 | + |
| 81 | +// handleAutoPinRefresh handles refreshing the pinning service token. |
| 82 | +func (bl *FxBlockchain) handleAutoPinRefresh(from peer.ID, w http.ResponseWriter, r *http.Request) { |
| 83 | + log := log.With("action", actionAutoPinRefresh, "from", from) |
| 84 | + defer r.Body.Close() |
| 85 | + |
| 86 | + var req AutoPinRefreshRequest |
| 87 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 88 | + log.Debugw("cannot parse request body", "err", err) |
| 89 | + http.Error(w, "", http.StatusBadRequest) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if req.PinningToken == "" { |
| 94 | + http.Error(w, `{"message":"pinning_token is required"}`, http.StatusBadRequest) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + props, err := config.ReadProperties() |
| 99 | + if err != nil { |
| 100 | + http.Error(w, `{"message":"not paired"}`, http.StatusNotFound) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + if _, ok := props["auto_pin_token"]; !ok { |
| 105 | + http.Error(w, `{"message":"not paired"}`, http.StatusNotFound) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + props["auto_pin_token"] = req.PinningToken |
| 110 | + if err := config.WriteProperties(props); err != nil { |
| 111 | + log.Errorw("failed to write properties", "err", err) |
| 112 | + http.Error(w, `{"message":"failed to update token"}`, http.StatusInternalServerError) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + w.Header().Set("Content-Type", "application/json") |
| 117 | + w.WriteHeader(http.StatusOK) |
| 118 | + json.NewEncoder(w).Encode(AutoPinRefreshResponse{Status: "refreshed"}) |
| 119 | +} |
| 120 | + |
| 121 | +// handleAutoPinUnpair removes auto-pin configuration. |
| 122 | +func (bl *FxBlockchain) handleAutoPinUnpair(from peer.ID, w http.ResponseWriter, r *http.Request) { |
| 123 | + log := log.With("action", actionAutoPinUnpair, "from", from) |
| 124 | + defer r.Body.Close() |
| 125 | + |
| 126 | + props, err := config.ReadProperties() |
| 127 | + if err != nil { |
| 128 | + http.Error(w, `{"message":"not paired"}`, http.StatusNotFound) |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + delete(props, "auto_pin_token") |
| 133 | + delete(props, "auto_pin_endpoint") |
| 134 | + delete(props, "auto_pin_pairing_secret") |
| 135 | + |
| 136 | + if err := config.WriteProperties(props); err != nil { |
| 137 | + log.Errorw("failed to write properties", "err", err) |
| 138 | + http.Error(w, `{"message":"failed to remove pairing config"}`, http.StatusInternalServerError) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + w.Header().Set("Content-Type", "application/json") |
| 143 | + w.WriteHeader(http.StatusOK) |
| 144 | + json.NewEncoder(w).Encode(AutoPinUnpairResponse{Status: "unpaired"}) |
| 145 | +} |
| 146 | + |
| 147 | +// AutoPinPair is the P2P client-side method for the mobile bridge. |
| 148 | +func (bl *FxBlockchain) AutoPinPair(ctx context.Context, to peer.ID, r AutoPinPairRequest) ([]byte, error) { |
| 149 | + if bl.allowTransientConnection { |
| 150 | + ctx = network.WithUseTransient(ctx, "fx.blockchain") |
| 151 | + } |
| 152 | + |
| 153 | + var buf bytes.Buffer |
| 154 | + if err := json.NewEncoder(&buf).Encode(r); err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + |
| 158 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+to.String()+".invalid/"+actionAutoPinPair, &buf) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + resp, err := bl.doP2PRequest(req) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + defer resp.Body.Close() |
| 167 | + b, err := io.ReadAll(resp.Body) |
| 168 | + |
| 169 | + switch { |
| 170 | + case err != nil: |
| 171 | + return nil, err |
| 172 | + case resp.StatusCode != http.StatusOK: |
| 173 | + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) |
| 174 | + default: |
| 175 | + return b, nil |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// AutoPinRefresh is the P2P client-side method for the mobile bridge. |
| 180 | +func (bl *FxBlockchain) AutoPinRefresh(ctx context.Context, to peer.ID, r AutoPinRefreshRequest) ([]byte, error) { |
| 181 | + if bl.allowTransientConnection { |
| 182 | + ctx = network.WithUseTransient(ctx, "fx.blockchain") |
| 183 | + } |
| 184 | + |
| 185 | + var buf bytes.Buffer |
| 186 | + if err := json.NewEncoder(&buf).Encode(r); err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + |
| 190 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+to.String()+".invalid/"+actionAutoPinRefresh, &buf) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + resp, err := bl.doP2PRequest(req) |
| 195 | + if err != nil { |
| 196 | + return nil, err |
| 197 | + } |
| 198 | + defer resp.Body.Close() |
| 199 | + b, err := io.ReadAll(resp.Body) |
| 200 | + |
| 201 | + switch { |
| 202 | + case err != nil: |
| 203 | + return nil, err |
| 204 | + case resp.StatusCode != http.StatusOK: |
| 205 | + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) |
| 206 | + default: |
| 207 | + return b, nil |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +// AutoPinUnpair is the P2P client-side method for the mobile bridge. |
| 212 | +func (bl *FxBlockchain) AutoPinUnpair(ctx context.Context, to peer.ID) ([]byte, error) { |
| 213 | + if bl.allowTransientConnection { |
| 214 | + ctx = network.WithUseTransient(ctx, "fx.blockchain") |
| 215 | + } |
| 216 | + |
| 217 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+to.String()+".invalid/"+actionAutoPinUnpair, nil) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + resp, err := bl.doP2PRequest(req) |
| 222 | + if err != nil { |
| 223 | + return nil, err |
| 224 | + } |
| 225 | + defer resp.Body.Close() |
| 226 | + b, err := io.ReadAll(resp.Body) |
| 227 | + |
| 228 | + switch { |
| 229 | + case err != nil: |
| 230 | + return nil, err |
| 231 | + case resp.StatusCode != http.StatusOK: |
| 232 | + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) |
| 233 | + default: |
| 234 | + return b, nil |
| 235 | + } |
| 236 | +} |
0 commit comments