1
0

unix_domain_socket.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  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 plugin
  16. import (
  17. "context"
  18. "io"
  19. "net"
  20. libio "github.com/fatedier/golib/io"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. "github.com/fatedier/frp/pkg/util/xlog"
  23. )
  24. func init() {
  25. Register(v1.PluginUnixDomainSocket, NewUnixDomainSocketPlugin)
  26. }
  27. type UnixDomainSocketPlugin struct {
  28. UnixAddr *net.UnixAddr
  29. }
  30. func NewUnixDomainSocketPlugin(options v1.ClientPluginOptions) (p Plugin, err error) {
  31. opts := options.(*v1.UnixDomainSocketPluginOptions)
  32. unixAddr, errRet := net.ResolveUnixAddr("unix", opts.UnixPath)
  33. if errRet != nil {
  34. err = errRet
  35. return
  36. }
  37. p = &UnixDomainSocketPlugin{
  38. UnixAddr: unixAddr,
  39. }
  40. return
  41. }
  42. func (uds *UnixDomainSocketPlugin) Handle(ctx context.Context, conn io.ReadWriteCloser, _ net.Conn, extra *ExtraInfo) {
  43. xl := xlog.FromContextSafe(ctx)
  44. localConn, err := net.DialUnix("unix", nil, uds.UnixAddr)
  45. if err != nil {
  46. xl.Warnf("dial to uds %s error: %v", uds.UnixAddr, err)
  47. return
  48. }
  49. if extra.ProxyProtocolHeader != nil {
  50. if _, err := extra.ProxyProtocolHeader.WriteTo(localConn); err != nil {
  51. return
  52. }
  53. }
  54. libio.Join(localConn, conn)
  55. }
  56. func (uds *UnixDomainSocketPlugin) Name() string {
  57. return v1.PluginUnixDomainSocket
  58. }
  59. func (uds *UnixDomainSocketPlugin) Close() error {
  60. return nil
  61. }