0
0
package guestagent
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
"filippo.io/age"
"github.com/pion/webrtc/v4"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/cloudinit"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/tunnel"
irohingress "github.com/johnandersen777/socialweb-computer-kcp/internal/ingress/iroh"
webrtcingress "github.com/johnandersen777/socialweb-computer-kcp/internal/ingress/webrtc"
)
const (
RequesterFile = "requester.json"
RequesterSocketFile = "requester.sock"
requesterTag = "requester"
DefaultDumbpipe = "/usr/local/bin/dumbpipe"
requesterChannelTimeout = cloudinit.SecretsPeerChannelTimeoutSec * time.Second
requesterChannelIroh = "iroh"
requesterChannelWebRTC = "webrtc"
)
type Requester struct {
Iroh string `json:"iroh,omitempty"`
WebRTC string `json:"webrtc,omitempty"`
}
type requesterOfferPlaintext struct {
SDP string `json:"sdp"`
ReplyTo string `json:"replyTo"`
ID string `json:"id"`
Session string `json:"session"`
Peer string `json:"peer"`
}
type requesterChannel struct {
name string
base string
client *http.Client
}
func (a *Agent) requesterChannels() []requesterChannel {
r := a.cfg.Requester
var out []requesterChannel
if r.Iroh != "" {
out = append(out, requesterChannel{name: requesterChannelIroh, base: r.Iroh, client: irohingress.Client(nil, DefaultDumbpipe, a.log)})
}
if r.WebRTC != "" && a.requesterPeer != nil {
out = append(out, requesterChannel{name: requesterChannelWebRTC, base: tunnel.Ref(r.WebRTC), client: webrtcingress.Client(nil, a.requesterPeer)})
}
return out
}
func (a *Agent) runRequester(ctx context.Context) {
if a.cfg.Requester.WebRTC != "" {
a.requesterPeer = webrtcingress.NewPeerWith(webrtcingress.PeerConfig{
API: a.api,
Configuration: webrtc.Configuration{
ICEServers: []webrtc.ICEServer{{URLs: []string{STUNServer}}},
Certificates: []webrtc.Certificate{a.certificate},
},
Log: a.log,
})
key := publicKeyRecord{Type: PublicKeyNSID, Kind: publicKeyKindAgeX25519, Tag: requesterTag, Key: a.identity.Recipient().String(), CreatedAt: createdAt()}
a.wg.Go(func() { _ = a.poster.Post(ctx, key.Type, requesterTag, key) })
}
path := filepath.Join(a.cfg.StateDir, RequesterSocketFile)
_ = os.Remove(path)
listener, err := net.Listen("unix", path)
if err != nil {
a.log.Error("guest_requester_socket_failed", "path", path, "err", err.Error())
return
}
if err := os.Chmod(path, 0o600); err != nil {
_ = listener.Close()
a.log.Error("guest_requester_socket_failed", "path", path, "err", err.Error())
return
}
server := &http.Server{Handler: http.HandlerFunc(a.forwardToRequester), ReadHeaderTimeout: requesterChannelTimeout}
a.log.Info("guest_requester_socket", "path", path, "iroh", a.cfg.Requester.Iroh != "", "webrtc", a.cfg.Requester.WebRTC != "")
stop := context.AfterFunc(ctx, func() { _ = server.Close() })
defer stop()
if err := server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
a.log.Warn("guest_requester_socket_closed", "err", err.Error())
}
if a.requesterPeer != nil {
_ = a.requesterPeer.Close()
}
}
var hopHeaders = []string{"Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization", "Proxy-Connection", "Te", "Trailer", "Transfer-Encoding", "Upgrade"}
func endToEnd(h http.Header) http.Header {
out := h.Clone()
for _, field := range out.Values("Connection") {
for _, name := range strings.Split(field, ",") {
out.Del(strings.TrimSpace(name))
}
}
for _, name := range hopHeaders {
out.Del(name)
}
return out
}
func idempotent(method string) bool {
return method == http.MethodGet || method == http.MethodHead
}
func innerError(err error) string {
var ue *url.Error
if errors.As(err, &ue) {
return ue.Op + ": " + ue.Err.Error()
}
return err.Error()
}
func noRedirect(c *http.Client) *http.Client {
out := *c
out.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
return &out
}
func (a *Agent) forwardToRequester(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "reading the request: "+err.Error(), http.StatusBadRequest)
return
}
var failures []string
for _, channel := range a.requesterChannels() {
ctx, cancel := context.WithTimeout(r.Context(), requesterChannelTimeout)
var wrote atomic.Bool
trace := &httptrace.ClientTrace{WroteRequest: func(httptrace.WroteRequestInfo) { wrote.Store(true) }}
out, err := http.NewRequestWithContext(httptrace.WithClientTrace(ctx, trace), r.Method, channel.base+r.URL.RequestURI(), bytes.NewReader(body))
if err != nil {
cancel()
failures = append(failures, channel.name+": "+innerError(err))
continue
}
out.Header = endToEnd(r.Header)
resp, err := noRedirect(channel.client).Do(out)
if err != nil {
cancel()
a.log.Warn("guest_requester_channel_failed", "channel", channel.name, "method", r.Method, "path", r.URL.Path, "sent", wrote.Load(), "err", innerError(err))
failures = append(failures, channel.name+": "+innerError(err))
if wrote.Load() && !idempotent(r.Method) {
http.Error(w, "the requester did not answer after the request was sent: "+strings.Join(failures, "; "), http.StatusBadGateway)
return
}
continue
}
for k, values := range endToEnd(resp.Header) {
for _, v := range values {
w.Header().Add(k, v)
}
}
w.Header().Set("Socialweb-Channel", channel.name)
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
_ = resp.Body.Close()
cancel()
a.log.Info("guest_requester_request", "channel", channel.name, "method", r.Method, "path", r.URL.Path, "status", resp.StatusCode)
return
}
http.Error(w, "no peer transport reached the requester: "+strings.Join(failures, "; "), http.StatusBadGateway)
}
func (a *Agent) requesterOffer(seq int64, ciphertext string) {
if a.requesterPeer == nil {
a.log.Debug("a requester offer for a guest with no requester WebRTC channel", "seq", seq)
return
}
if ciphertext == "" || len(ciphertext) > maxCiphertext {
return
}
plaintext, err := open(a.identity, ciphertext)
if err != nil {
a.log.Debug("a requester offer does not open with this guest's identity", "seq", seq)
return
}
var offer requesterOfferPlaintext
if err := json.Unmarshal(plaintext, &offer); err != nil || offer.SDP == "" {
a.log.Warn("a requester offer opened but is not {sdp, replyTo, id, session, peer}", "seq", seq)
return
}
replyTo, err := age.ParseX25519Recipient(strings.TrimSpace(offer.ReplyTo))
if err != nil {
a.log.Warn("a requester offer names no age X25519 replyTo", "seq", seq)
return
}
a.wg.Go(func() { a.answerRequester(offer, replyTo) })
}
func (a *Agent) answerRequester(offer requesterOfferPlaintext, replyTo *age.X25519Recipient) {
ctx, cancel := context.WithTimeout(a.ctx, sessionConnectTimeout)
defer cancel()
answer, err := a.requesterPeer.Answer(ctx, tunnel.Offer{Kind: tunnel.KindWebRTC, ID: offer.ID, Session: offer.Session, Peer: offer.Peer,
Type: tunnel.SignalOffer, SDP: offer.SDP})
if err != nil {
a.log.Warn("guest_requester_answer_failed", "session", offer.Session, "err", err.Error())
return
}
plaintext, err := json.Marshal(answer)
if err != nil {
return
}
sealed, err := seal(replyTo, plaintext)
if err != nil || len(sealed) > maxCiphertext {
a.log.Warn("guest_requester_answer_failed", "session", offer.Session, "err", "sealing the answer", "bytes", len(sealed))
return
}
record := signalRecord{Type: SignalWebRTCNSID, Kind: signalKindAnswer, Tag: requesterTag, Ciphertext: sealed, CreatedAt: createdAt()}
if err := a.poster.Post(ctx, record.Type, requesterTag, record); err != nil {
a.log.Warn("guest_requester_answer_failed", "session", offer.Session, "err", err.Error())
return
}
a.log.Info("guest_requester_answered", "session", offer.Session)
}