demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 39 minutes ago
History
talkback.go · 1.5 KB · 50 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httputil" |
| 6 | "net/url" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | // talkbackPaths are all a phone may reach: what is new, the next event, and |
| 12 | // its audio. Adding speech or notices (POST) is for the desk itself only. |
| 13 | var talkbackPaths = regexp.MustCompile(`^/(health|api/voice/status|api/voice/next|audio/voice-[0-9]+-[0-9a-f]+\.wav)$`) |
| 14 | |
| 15 | // talkbackProxy lets the SRT Stream app on a phone hear and see what the desk |
| 16 | // says back to a live stream (nc-voice's talkback, on loopback) at |
| 17 | // https://<desk>/talkback/..., over the desk's own certificate. Reading only; |
| 18 | // the phone's passphrase (Authorization: Bearer) goes through and is checked |
| 19 | // by the talkback service; the desk's login cookie never does. |
| 20 | func talkbackProxy(target string) (http.Handler, error) { |
| 21 | u, err := url.Parse(target) |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | p := httputil.NewSingleHostReverseProxy(u) |
| 26 | base := p.Director |
| 27 | p.Director = func(r *http.Request) { |
| 28 | base(r) |
| 29 | r.Header.Del("Cookie") |
| 30 | r.Host = u.Host |
| 31 | } |
| 32 | p.ModifyResponse = func(res *http.Response) error { |
| 33 | res.Header.Del("Set-Cookie") |
| 34 | return nil |
| 35 | } |
| 36 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | if r.Method != http.MethodGet && r.Method != http.MethodHead { |
| 38 | http.Error(w, "read only", http.StatusMethodNotAllowed) |
| 39 | return |
| 40 | } |
| 41 | rest := strings.TrimPrefix(r.URL.Path, "/talkback") |
| 42 | if !talkbackPaths.MatchString(rest) { |
| 43 | http.NotFound(w, r) |
| 44 | return |
| 45 | } |
| 46 | r2 := r.Clone(r.Context()) |
| 47 | r2.URL.Path, r2.URL.RawPath = rest, "" |
| 48 | p.ServeHTTP(w, r2) |
| 49 | }), nil |
| 50 | } |