forked from DHEBP/HOLOGRAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnomon_ws_server.go
More file actions
488 lines (418 loc) · 11.9 KB
/
Copy pathgnomon_ws_server.go
File metadata and controls
488 lines (418 loc) · 11.9 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright 2025 HOLOGRAM Project. All rights reserved.
// Gnomon WebSocket Query API Server
// Ported from simple-gnomon for external app integration
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
// Default WebSocket server configuration
const (
DefaultWSPort = 9190
MaxWSPort = 9199
WSReadTimeout = 60 * time.Second
WSWriteTimeout = 10 * time.Second
WSPingPeriod = 30 * time.Second
WSMaxMessageSize = 1024 * 1024 // 1MB
)
// GnomonWSRequest represents an incoming WebSocket request
type GnomonWSRequest struct {
ID any `json:"id"`
Method string `json:"method"`
Params map[string]any `json:"params,omitempty"`
}
// GnomonWSResponse represents an outgoing WebSocket response
type GnomonWSResponse struct {
ID any `json:"id"`
Result any `json:"result,omitempty"`
Error *WSError `json:"error,omitempty"`
}
// WSError represents a WebSocket error
type WSError struct {
Code int `json:"code"`
Message string `json:"message"`
}
// GnomonWSServer manages the WebSocket API server
type GnomonWSServer struct {
app *App
server *http.Server
address string
port int
running bool
clients map[*websocket.Conn]bool
clientsMu sync.RWMutex
upgrader websocket.Upgrader
}
// NewGnomonWSServer creates a new WebSocket API server
func NewGnomonWSServer(app *App, address string) *GnomonWSServer {
if address == "" {
address = fmt.Sprintf("127.0.0.1:%d", DefaultWSPort)
}
return &GnomonWSServer{
app: app,
address: address,
clients: make(map[*websocket.Conn]bool),
upgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Allow all origins (local API)
},
},
}
}
// Start starts the WebSocket server
func (s *GnomonWSServer) Start(ctx context.Context) error {
if s.running {
return fmt.Errorf("WebSocket server already running")
}
var listener net.Listener
var err error
// If address was provided, try to use it directly first
if s.address != "" && s.address != fmt.Sprintf("127.0.0.1:%d", DefaultWSPort) {
listener, err = net.Listen("tcp", s.address)
if err == nil {
// Parse port from address for tracking
if parts := strings.Split(s.address, ":"); len(parts) == 2 {
if p, parseErr := strconv.Atoi(parts[1]); parseErr == nil {
s.port = p
}
}
}
}
// Fall back to auto-port scanning if no address or listen failed
if listener == nil {
port := DefaultWSPort
for port <= MaxWSPort {
addr := fmt.Sprintf("127.0.0.1:%d", port)
listener, err = net.Listen("tcp", addr)
if err == nil {
s.address = addr
s.port = port
break
}
port++
}
}
if listener == nil {
return fmt.Errorf("no available port in range %d-%d", DefaultWSPort, MaxWSPort)
}
mux := http.NewServeMux()
mux.HandleFunc("/ws", s.handleWebSocket)
mux.HandleFunc("/health", s.handleHealth)
s.server = &http.Server{
Handler: mux,
ReadTimeout: WSReadTimeout,
WriteTimeout: WSWriteTimeout,
}
s.running = true
log.Printf("[GNOMON-WS] WebSocket API server starting on ws://%s/ws", s.address)
go func() {
if err := s.server.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Printf("[GNOMON-WS] Server error: %v", err)
s.running = false
}
}()
// Wait for context cancellation
go func() {
<-ctx.Done()
s.Stop()
}()
return nil
}
// Stop stops the WebSocket server
func (s *GnomonWSServer) Stop() {
if !s.running {
return
}
s.running = false
// Close all client connections
s.clientsMu.Lock()
for conn := range s.clients {
conn.Close()
delete(s.clients, conn)
}
s.clientsMu.Unlock()
if s.server != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.server.Shutdown(ctx)
}
log.Printf("[GNOMON-WS] WebSocket API server stopped")
}
// IsRunning returns whether the server is running
func (s *GnomonWSServer) IsRunning() bool {
return s.running
}
// GetAddress returns the server address
func (s *GnomonWSServer) GetAddress() string {
return s.address
}
// GetPort returns the server port
func (s *GnomonWSServer) GetPort() int {
return s.port
}
// GetClientCount returns the number of connected clients
func (s *GnomonWSServer) GetClientCount() int {
s.clientsMu.RLock()
defer s.clientsMu.RUnlock()
return len(s.clients)
}
// handleHealth handles health check requests
func (s *GnomonWSServer) handleHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"status": "ok",
"gnomon": s.app.gnomonClient != nil && s.app.gnomonClient.IsRunning(),
"clients": s.GetClientCount(),
})
}
// handleWebSocket handles WebSocket connections
func (s *GnomonWSServer) handleWebSocket(w http.ResponseWriter, r *http.Request) {
conn, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("[GNOMON-WS] Upgrade failed: %v", err)
return
}
// Register client
s.clientsMu.Lock()
s.clients[conn] = true
s.clientsMu.Unlock()
log.Printf("[GNOMON-WS] Client connected from %s", conn.RemoteAddr())
// Cleanup on disconnect
defer func() {
s.clientsMu.Lock()
delete(s.clients, conn)
s.clientsMu.Unlock()
conn.Close()
log.Printf("[GNOMON-WS] Client disconnected from %s", conn.RemoteAddr())
}()
// Set read limit
conn.SetReadLimit(WSMaxMessageSize)
// Handle messages
for {
_, message, err := conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("[GNOMON-WS] Read error: %v", err)
}
break
}
// Parse request
var req GnomonWSRequest
if err := json.Unmarshal(message, &req); err != nil {
s.sendError(conn, nil, -32700, "Parse error")
continue
}
// Handle request
response := s.handleRequest(req)
// Send response
if err := conn.WriteJSON(response); err != nil {
log.Printf("[GNOMON-WS] Write error: %v", err)
break
}
}
}
// sendError sends an error response
func (s *GnomonWSServer) sendError(conn *websocket.Conn, id any, code int, message string) {
response := GnomonWSResponse{
ID: id,
Error: &WSError{
Code: code,
Message: message,
},
}
conn.WriteJSON(response)
}
// handleRequest processes a WebSocket request
func (s *GnomonWSServer) handleRequest(req GnomonWSRequest) GnomonWSResponse {
// Check if Gnomon is running
if s.app.gnomonClient == nil || !s.app.gnomonClient.IsRunning() {
return GnomonWSResponse{
ID: req.ID,
Error: &WSError{
Code: -32603,
Message: "Gnomon is not running",
},
}
}
// Route method
method := strings.ToLower(req.Method)
var result any
switch method {
// === SCID Queries ===
case "getallscids", "get_all_scids":
// Return list of SCIDs (keys from scid->owner map)
ownersMap := s.app.gnomonClient.GetAllOwnersAndSCIDs()
scidList := make([]string, 0, len(ownersMap))
for scid := range ownersMap {
scidList = append(scidList, scid)
}
result = scidList
case "getallownersandscids", "get_all_owners_and_scids":
// Return full map for clients that need owner info
result = s.app.gnomonClient.GetAllOwnersAndSCIDs()
case "getscidvariables", "get_scid_variables":
scid := getStringParam(req.Params, "scid")
if scid == "" {
return errorResponse(req.ID, -32602, "Missing 'scid' parameter")
}
result = s.app.gnomonClient.GetAllSCIDVariableDetails(scid)
case "getscidvaluesbykey", "get_scid_values_by_key":
scid := getStringParam(req.Params, "scid")
key := getStringParam(req.Params, "key")
if scid == "" || key == "" {
return errorResponse(req.ID, -32602, "Missing 'scid' or 'key' parameter")
}
valStr, valUint := s.app.gnomonClient.GetSCIDValuesByKey(scid, key)
result = map[string]any{
"values_string": valStr,
"values_uint64": valUint,
}
case "getscidkeysbyvalue", "get_scid_keys_by_value":
scid := getStringParam(req.Params, "scid")
value := req.Params["value"]
if scid == "" || value == nil {
return errorResponse(req.ID, -32602, "Missing 'scid' or 'value' parameter")
}
keyStr, keyUint := s.app.gnomonClient.GetSCIDKeysByValue(scid, value)
result = map[string]any{
"keys_string": keyStr,
"keys_uint64": keyUint,
}
// === TELA Queries ===
case "gettelaapps", "get_tela_apps":
result = s.app.gnomonClient.GetTELAApps()
case "gettelalibraries", "get_tela_libraries":
result = s.app.gnomonClient.GetTELALibraries()
case "searchtelaapps", "search_tela_apps":
query := getStringParam(req.Params, "query")
result = s.app.gnomonClient.SearchTELApps(query)
case "resolvedurl", "resolve_durl":
durl := getStringParam(req.Params, "durl")
if scid, ok := s.app.resolveServableDURL(durl); ok {
result = map[string]any{"scid": scid, "found": true}
} else {
result = map[string]any{"found": false}
}
case "resolvename", "resolve_name":
name := getStringParam(req.Params, "name")
if scid, ok := s.app.gnomonClient.ResolveName(name); ok {
result = map[string]any{"scid": scid, "found": true}
} else {
result = map[string]any{"found": false}
}
// === Tag System ===
case "getscidsbytag", "get_scids_by_tag":
tag := getStringParam(req.Params, "tag")
store := InitSCIDTagStore()
result = store.GetSCIDsByTag(tag)
case "getscidsbyclass", "get_scids_by_class":
class := getStringParam(req.Params, "class")
store := InitSCIDTagStore()
result = store.GetSCIDsByClass(class)
case "getalltags", "get_all_tags":
store := InitSCIDTagStore()
result = store.GetAllTags()
case "getallclasses", "get_all_classes":
store := InitSCIDTagStore()
result = store.GetAllClasses()
case "getscidmetadata", "get_scid_metadata":
scid := getStringParam(req.Params, "scid")
store := InitSCIDTagStore()
result = store.GetMetadata(scid)
case "gettagstats", "get_tag_stats":
store := InitSCIDTagStore()
result = store.GetStats()
// === Status ===
case "getstatus", "get_status":
result = s.app.gnomonClient.GetStatus()
case "getindexheight", "get_index_height":
status := s.app.gnomonClient.GetStatus()
result = map[string]any{
"indexed_height": status["indexed_height"],
"chain_height": status["chain_height"],
"progress": status["progress"],
}
// === AddSCIDToIndex (Bug #1 fix) ===
// Manually index a SCID that fastsync missed. Compatible with civilware's
// feat-addscidtoindex-wsserver WS API so dApps following that pattern just work.
case "addscidtoindex", "addscid_toindex", "add_scid_to_index":
scid := getStringParam(req.Params, "scid")
if scid == "" {
return errorResponse(req.ID, -32602, "Missing 'scid' parameter")
}
varstoreonly := getBoolParam(req.Params, "varstoreonly")
skipfsrecheck := getBoolParam(req.Params, "skipfsrecheck")
err := s.app.gnomonClient.AddSCIDToIndex(scid, varstoreonly, skipfsrecheck)
if err != nil {
result = map[string]any{
"success": false,
"result": fmt.Sprintf("Err - %v", err),
}
} else {
result = map[string]any{
"success": true,
"result": "Success",
"scid": scid,
}
}
default:
return errorResponse(req.ID, -32601, fmt.Sprintf("Method not found: %s", req.Method))
}
return GnomonWSResponse{
ID: req.ID,
Result: result,
}
}
// Helper functions
func errorResponse(id any, code int, message string) GnomonWSResponse {
return GnomonWSResponse{
ID: id,
Error: &WSError{
Code: code,
Message: message,
},
}
}
func getStringParam(params map[string]any, key string) string {
if params == nil {
return ""
}
if val, ok := params[key].(string); ok {
return val
}
return ""
}
func getInt64Param(params map[string]any, key string) int64 {
if params == nil {
return 0
}
switch v := params[key].(type) {
case float64:
return int64(v)
case int64:
return v
case int:
return int64(v)
}
return 0
}
func getBoolParam(params map[string]any, key string) bool {
if params == nil {
return false
}
if v, ok := params[key].(bool); ok {
return v
}
return false
}