Skip to content

Commit

Permalink
support multilevel subdomain, fix #1132
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Mar 15, 2019
1 parent 6b61cb3 commit 9be24db
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 250 deletions.
6 changes: 6 additions & 0 deletions tests/ci/auto_test_frpc.ini
Expand Up @@ -127,6 +127,12 @@ custom_domains = test6.frp.com
host_header_rewrite = test6.frp.com
header_X-From-Where = frp

[wildcard_http]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = *.frp1.com

[subhost01]
type = http
local_ip = 127.0.0.1
Expand Down
15 changes: 15 additions & 0 deletions tests/ci/normal_test.go
Expand Up @@ -182,6 +182,21 @@ func TestHttp(t *testing.T) {
assert.Equal("true", header.Get("X-Header-Set"))
}

// wildcard_http
// test.frp1.com match *.frp1.com
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test.frp1.com", nil, "")
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}

// new.test.frp1.com also match *.frp1.com
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "new.test.frp1.com", nil, "")
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(consts.TEST_HTTP_NORMAL_STR, body)
}

// subhost01
code, body, _, err = util.SendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", consts.TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "")
if assert.NoError(err) {
Expand Down
6 changes: 4 additions & 2 deletions tests/mock/http_server.go
Expand Up @@ -88,8 +88,10 @@ func handleHttp(w http.ResponseWriter, r *http.Request) {
return
}

if strings.Contains(r.Host, "127.0.0.1") || strings.Contains(r.Host, "test2.frp.com") ||
strings.Contains(r.Host, "test5.frp.com") || strings.Contains(r.Host, "test6.frp.com") {
if strings.HasPrefix(r.Host, "127.0.0.1") || strings.HasPrefix(r.Host, "test2.frp.com") ||
strings.HasPrefix(r.Host, "test5.frp.com") || strings.HasPrefix(r.Host, "test6.frp.com") ||
strings.HasPrefix(r.Host, "test.frp1.com") || strings.HasPrefix(r.Host, "new.test.frp1.com") {

w.WriteHeader(200)
w.Write([]byte(consts.TEST_HTTP_NORMAL_STR))
} else if strings.Contains(r.Host, "test3.frp.com") {
Expand Down
235 changes: 0 additions & 235 deletions utils/vhost/http.go

This file was deleted.

22 changes: 17 additions & 5 deletions utils/vhost/newhttp.go
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -145,7 +146,7 @@ func (rp *HttpReverseProxy) CreateConnection(domain string, location string) (ne
return fn()
}
}
return nil, ErrNoDomain
return nil, fmt.Errorf("%v: %s %s", ErrNoDomain, domain, location)
}

func (rp *HttpReverseProxy) CheckAuth(domain, location, user, passwd string) bool {
Expand Down Expand Up @@ -173,11 +174,22 @@ func (rp *HttpReverseProxy) getVhost(domain string, location string) (vr *VhostR

domainSplit := strings.Split(domain, ".")
if len(domainSplit) < 3 {
return vr, false
return nil, false
}

for {
if len(domainSplit) < 3 {
return nil, false
}

domainSplit[0] = "*"
domain = strings.Join(domainSplit, ".")
vr, ok = rp.vhostRouter.Get(domain, location)
if ok {
return vr, true
}
domainSplit = domainSplit[1:]
}
domainSplit[0] = "*"
domain = strings.Join(domainSplit, ".")
vr, ok = rp.vhostRouter.Get(domain, location)
return
}

Expand Down
14 changes: 14 additions & 0 deletions utils/vhost/resource.go
Expand Up @@ -61,3 +61,17 @@ func notFoundResponse() *http.Response {
}
return res
}

func noAuthResponse() *http.Response {
header := make(map[string][]string)
header["WWW-Authenticate"] = []string{`Basic realm="Restricted"`}
res := &http.Response{
Status: "401 Not authorized",
StatusCode: 401,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: header,
}
return res
}
23 changes: 15 additions & 8 deletions utils/vhost/vhost.go
Expand Up @@ -102,17 +102,24 @@ func (v *VhostMuxer) getListener(name, path string) (l *Listener, exist bool) {

domainSplit := strings.Split(name, ".")
if len(domainSplit) < 3 {
return l, false
}
domainSplit[0] = "*"
name = strings.Join(domainSplit, ".")

vr, found = v.registryRouter.Get(name, path)
if !found {
return
}

return vr.payload.(*Listener), true
for {
if len(domainSplit) < 3 {
return
}

domainSplit[0] = "*"
name = strings.Join(domainSplit, ".")

vr, found = v.registryRouter.Get(name, path)
if found {
return vr.payload.(*Listener), true
}
domainSplit = domainSplit[1:]
}
return
}

func (v *VhostMuxer) run() {
Expand Down

0 comments on commit 9be24db

Please sign in to comment.