1
0

unix_domain_socket.go 1.7 KB

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