1
0

unix_domain_socket.go 1.8 KB

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