2019-04-10 06:02:22 +02: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"
|
2019-10-12 14:13:12 +02:00
|
|
|
"net"
|
2019-04-10 06:02:22 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2022-06-05 11:15:28 +02:00
|
|
|
"github.com/fatedier/frp/pkg/transport"
|
2023-05-29 08:10:34 +02:00
|
|
|
utilnet "github.com/fatedier/frp/pkg/util/net"
|
2019-04-10 06:02:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-09-06 04:18:02 +02:00
|
|
|
Register(v1.PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin)
|
2019-04-10 06:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPS2HTTPPlugin struct {
|
2023-09-06 04:18:02 +02:00
|
|
|
opts *v1.HTTPS2HTTPPluginOptions
|
2019-04-10 06:02:22 +02:00
|
|
|
|
|
|
|
l *Listener
|
|
|
|
s *http.Server
|
|
|
|
}
|
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
func NewHTTPS2HTTPPlugin(options v1.ClientPluginOptions) (Plugin, error) {
|
|
|
|
opts := options.(*v1.HTTPS2HTTPPluginOptions)
|
2019-04-10 06:02:22 +02:00
|
|
|
listener := NewProxyListener()
|
|
|
|
|
|
|
|
p := &HTTPS2HTTPPlugin{
|
2023-09-06 04:18:02 +02:00
|
|
|
opts: opts,
|
|
|
|
l: listener,
|
2019-04-10 06:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rp := &httputil.ReverseProxy{
|
|
|
|
Director: func(req *http.Request) {
|
|
|
|
req.URL.Scheme = "http"
|
2023-09-06 04:18:02 +02:00
|
|
|
req.URL.Host = p.opts.LocalAddr
|
|
|
|
if p.opts.HostHeaderRewrite != "" {
|
|
|
|
req.Host = p.opts.HostHeaderRewrite
|
2019-04-10 06:02:22 +02:00
|
|
|
}
|
2023-09-06 04:18:02 +02:00
|
|
|
for k, v := range p.opts.RequestHeaders.Set {
|
2019-08-14 14:36:10 +02:00
|
|
|
req.Header.Set(k, v)
|
2019-08-13 15:14:06 +02:00
|
|
|
}
|
2019-04-10 06:02:22 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.s = &http.Server{
|
|
|
|
Handler: rp,
|
|
|
|
}
|
|
|
|
|
2022-06-05 11:15:28 +02:00
|
|
|
var (
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
err error
|
|
|
|
)
|
2023-09-06 04:18:02 +02:00
|
|
|
if opts.CrtPath != "" || opts.KeyPath != "" {
|
2022-06-05 11:15:28 +02:00
|
|
|
tlsConfig, err = p.genTLSConfig()
|
|
|
|
} else {
|
|
|
|
tlsConfig, err = transport.NewServerTLSConfig("", "", "")
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
}
|
2019-04-10 06:02:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gen TLS config error: %v", err)
|
|
|
|
}
|
|
|
|
ln := tls.NewListener(listener, tlsConfig)
|
|
|
|
|
2022-08-28 19:02:53 +02:00
|
|
|
go func() {
|
|
|
|
_ = p.s.Serve(ln)
|
|
|
|
}()
|
2019-04-10 06:02:22 +02:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTPS2HTTPPlugin) genTLSConfig() (*tls.Config, error) {
|
2023-09-06 04:18:02 +02:00
|
|
|
cert, err := tls.LoadX509KeyPair(p.opts.CrtPath, p.opts.KeyPath)
|
2019-04-10 06:02:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &tls.Config{Certificates: []tls.Certificate{cert}}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 04:30:46 +02:00
|
|
|
func (p *HTTPS2HTTPPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, _ []byte) {
|
2023-05-29 08:10:34 +02:00
|
|
|
wrapConn := utilnet.WrapReadWriteCloserToConn(conn, realConn)
|
2022-08-28 19:02:53 +02:00
|
|
|
_ = p.l.PutConn(wrapConn)
|
2019-04-10 06:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTPS2HTTPPlugin) Name() string {
|
2023-09-06 04:18:02 +02:00
|
|
|
return v1.PluginHTTPS2HTTP
|
2019-04-10 06:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTPS2HTTPPlugin) Close() error {
|
2023-07-21 04:30:46 +02:00
|
|
|
return p.s.Close()
|
2019-04-10 06:02:22 +02:00
|
|
|
}
|