1
0

tls2raw.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2024 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. //go:build !frps
  15. package client
  16. import (
  17. "context"
  18. "crypto/tls"
  19. "net"
  20. libio "github.com/fatedier/golib/io"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. "github.com/fatedier/frp/pkg/transport"
  23. netpkg "github.com/fatedier/frp/pkg/util/net"
  24. "github.com/fatedier/frp/pkg/util/xlog"
  25. )
  26. func init() {
  27. Register(v1.PluginTLS2Raw, NewTLS2RawPlugin)
  28. }
  29. type TLS2RawPlugin struct {
  30. opts *v1.TLS2RawPluginOptions
  31. tlsConfig *tls.Config
  32. }
  33. func NewTLS2RawPlugin(_ PluginContext, options v1.ClientPluginOptions) (Plugin, error) {
  34. opts := options.(*v1.TLS2RawPluginOptions)
  35. p := &TLS2RawPlugin{
  36. opts: opts,
  37. }
  38. tlsConfig, err := transport.NewServerTLSConfig(p.opts.CrtPath, p.opts.KeyPath, "")
  39. if err != nil {
  40. return nil, err
  41. }
  42. p.tlsConfig = tlsConfig
  43. return p, nil
  44. }
  45. func (p *TLS2RawPlugin) Handle(ctx context.Context, connInfo *ConnectionInfo) {
  46. xl := xlog.FromContextSafe(ctx)
  47. wrapConn := netpkg.WrapReadWriteCloserToConn(connInfo.Conn, connInfo.UnderlyingConn)
  48. tlsConn := tls.Server(wrapConn, p.tlsConfig)
  49. if err := tlsConn.Handshake(); err != nil {
  50. xl.Warnf("tls handshake error: %v", err)
  51. return
  52. }
  53. rawConn, err := net.Dial("tcp", p.opts.LocalAddr)
  54. if err != nil {
  55. xl.Warnf("dial to local addr error: %v", err)
  56. return
  57. }
  58. libio.Join(tlsConn, rawConn)
  59. }
  60. func (p *TLS2RawPlugin) Name() string {
  61. return v1.PluginTLS2Raw
  62. }
  63. func (p *TLS2RawPlugin) Close() error {
  64. return nil
  65. }