From 56c53909aa5181d6b5c3e929f4c9a6e2ed3150a1 Mon Sep 17 00:00:00 2001 From: kingjcy Date: Wed, 20 Nov 2019 20:56:55 +0800 Subject: [PATCH 1/2] plugin http2https plugin http2https --- models/plugin/http2https.go | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 models/plugin/http2https.go diff --git a/models/plugin/http2https.go b/models/plugin/http2https.go new file mode 100644 index 00000000..04871c97 --- /dev/null +++ b/models/plugin/http2https.go @@ -0,0 +1,109 @@ +// Copyright 2019 fatedier, fatedier@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package plugin + +import ( + "crypto/tls" + "fmt" + "io" + "net" + "net/http" + "net/http/httputil" + "strings" + + frpNet "github.com/fatedier/frp/utils/net" +) + +const PluginHTTP2HTTPS = "http2https" + +func init() { + Register(PluginHTTP2HTTPS, NewHTTP2HTTPSPlugin) +} + +type HTTP2HTTPSPlugin struct { + hostHeaderRewrite string + localAddr string + headers map[string]string + + l *Listener + s *http.Server +} + +func NewHTTP2HTTPSPlugin(params map[string]string) (Plugin, error) { + localAddr := params["plugin_local_addr"] + hostHeaderRewrite := params["plugin_host_header_rewrite"] + headers := make(map[string]string) + for k, v := range params { + if !strings.HasPrefix(k, "plugin_header_") { + continue + } + if k = strings.TrimPrefix(k, "plugin_header_"); k != "" { + headers[k] = v + } + } + + if localAddr == "" { + return nil, fmt.Errorf("plugin_local_addr is required") + } + + listener := NewProxyListener() + + p := &HTTPS2HTTPPlugin{ + localAddr: localAddr, + hostHeaderRewrite: hostHeaderRewrite, + headers: headers, + l: listener, + } + + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + + rp := &httputil.ReverseProxy{ + Director: func(req *http.Request) { + req.URL.Scheme = "https" + req.URL.Host = p.localAddr + if p.hostHeaderRewrite != "" { + req.Host = p.hostHeaderRewrite + } + for k, v := range p.headers { + req.Header.Set(k, v) + } + }, + Transport: tr, + } + + p.s = &http.Server{ + Handler: rp, + } + + go p.s.Serve(listener) + + return p, nil +} + + +func (p *HTTP2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) { + wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn) + p.l.PutConn(wrapConn) +} + +func (p *HTTP2HTTPSPlugin) Name() string { + return PluginHTTP2HTTPS +} + +func (p *HTTP2HTTPSPlugin) Close() error { + return nil +} From 62af5c8844f2436d6366fbf6192620abc52e99be Mon Sep 17 00:00:00 2001 From: kingjcy Date: Fri, 22 Nov 2019 15:18:20 +0800 Subject: [PATCH 2/2] handle close --- models/plugin/http2https.go | 3 +++ models/plugin/https2http.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/models/plugin/http2https.go b/models/plugin/http2https.go index 04871c97..6f5965e6 100644 --- a/models/plugin/http2https.go +++ b/models/plugin/http2https.go @@ -105,5 +105,8 @@ func (p *HTTP2HTTPSPlugin) Name() string { } func (p *HTTP2HTTPSPlugin) Close() error { + if err := p.s.Close();err != nil { + return err + } return nil } diff --git a/models/plugin/https2http.go b/models/plugin/https2http.go index 65540356..af5b6af9 100644 --- a/models/plugin/https2http.go +++ b/models/plugin/https2http.go @@ -126,5 +126,8 @@ func (p *HTTPS2HTTPPlugin) Name() string { } func (p *HTTPS2HTTPPlugin) Close() error { + if err := p.s.Close();err != nil { + return err + } return nil }