ipn/ipnlocal: use ServerConfig views internally

Updates tailscale/corp#7515

Change-Id: Ica2bc44b92d281d5ce16cee55b7ca51c7910145c
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
bradfitz/set_prefs_locked
Brad Fitzpatrick 2 years ago committed by Brad Fitzpatrick
parent 4de643b714
commit 4bccc02413

@ -199,7 +199,7 @@ type LocalBackend struct {
// ServeConfig fields. (also guarded by mu) // ServeConfig fields. (also guarded by mu)
lastServeConfJSON mem.RO // last JSON that was parsed into serveConfig lastServeConfJSON mem.RO // last JSON that was parsed into serveConfig
serveConfig ipn.ServeConfig // only replaced wholesale; don't mutate in-place serveConfig ipn.ServeConfigView // or !Valid if none
// statusLock must be held before calling statusChanged.Wait() or // statusLock must be held before calling statusChanged.Wait() or
// statusChanged.Broadcast(). // statusChanged.Broadcast().
@ -3514,13 +3514,18 @@ func (b *LocalBackend) setTCPPortsInterceptedFromNetmapAndPrefsLocked() {
var conf ipn.ServeConfig var conf ipn.ServeConfig
if err := json.Unmarshal(confj, &conf); err != nil { if err := json.Unmarshal(confj, &conf); err != nil {
b.logf("invalid ServeConfig %q in StateStore: %v", confKey, err) b.logf("invalid ServeConfig %q in StateStore: %v", confKey, err)
b.serveConfig = ipn.ServeConfigView{}
} else {
b.serveConfig = conf.View()
} }
b.serveConfig = conf
} }
for p := range b.serveConfig.TCP { if b.serveConfig.Valid() {
if p > 0 && p <= math.MaxUint16 { b.serveConfig.TCP().Range(func(port int, _ ipn.TCPPortHandlerView) bool {
handlePorts = append(handlePorts, uint16(p)) if port > 0 && port <= math.MaxUint16 {
handlePorts = append(handlePorts, uint16(port))
} }
return true
})
} }
} }
} }

@ -38,9 +38,11 @@ func (b *LocalBackend) HandleInterceptedTCPConn(c net.Conn) {
hs.ServeTLS(netutil.NewOneConnListener(c, nil), "", "") hs.ServeTLS(netutil.NewOneConnListener(c, nil), "", "")
} }
func (b *LocalBackend) getServeHandler(r *http.Request) (_ *ipn.HTTPHandler, ok bool) { func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, ok bool) {
var z ipn.HTTPHandlerView // zero value
if r.TLS == nil { if r.TLS == nil {
return nil, false return z, false
} }
sni := r.TLS.ServerName sni := r.TLS.ServerName
@ -50,17 +52,21 @@ func (b *LocalBackend) getServeHandler(r *http.Request) (_ *ipn.HTTPHandler, ok
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
wsc, ok := b.serveConfig.Web[key] if !b.serveConfig.Valid() {
return z, false
}
wsc, ok := b.serveConfig.Web().GetOk(key)
if !ok { if !ok {
return nil, false return z, false
} }
path := r.URL.Path path := r.URL.Path
for { for {
if h, ok := wsc.Handlers[path]; ok { if h, ok := wsc.Handlers().GetOk(path); ok {
return h, true return h, true
} }
if path == "/" { if path == "/" {
return nil, false return z, false
} }
path = pathpkg.Dir(path) path = pathpkg.Dir(path)
} }
@ -72,16 +78,16 @@ func (b *LocalBackend) serveWebHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
if s := h.Text; s != "" { if s := h.Text(); s != "" {
w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Header().Set("Content-Type", "text/plain; charset=utf-8")
io.WriteString(w, s) io.WriteString(w, s)
return return
} }
if v := h.Path; v != "" { if v := h.Path(); v != "" {
io.WriteString(w, "TODO(bradfitz): serve file") io.WriteString(w, "TODO(bradfitz): serve file")
return return
} }
if v := h.Proxy; v != "" { if v := h.Proxy(); v != "" {
io.WriteString(w, "TODO(bradfitz): proxy") io.WriteString(w, "TODO(bradfitz): proxy")
return return
} }

Loading…
Cancel
Save