ServeHTTP implements http.Handler.
(w http.ResponseWriter, r *http.Request)
| 113 | |
| 114 | // ServeHTTP implements http.Handler. |
| 115 | func (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. |
| 154 | func firstLabel(s string) string { |
nothing calls this directly
no test coverage detected