|
| 1 | +package coderd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "golang.org/x/xerrors" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + // XForwardedHostHeader is a header used by proxies to indicate the |
| 14 | + // original host of the request. |
| 15 | + XForwardedHostHeader = "X-Forwarded-Host" |
| 16 | + xForwardedProto = "X-Forwarded-Proto" |
| 17 | +) |
| 18 | + |
| 19 | +type Application struct { |
| 20 | + AppURL string |
| 21 | + AppName string |
| 22 | + Workspace string |
| 23 | + Agent string |
| 24 | + User string |
| 25 | + Path string |
| 26 | + |
| 27 | + // Domain is used to output the url to reach the app. |
| 28 | + Domain string |
| 29 | +} |
| 30 | + |
| 31 | +func (api *API) handleSubdomain(next http.Handler) http.Handler { |
| 32 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | + |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +var ( |
| 38 | + nameRegex = `[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*` |
| 39 | + appURL = regexp.MustCompile(fmt.Sprintf( |
| 40 | + // {USERNAME}--{WORKSPACE_NAME}}--{{AGENT_NAME}}--{{PORT}} |
| 41 | + `^(?P<UserName>%[1]s)--(?P<WorkspaceName>%[1]s)(--(?P<AgentName>%[1]s))?--(?P<AppName>%[1]s)$`, |
| 42 | + nameRegex)) |
| 43 | +) |
| 44 | + |
| 45 | +func ParseSubdomainAppURL(r *http.Request) (Application, error) { |
| 46 | + host := RequestHost(r) |
| 47 | + if host == "" { |
| 48 | + return Application{}, xerrors.Errorf("no host header") |
| 49 | + } |
| 50 | + |
| 51 | + subdomain, domain, err := SplitSubdomain(host) |
| 52 | + if err != nil { |
| 53 | + return Application{}, xerrors.Errorf("split host domain: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + matches := appURL.FindAllStringSubmatch(subdomain, -1) |
| 57 | + if len(matches) == 0 { |
| 58 | + return Application{}, xerrors.Errorf("invalid application url format: %q", subdomain) |
| 59 | + } |
| 60 | + |
| 61 | + if len(matches) > 1 { |
| 62 | + return Application{}, xerrors.Errorf("multiple matches (%d) for application url: %q", len(matches), subdomain) |
| 63 | + } |
| 64 | + matchGroup := matches[0] |
| 65 | + |
| 66 | + return Application{ |
| 67 | + AppURL: "", |
| 68 | + AppName: matchGroup[appURL.SubexpIndex("AppName")], |
| 69 | + Workspace: matchGroup[appURL.SubexpIndex("WorkspaceName")], |
| 70 | + Agent: matchGroup[appURL.SubexpIndex("AgentName")], |
| 71 | + User: matchGroup[appURL.SubexpIndex("UserName")], |
| 72 | + Path: r.URL.Path, |
| 73 | + Domain: domain, |
| 74 | + }, nil |
| 75 | +} |
| 76 | + |
| 77 | +// Parse parses a DevURL from the subdomain of r's Host header. |
| 78 | +// If DevURL is not valid, returns a non-nil error. |
| 79 | +// |
| 80 | +// devurls can be in two forms, each field separate by 2 hypthens: |
| 81 | +// 1) port-envname-user (eg. http://8080--myenv--johndoe.cdrdeploy.c8s.io) |
| 82 | +// 2) name-user (eg. http://demosvc--johndoe.cdrdeploy.c8s.io) |
| 83 | +// |
| 84 | +// Note that envname itself can contain hyphens. |
| 85 | +// If subdomain begins with a sequence of numbers, form 1 is assumed. |
| 86 | +// Otherwise, form 2 is assumed. |
| 87 | +//func Parse(r *http.Request, devurlSuffix string) (Application, error) { |
| 88 | +// |
| 89 | +// return d, nil |
| 90 | +//} |
| 91 | + |
| 92 | +// RequestHost returns the name of the host from the request. It prioritizes |
| 93 | +// 'X-Forwarded-Host' over r.Host since most requests are being proxied. |
| 94 | +func RequestHost(r *http.Request) string { |
| 95 | + host := r.Header.Get(XForwardedHostHeader) |
| 96 | + if host != "" { |
| 97 | + return host |
| 98 | + } |
| 99 | + |
| 100 | + return r.Host |
| 101 | +} |
| 102 | + |
| 103 | +// SplitSubdomain splits a subdomain from a domain. E.g.: |
| 104 | +// - "foo.bar.com" becomes "foo", "bar.com" |
| 105 | +// - "foo.bar.baz.com" becomes "foo", "bar.baz.com" |
| 106 | +// |
| 107 | +// An error is returned if the string doesn't contain a period. |
| 108 | +func SplitSubdomain(hostname string) (string, string, error) { |
| 109 | + toks := strings.SplitN(hostname, ".", 2) |
| 110 | + if len(toks) < 2 { |
| 111 | + return "", "", xerrors.Errorf("no domain") |
| 112 | + } |
| 113 | + |
| 114 | + return toks[0], toks[1], nil |
| 115 | +} |
0 commit comments