MCPcopy Create free account
hub / github.com/coder/wush / handleNextMessage

Method handleNextMessage

overlay/receive.go:383–452  ·  view source on GitHub ↗
(src key.NodePublic, msg []byte, system string)

Source from the content-addressed store, hash-verified

381}
382
383func (r *Receive) handleNextMessage(src key.NodePublic, msg []byte, system string) (resRaw []byte, nodeKey key.NodePublic, _ error) {
384 cleartext, ok := r.SelfPriv.OpenFrom(r.PeerPriv.Public(), msg)
385 if !ok {
386 return nil, key.NodePublic{}, errors.New("message failed decryption")
387 }
388
389 var ovMsg overlayMessage
390 err := json.Unmarshal(cleartext, &ovMsg)
391 if err != nil {
392 panic("unmarshal node: " + err.Error())
393 }
394
395 res := overlayMessage{}
396 switch ovMsg.Typ {
397 case messageTypePing:
398 res.Typ = messageTypePong
399 case messageTypePong:
400 // do nothing
401 case messageTypeHello:
402 res.Typ = messageTypeHelloResponse
403 username := "unknown"
404 if u := ovMsg.HostInfo.Username; u != "" {
405 username = u
406 }
407 hostname := "unknown"
408 if h := ovMsg.HostInfo.Hostname; h != "" {
409 hostname = h
410 }
411 if lastNode := r.lastNode.Load(); lastNode != nil {
412 res.Node = *lastNode
413 }
414
415 if ovMsg.WebrtcDescription != nil {
416 r.setupWebrtcConnection(src, &res, *ovMsg.WebrtcDescription)
417 }
418
419 r.HumanLogf("%s Received connection request over %s from %s", cliui.Timestamp(time.Now()), system, cliui.Keyword(fmt.Sprintf("%s@%s", username, hostname)))
420 case messageTypeNodeUpdate:
421 r.Logger.Debug("received updated node", slog.String("node_key", ovMsg.Node.Key.String()))
422 r.in <- &ovMsg.Node
423 res.Typ = messageTypeNodeUpdate
424 if lastNode := r.lastNode.Load(); lastNode != nil {
425 res.Node = *lastNode
426 }
427
428 case messageTypeWebRTCCandidate:
429 pc, ok := r.webrtcConns.Load(src)
430 if !ok {
431 fmt.Println("got candidate for unknown connection")
432 break
433 }
434
435 err := pc.AddICECandidate(*ovMsg.WebrtcCandidate)
436 if err != nil {
437 fmt.Println("failed to add ice candidate:", err)
438 }
439 }
440

Callers 2

ListenOverlaySTUNMethod · 0.95
ListenOverlayDERPMethod · 0.95

Calls 3

setupWebrtcConnectionMethod · 0.95
TimestampFunction · 0.92
KeywordFunction · 0.92

Tested by

no test coverage detected