|
| 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 | +typeApplicationstruct { |
| 20 | +AppURLstring |
| 21 | +AppNamestring |
| 22 | +Workspacestring |
| 23 | +Agentstring |
| 24 | +Userstring |
| 25 | +Pathstring |
| 26 | + |
| 27 | +// Domain is used to output the url to reach the app. |
| 28 | +Domainstring |
| 29 | +} |
| 30 | + |
| 31 | +func (api*API)handleSubdomain(next http.Handler) http.Handler { |
| 32 | +returnhttp.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 | +funcParseSubdomainAppURL(r*http.Request) (Application,error) { |
| 46 | +host:=RequestHost(r) |
| 47 | +ifhost=="" { |
| 48 | +returnApplication{},xerrors.Errorf("no host header") |
| 49 | +} |
| 50 | + |
| 51 | +subdomain,domain,err:=SplitSubdomain(host) |
| 52 | +iferr!=nil { |
| 53 | +returnApplication{},xerrors.Errorf("split host domain: %w",err) |
| 54 | +} |
| 55 | + |
| 56 | +matches:=appURL.FindAllStringSubmatch(subdomain,-1) |
| 57 | +iflen(matches)==0 { |
| 58 | +returnApplication{},xerrors.Errorf("invalid application url format: %q",subdomain) |
| 59 | +} |
| 60 | + |
| 61 | +iflen(matches)>1 { |
| 62 | +returnApplication{},xerrors.Errorf("multiple matches (%d) for application url: %q",len(matches),subdomain) |
| 63 | +} |
| 64 | +matchGroup:=matches[0] |
| 65 | + |
| 66 | +returnApplication{ |
| 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 | +funcRequestHost(r*http.Request)string { |
| 95 | +host:=r.Header.Get(XForwardedHostHeader) |
| 96 | +ifhost!="" { |
| 97 | +returnhost |
| 98 | +} |
| 99 | + |
| 100 | +returnr.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 | +funcSplitSubdomain(hostnamestring) (string,string,error) { |
| 109 | +toks:=strings.SplitN(hostname,".",2) |
| 110 | +iflen(toks)<2 { |
| 111 | +return"","",xerrors.Errorf("no domain") |
| 112 | +} |
| 113 | + |
| 114 | +returntoks[0],toks[1],nil |
| 115 | +} |