this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

more sibling forward fixes

+50 -17
+6 -1
cmd/relay/handlers.go
··· 1 1 package main 2 2 3 3 import ( 4 + "encoding/json" 4 5 "errors" 5 6 "fmt" 6 7 "net/http" ··· 58 59 } 59 60 60 61 // forward on to any sibling instances (note that sometimes is, sometimes isn't an admin request) 61 - go s.ForwardAdminRequest(c) 62 + b, err := json.Marshal(body) 63 + if err != nil { 64 + return err 65 + } 66 + go s.ForwardSiblingRequest(c, b) 62 67 63 68 return s.relay.SubscribeToHost(ctx, hostname, noSSL, false) 64 69 }
+44 -16
cmd/relay/handlers_admin.go
··· 1 1 package main 2 2 3 3 import ( 4 + "bytes" 5 + "encoding/json" 4 6 "errors" 5 7 "fmt" 8 + "io" 6 9 "net/http" 7 10 "strconv" 8 11 "strings" ··· 98 101 } 99 102 100 103 // forward on to any sibling instances 101 - go s.ForwardAdminRequest(c) 104 + b, err := json.Marshal(body) 105 + if err != nil { 106 + return err 107 + } 108 + go s.ForwardSiblingRequest(c, b) 102 109 103 110 return c.JSON(http.StatusOK, map[string]any{ 104 111 "success": "true", ··· 138 145 } 139 146 140 147 // forward on to any sibling instances 141 - go s.ForwardAdminRequest(c) 148 + b, err := json.Marshal(body) 149 + if err != nil { 150 + return err 151 + } 152 + go s.ForwardSiblingRequest(c, b) 142 153 143 154 return c.JSON(http.StatusOK, map[string]any{ 144 155 "success": "true", ··· 344 355 _ = s.relay.Slurper.KillUpstreamConnection(ctx, host.Hostname, false) 345 356 346 357 // forward on to any sibling instances 347 - go s.ForwardAdminRequest(c) 358 + go s.ForwardSiblingRequest(c, nil) 348 359 349 360 return c.JSON(http.StatusOK, map[string]any{ 350 361 "success": "true", ··· 375 386 } 376 387 377 388 // forward on to any sibling instances 378 - go s.ForwardAdminRequest(c) 389 + go s.ForwardSiblingRequest(c, nil) 379 390 380 391 return c.JSON(http.StatusOK, map[string]any{ 381 392 "success": "true", ··· 423 434 } 424 435 425 436 // forward on to any sibling instances 426 - go s.ForwardAdminRequest(c) 437 + b, err := json.Marshal(body) 438 + if err != nil { 439 + return err 440 + } 441 + go s.ForwardSiblingRequest(c, b) 427 442 428 443 return c.JSON(http.StatusOK, map[string]any{ 429 444 "success": "true", ··· 444 459 } 445 460 446 461 // forward on to any sibling instances 447 - go s.ForwardAdminRequest(c) 462 + b, err := json.Marshal(body) 463 + if err != nil { 464 + return err 465 + } 466 + go s.ForwardSiblingRequest(c, b) 448 467 449 468 return c.JSON(http.StatusOK, map[string]any{ 450 469 "success": "true", ··· 485 504 } 486 505 487 506 // forward on to any sibling instances 488 - go s.ForwardAdminRequest(c) 507 + b, err := json.Marshal(body) 508 + if err != nil { 509 + return err 510 + } 511 + go s.ForwardSiblingRequest(c, b) 489 512 490 513 return c.JSON(http.StatusOK, map[string]any{ 491 514 "success": "true", ··· 493 516 } 494 517 495 518 // this method expects to be run in a goroutine. it does not take a `context.Context`, the input `echo.Context` has likely be cancelled/closed, and does not return an error (only logs) 496 - func (s *Service) ForwardAdminRequest(c echo.Context) { 519 + func (s *Service) ForwardSiblingRequest(c echo.Context, body []byte) { 497 520 498 521 if len(s.config.SiblingRelayHosts) == 0 { 499 522 return ··· 531 554 } else { 532 555 u.Scheme = "https" 533 556 } 534 - upstreamReq, err := http.NewRequest(req.Method, u.String(), req.Body) 557 + var b io.Reader 558 + if body != nil { 559 + b = bytes.NewBuffer(body) 560 + } 561 + upstreamReq, err := http.NewRequest(req.Method, u.String(), b) 535 562 if err != nil { 536 563 s.logger.Error("creating admin forward request failed", "method", req.Method, "url", u.String(), "err", err) 537 564 continue 538 565 } 539 566 540 567 // copy some headers from inbound request 541 - for k, vals := range req.Header { 542 - if strings.ToLower(k) == "accept" || strings.ToLower(k) == "authentication" { 543 - upstreamReq.Header.Add(k, vals[0]) 568 + for _, hdr := range []string{"Accept", "User-Agent", "Authorization", "Content-Type"} { 569 + val := req.Header.Get(hdr) 570 + if val != "" { 571 + upstreamReq.Header.Set(hdr, val) 544 572 } 545 573 } 546 - upstreamReq.Header.Add("User-Agent", s.relay.Config.UserAgent) 547 - upstreamReq.Header.Add("Forwarded", "by=relay") 574 + upstreamReq.Header.Add("Via", s.relay.Config.UserAgent) 548 575 549 576 upstreamResp, err := client.Do(upstreamReq) 550 577 if err != nil { 551 578 s.logger.Error("forwarded admin HTTP request failed", "method", req.Method, "url", u.String(), "err", err) 552 579 continue 553 580 } 554 - upstreamResp.Body.Close() 555 581 if upstreamResp.StatusCode != http.StatusOK { 556 - s.logger.Error("forwarded admin HTTP request failed", "method", req.Method, "url", u.String(), "statusCode", upstreamResp.StatusCode) 582 + respBytes, _ := io.ReadAll(upstreamResp.Body) 583 + s.logger.Error("forwarded admin HTTP request failed", "method", req.Method, "url", u.String(), "statusCode", upstreamResp.StatusCode, "body", string(respBytes)) 557 584 continue 558 585 } 586 + upstreamResp.Body.Close() 559 587 s.logger.Info("successfully forwarded admin HTTP request", "method", req.Method, "url", u.String()) 560 588 } 561 589 }