askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
pairing.go · 2.3 KB · 89 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "crypto/hmac" |
| 5 | "crypto/rand" |
| 6 | "crypto/sha256" |
| 7 | "crypto/subtle" |
| 8 | "encoding/base64" |
| 9 | "encoding/hex" |
| 10 | "fmt" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strconv" |
| 14 | "strings" |
| 15 | "time" |
| 16 | ) |
| 17 | |
| 18 | // pairing lets a client type the PIN once. After a correct PIN the host hands |
| 19 | // back a token signed with a secret kept on disk; the client stores it and |
| 20 | // presents it instead of the PIN from then on. The secret persists so pairings |
| 21 | // survive restarts; rotating it (-reset-pairings) revokes every pairing. |
| 22 | type pairing struct { |
| 23 | host string |
| 24 | secret []byte |
| 25 | } |
| 26 | |
| 27 | const pairTTL = 90 * 24 * time.Hour |
| 28 | |
| 29 | func loadPairing(host, dir string, reset bool) (*pairing, error) { |
| 30 | path := filepath.Join(dir, "pair.key") |
| 31 | if !reset { |
| 32 | if b, err := os.ReadFile(path); err == nil { |
| 33 | if sec, err := hex.DecodeString(strings.TrimSpace(string(b))); err == nil && len(sec) >= 32 { |
| 34 | return &pairing{host: host, secret: sec}, nil |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | sec := make([]byte, 32) |
| 39 | if _, err := rand.Read(sec); err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | if err := os.MkdirAll(dir, 0o700); err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | if err := os.WriteFile(path, []byte(hex.EncodeToString(sec)+"\n"), 0o600); err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | return &pairing{host: host, secret: sec}, nil |
| 49 | } |
| 50 | |
| 51 | func (p *pairing) sign(payload string) string { |
| 52 | m := hmac.New(sha256.New, p.secret) |
| 53 | m.Write([]byte(payload)) |
| 54 | return base64.RawURLEncoding.EncodeToString(m.Sum(nil)) |
| 55 | } |
| 56 | |
| 57 | // mint issues a token bound to this host name, valid for pairTTL. |
| 58 | func (p *pairing) mint() string { |
| 59 | payload := fmt.Sprintf("pair|%s|%d", p.host, time.Now().Add(pairTTL).Unix()) |
| 60 | return base64.RawURLEncoding.EncodeToString([]byte(payload)) + "." + p.sign(payload) |
| 61 | } |
| 62 | |
| 63 | func (p *pairing) valid(tok string) bool { |
| 64 | dot := strings.LastIndex(tok, ".") |
| 65 | if dot < 0 { |
| 66 | return false |
| 67 | } |
| 68 | raw, err := base64.RawURLEncoding.DecodeString(tok[:dot]) |
| 69 | if err != nil { |
| 70 | return false |
| 71 | } |
| 72 | payload := string(raw) |
| 73 | if subtle.ConstantTimeCompare([]byte(p.sign(payload)), []byte(tok[dot+1:])) != 1 { |
| 74 | return false |
| 75 | } |
| 76 | f := strings.Split(payload, "|") |
| 77 | if len(f) != 3 || f[0] != "pair" || f[1] != p.host { |
| 78 | return false |
| 79 | } |
| 80 | exp, err := strconv.ParseInt(f[2], 10, 64) |
| 81 | return err == nil && time.Now().Unix() <= exp |
| 82 | } |
| 83 | |
| 84 | func defaultHostStateDir() string { |
| 85 | if d, err := os.UserHomeDir(); err == nil && d != "" && d != "/" { |
| 86 | return filepath.Join(d, ".local", "state", "nc-host") |
| 87 | } |
| 88 | return "/var/lib/nc-host" |
| 89 | } |