2019-11-20 20:56:55 +08:00
|
|
|
// 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"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
frpNet "github.com/fatedier/frp/pkg/util/net"
|
2019-11-20 20:56:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-06-24 16:33:52 +08:00
|
|
|
p := &HTTP2HTTPSPlugin{
|
2019-11-20 20:56:55 +08:00
|
|
|
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{
|
2022-08-29 01:02:53 +08:00
|
|
|
Handler: rp,
|
|
|
|
ReadHeaderTimeout: 0,
|
2019-11-20 20:56:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
go func() {
|
|
|
|
_ = p.s.Serve(listener)
|
|
|
|
}()
|
2019-11-20 20:56:55 +08:00
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTP2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) {
|
|
|
|
wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = p.l.PutConn(wrapConn)
|
2019-11-20 20:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTP2HTTPSPlugin) Name() string {
|
|
|
|
return PluginHTTP2HTTPS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTP2HTTPSPlugin) Close() error {
|
2019-12-08 21:01:58 +08:00
|
|
|
if err := p.s.Close(); err != nil {
|
2019-11-22 15:18:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-11-20 20:56:55 +08:00
|
|
|
return nil
|
|
|
|
}
|