Skip to content

Commit

Permalink
return ssl alert unrecognized_name when https domain not registered (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dong-zeyu authored Sep 18, 2023
1 parent bae0b4d commit 5c8ea51
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
12 changes: 11 additions & 1 deletion pkg/util/tcpmux/httpconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func NewHTTPConnectTCPMuxer(listener net.Listener, passthrough bool, timeout tim
ret := &HTTPConnectTCPMuxer{passthrough: passthrough}
mux, err := vhost.NewMuxer(listener, ret.getHostFromHTTPConnect, timeout)
mux.SetCheckAuthFunc(ret.auth).
SetSuccessHookFunc(ret.sendConnectResponse)
SetSuccessHookFunc(ret.sendConnectResponse).
SetFailHookFunc(vhostFailed)
ret.Muxer = mux
return ret, err
}
Expand Down Expand Up @@ -92,6 +93,15 @@ func (muxer *HTTPConnectTCPMuxer) auth(c net.Conn, username, password string, re
return false, nil
}

func vhostFailed(c net.Conn) {
res := vhost.NotFoundResponse()
if res.Body != nil {
defer res.Body.Close()
}
_ = res.Write(c)
_ = c.Close()
}

func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) {
reqInfoMap := make(map[string]string, 0)
sc, rd := libnet.NewSharedConn(c)
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/vhost/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (rp *HTTPReverseProxy) connectHandler(rw http.ResponseWriter, req *http.Req

remote, err := rp.CreateConnection(req.Context().Value(RouteInfoKey).(*RequestRouteInfo), false)
if err != nil {
_ = notFoundResponse().Write(client)
_ = NotFoundResponse().Write(client)
client.Close()
return
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/util/vhost/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type HTTPSMuxer struct {

func NewHTTPSMuxer(listener net.Listener, timeout time.Duration) (*HTTPSMuxer, error) {
mux, err := NewMuxer(listener, GetHTTPSHostname, timeout)
mux.SetFailHookFunc(vhostFailed)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -69,6 +70,12 @@ func readClientHello(reader io.Reader) (*tls.ClientHelloInfo, error) {
return hello, nil
}

func vhostFailed(c net.Conn) {
// Alert with alertUnrecognizedName
_ = tls.Server(c, &tls.Config{}).Handshake()
c.Close()
}

type readOnlyConn struct {
reader io.Reader
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/vhost/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func getNotFoundPageContent() []byte {
return buf
}

func notFoundResponse() *http.Response {
func NotFoundResponse() *http.Response {
header := make(http.Header)
header.Set("server", "frp/"+version.Full())
header.Set("Content-Type", "text/html")
Expand Down
14 changes: 8 additions & 6 deletions pkg/util/vhost/vhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type (
authFunc func(conn net.Conn, username, password string, reqInfoMap map[string]string) (bool, error)
hostRewriteFunc func(net.Conn, string) (net.Conn, error)
successHookFunc func(net.Conn, map[string]string) error
failHookFunc func(net.Conn)
)

// Muxer is a functional component used for https and tcpmux proxies.
Expand All @@ -58,6 +59,7 @@ type Muxer struct {
vhostFunc muxFunc
checkAuth authFunc
successHook successHookFunc
failHook failHookFunc
rewriteHost hostRewriteFunc
registryRouter *Routers
}
Expand Down Expand Up @@ -87,6 +89,11 @@ func (v *Muxer) SetSuccessHookFunc(f successHookFunc) *Muxer {
return v
}

func (v *Muxer) SetFailHookFunc(f failHookFunc) *Muxer {
v.failHook = f
return v
}

func (v *Muxer) SetRewriteHostFunc(f hostRewriteFunc) *Muxer {
v.rewriteHost = f
return v
Expand Down Expand Up @@ -206,13 +213,8 @@ func (v *Muxer) handle(c net.Conn) {
httpUser := reqInfoMap["HTTPUser"]
l, ok := v.getListener(name, path, httpUser)
if !ok {
res := notFoundResponse()
if res.Body != nil {
defer res.Body.Close()
}
_ = res.Write(c)
log.Debug("http request for host [%s] path [%s] httpUser [%s] not found", name, path, httpUser)
_ = c.Close()
v.failHook(sConn)
return
}

Expand Down

0 comments on commit 5c8ea51

Please sign in to comment.