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.
|
|
|
|
|
2023-11-21 11:19:35 +08:00
|
|
|
//go:build !frps
|
|
|
|
|
2019-11-20 20:56:55 +08:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-05-29 14:10:34 +08:00
|
|
|
utilnet "github.com/fatedier/frp/pkg/util/net"
|
2019-11-20 20:56:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-09-06 10:18:02 +08:00
|
|
|
Register(v1.PluginHTTP2HTTPS, NewHTTP2HTTPSPlugin)
|
2019-11-20 20:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTP2HTTPSPlugin struct {
|
2023-09-06 10:18:02 +08:00
|
|
|
opts *v1.HTTP2HTTPSPluginOptions
|
2019-11-20 20:56:55 +08:00
|
|
|
|
|
|
|
l *Listener
|
|
|
|
s *http.Server
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func NewHTTP2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) {
|
|
|
|
opts := options.(*v1.HTTP2HTTPSPluginOptions)
|
2019-11-20 20:56:55 +08:00
|
|
|
|
|
|
|
listener := NewProxyListener()
|
|
|
|
|
2021-06-24 16:33:52 +08:00
|
|
|
p := &HTTP2HTTPSPlugin{
|
2023-09-06 10:18:02 +08:00
|
|
|
opts: opts,
|
|
|
|
l: listener,
|
2019-11-20 20:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
rp := &httputil.ReverseProxy{
|
|
|
|
Director: func(req *http.Request) {
|
|
|
|
req.URL.Scheme = "https"
|
2023-09-06 10:18:02 +08:00
|
|
|
req.URL.Host = p.opts.LocalAddr
|
|
|
|
if p.opts.HostHeaderRewrite != "" {
|
|
|
|
req.Host = p.opts.HostHeaderRewrite
|
2019-11-20 20:56:55 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
for k, v := range p.opts.RequestHeaders.Set {
|
2019-11-20 20:56:55 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-20 15:18:50 +08:00
|
|
|
func (p *HTTP2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
|
2023-05-29 14:10:34 +08:00
|
|
|
wrapConn := utilnet.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 {
|
2023-09-06 10:18:02 +08:00
|
|
|
return v1.PluginHTTP2HTTPS
|
2019-11-20 20:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTP2HTTPSPlugin) Close() error {
|
2023-07-21 10:30:46 +08:00
|
|
|
return p.s.Close()
|
2019-11-20 20:56:55 +08:00
|
|
|
}
|