unix_domain_socket.go 1.7 KB

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