unix_domain_socket.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "fmt"
  17. "io"
  18. "net"
  19. frpIo "github.com/fatedier/frp/utils/io"
  20. frpNet "github.com/fatedier/frp/utils/net"
  21. )
  22. const PluginUnixDomainSocket = "unix_domain_socket"
  23. func init() {
  24. Register(PluginUnixDomainSocket, NewUnixDomainSocketPlugin)
  25. }
  26. type UnixDomainSocketPlugin struct {
  27. UnixAddr *net.UnixAddr
  28. }
  29. func NewUnixDomainSocketPlugin(params map[string]string) (p Plugin, err error) {
  30. unixPath, ok := params["plugin_unix_path"]
  31. if !ok {
  32. err = fmt.Errorf("plugin_unix_path not found")
  33. return
  34. }
  35. unixAddr, errRet := net.ResolveUnixAddr("unix", unixPath)
  36. if errRet != nil {
  37. err = errRet
  38. return
  39. }
  40. p = &UnixDomainSocketPlugin{
  41. UnixAddr: unixAddr,
  42. }
  43. return
  44. }
  45. func (uds *UnixDomainSocketPlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) {
  46. localConn, err := net.DialUnix("unix", nil, uds.UnixAddr)
  47. if err != nil {
  48. return
  49. }
  50. frpIo.Join(localConn, conn)
  51. }
  52. func (uds *UnixDomainSocketPlugin) Name() string {
  53. return PluginUnixDomainSocket
  54. }
  55. func (uds *UnixDomainSocketPlugin) Close() error {
  56. return nil
  57. }