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