unix_domain_socket.go 1.7 KB

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