MCPcopy Index your code
hub / github.com/tailscale/tailscale / ServeHTTP

Method ServeHTTP

cmd/hello/helloserver/helloserver.go:115–151  ·  view source on GitHub ↗

ServeHTTP implements http.Handler.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

113
114// ServeHTTP implements http.Handler.
115func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
116 if r.TLS == nil && s.HTTPSAddr != "" {
117 host := r.Host
118 if strings.Contains(r.Host, "100.101.102.103") {
119 host = "hello.ts.net"
120 }
121 http.Redirect(w, r, "https://"+host, http.StatusFound)
122 return
123 }
124
125 if strings.HasPrefix(r.RequestURI, "/static/") {
126 staticHandler.ServeHTTP(w, r)
127 return
128 }
129
130 if r.RequestURI != "/" {
131 http.Redirect(w, r, "/", http.StatusFound)
132 return
133 }
134
135 who, err := s.localClient().WhoIs(r.Context(), r.RemoteAddr)
136 if err != nil {
137 log.Printf("whois(%q) error: %v", r.RemoteAddr, err)
138 http.Error(w, "Your Tailscale works, but we failed to look you up.", 500)
139 return
140 }
141 data := tmplData{
142 DisplayName: who.UserProfile.DisplayName,
143 LoginName: who.UserProfile.LoginName,
144 ProfilePicURL: who.UserProfile.ProfilePicURL,
145 MachineName: firstLabel(who.Node.ComputedName),
146 MachineOS: who.Node.Hostinfo.OS(),
147 IP: tailscaleIP(who),
148 }
149 w.Header().Set("Content-Type", "text/html; charset=utf-8")
150 tmpl.Execute(w, data)
151}
152
153// firstLabel returns s up until the first period, if any.
154func firstLabel(s string) string {

Callers

nothing calls this directly

Calls 13

localClientMethod · 0.95
tailscaleIPFunction · 0.85
RedirectMethod · 0.80
PrintfMethod · 0.80
OSMethod · 0.80
ExecuteMethod · 0.80
firstLabelFunction · 0.70
WhoIsMethod · 0.65
ContextMethod · 0.65
ErrorMethod · 0.65
SetMethod · 0.65
ContainsMethod · 0.45

Tested by

no test coverage detected