2021-01-27 13:14:16 +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
|
|
|
|
|
2021-01-27 13:14:16 +08:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-04-11 20:19:08 +08:00
|
|
|
stdlog "log"
|
2021-01-27 13:14:16 +08:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
2024-04-11 20:19:08 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatedier/golib/pool"
|
2021-01-27 13:14:16 +08:00
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2022-06-05 17:15:28 +08:00
|
|
|
"github.com/fatedier/frp/pkg/transport"
|
2024-04-11 20:19:08 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/log"
|
2023-11-27 15:47:49 +08:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2021-01-27 13:14:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-09-06 10:18:02 +08:00
|
|
|
Register(v1.PluginHTTPS2HTTPS, NewHTTPS2HTTPSPlugin)
|
2021-01-27 13:14:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPS2HTTPSPlugin struct {
|
2023-09-06 10:18:02 +08:00
|
|
|
opts *v1.HTTPS2HTTPSPluginOptions
|
2021-01-27 13:14:16 +08:00
|
|
|
|
|
|
|
l *Listener
|
|
|
|
s *http.Server
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func NewHTTPS2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) {
|
|
|
|
opts := options.(*v1.HTTPS2HTTPSPluginOptions)
|
2021-01-27 13:14:16 +08:00
|
|
|
|
|
|
|
listener := NewProxyListener()
|
|
|
|
|
|
|
|
p := &HTTPS2HTTPSPlugin{
|
2023-09-06 10:18:02 +08:00
|
|
|
opts: opts,
|
|
|
|
l: listener,
|
2021-01-27 13:14:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
rp := &httputil.ReverseProxy{
|
2024-02-19 21:04:29 +08:00
|
|
|
Rewrite: func(r *httputil.ProxyRequest) {
|
2024-03-28 16:47:27 +08:00
|
|
|
r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
|
|
|
|
r.SetXForwarded()
|
2024-02-19 21:04:29 +08:00
|
|
|
req := r.Out
|
2021-01-27 13:14:16 +08:00
|
|
|
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
|
2021-01-27 13:14:16 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
for k, v := range p.opts.RequestHeaders.Set {
|
2021-01-27 13:14:16 +08:00
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
},
|
2024-04-11 20:19:08 +08:00
|
|
|
Transport: tr,
|
|
|
|
BufferPool: pool.NewBuffer(32 * 1024),
|
|
|
|
ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
|
2021-01-27 13:14:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-05 17:15:28 +08:00
|
|
|
var (
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
err error
|
|
|
|
)
|
2023-09-06 10:18:02 +08:00
|
|
|
if opts.CrtPath != "" || opts.KeyPath != "" {
|
2022-06-05 17:15:28 +08:00
|
|
|
tlsConfig, err = p.genTLSConfig()
|
|
|
|
} else {
|
|
|
|
tlsConfig, err = transport.NewServerTLSConfig("", "", "")
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
}
|
2021-01-27 13:14:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gen TLS config error: %v", err)
|
|
|
|
}
|
2024-05-21 11:26:52 +08:00
|
|
|
|
|
|
|
p.s = &http.Server{
|
|
|
|
Handler: rp,
|
|
|
|
ReadHeaderTimeout: 60 * time.Second,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
}
|
2021-01-27 13:14:16 +08:00
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
go func() {
|
2024-05-21 11:26:52 +08:00
|
|
|
_ = p.s.ServeTLS(listener, "", "")
|
2022-08-29 01:02:53 +08:00
|
|
|
}()
|
2021-01-27 13:14:16 +08:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTPS2HTTPSPlugin) genTLSConfig() (*tls.Config, error) {
|
2023-09-06 10:18:02 +08:00
|
|
|
cert, err := tls.LoadX509KeyPair(p.opts.CrtPath, p.opts.KeyPath)
|
2021-01-27 13:14:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &tls.Config{Certificates: []tls.Certificate{cert}}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2024-03-28 16:47:27 +08:00
|
|
|
func (p *HTTPS2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extra *ExtraInfo) {
|
2023-11-27 15:47:49 +08:00
|
|
|
wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
|
2024-03-28 16:47:27 +08:00
|
|
|
if extra.SrcAddr != nil {
|
|
|
|
wrapConn.SetRemoteAddr(extra.SrcAddr)
|
|
|
|
}
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = p.l.PutConn(wrapConn)
|
2021-01-27 13:14:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTPS2HTTPSPlugin) Name() string {
|
2023-09-06 10:18:02 +08:00
|
|
|
return v1.PluginHTTPS2HTTPS
|
2021-01-27 13:14:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *HTTPS2HTTPSPlugin) Close() error {
|
2023-07-21 10:30:46 +08:00
|
|
|
return p.s.Close()
|
2021-01-27 13:14:16 +08:00
|
|
|
}
|