| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | //go:build !frps |
| 16 |
| 17 | package client |
| 18 |
| 19 | import ( |
| 20 | "context" |
| 21 | "crypto/tls" |
| 22 | "net" |
| 23 |
| 24 | libio "github.com/fatedier/golib/io" |
| 25 |
| 26 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 27 | "github.com/fatedier/frp/pkg/transport" |
| 28 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 29 | "github.com/fatedier/frp/pkg/util/xlog" |
| 30 | ) |
| 31 |
| 32 | func init() { |
| 33 | Register(v1.PluginTLS2Raw, NewTLS2RawPlugin) |
| 34 | } |
| 35 |
| 36 | type TLS2RawPlugin struct { |
| 37 | opts *v1.TLS2RawPluginOptions |
| 38 |
| 39 | tlsConfig *tls.Config |
| 40 | } |
| 41 |
| 42 | func NewTLS2RawPlugin(_ PluginContext, options v1.ClientPluginOptions) (Plugin, error) { |
| 43 | opts := options.(*v1.TLS2RawPluginOptions) |
| 44 |
| 45 | p := &TLS2RawPlugin{ |
| 46 | opts: opts, |
| 47 | } |
| 48 |
| 49 | tlsConfig, err := transport.NewServerTLSConfig(p.opts.CrtPath, p.opts.KeyPath, "") |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | p.tlsConfig = tlsConfig |
| 54 | return p, nil |
| 55 | } |
| 56 |
| 57 | func (p *TLS2RawPlugin) Handle(ctx context.Context, connInfo *ConnectionInfo) { |
| 58 | xl := xlog.FromContextSafe(ctx) |
| 59 |
| 60 | wrapConn := netpkg.WrapReadWriteCloserToConn(connInfo.Conn, connInfo.UnderlyingConn) |
| 61 | tlsConn := tls.Server(wrapConn, p.tlsConfig) |
| 62 |
| 63 | if err := tlsConn.Handshake(); err != nil { |
| 64 | xl.Warnf("tls handshake error: %v", err) |
| 65 | tlsConn.Close() |
| 66 | return |
| 67 | } |
| 68 | rawConn, err := net.Dial("tcp", p.opts.LocalAddr) |
| 69 | if err != nil { |
| 70 | xl.Warnf("dial to local addr error: %v", err) |
| 71 | tlsConn.Close() |
| 72 | return |
| 73 | } |
| 74 |
| 75 | if connInfo.ProxyProtocolHeader != nil { |
| 76 | if _, err := connInfo.ProxyProtocolHeader.WriteTo(rawConn); err != nil { |
| 77 | xl.Warnf("tls2raw write proxy protocol header to local conn error: %v", err) |
| 78 | rawConn.Close() |
| 79 | tlsConn.Close() |
| 80 | return |
| 81 | } |
| 82 | } |
| 83 |
| 84 | libio.Join(tlsConn, rawConn) |
| 85 | } |
| 86 |
| 87 | func (p *TLS2RawPlugin) Name() string { |
| 88 | return v1.PluginTLS2Raw |
| 89 | } |
| 90 |
| 91 | func (p *TLS2RawPlugin) Close() error { |
| 92 | return nil |
| 93 | } |