1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 10:56:06 -05:00
v2fly/app/proxyman/inbound/unix.go

101 lines
2.4 KiB
Go
Raw Normal View History

2018-03-11 00:44:21 -05:00
package inbound
import (
"context"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/mux"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet/domainsocket"
)
type UnixInboundHandler struct {
tag string
listenerHolder *domainsocket.Listener
ctx context.Context
path string
proxy proxy.Inbound
mux *mux.Server
2018-03-28 08:09:43 -04:00
additional *proxyman.UnixReceiverConfig
2018-03-11 00:44:21 -05:00
}
2018-04-04 05:12:09 -04:00
func (uih *UnixInboundHandler) Start() error {
2018-03-11 00:44:21 -05:00
var err error
uih.listenerHolder, err = domainsocket.ListenDS(uih.ctx, uih.path)
2018-03-16 00:35:12 -04:00
if err != nil {
2018-04-04 05:12:09 -04:00
return newError(err).AtError()
2018-03-16 00:35:12 -04:00
}
2018-03-18 10:08:03 -04:00
err = uih.listenerHolder.LowerUP()
if err != nil {
2018-04-04 05:12:09 -04:00
return newError(err).AtError()
2018-03-18 10:08:03 -04:00
}
nchan := make(chan net.Conn, 2)
err = uih.listenerHolder.UP(nchan, false)
if err != nil {
2018-04-04 05:12:09 -04:00
return newError(err).AtError()
2018-03-18 10:08:03 -04:00
}
go uih.progressTraffic(nchan)
2018-04-04 05:12:09 -04:00
return nil
2018-03-18 10:08:03 -04:00
}
func (uih *UnixInboundHandler) progressTraffic(rece <-chan net.Conn) {
2018-03-18 10:13:17 -04:00
2018-03-18 10:08:03 -04:00
for {
2018-03-18 10:13:17 -04:00
conn, notclosed := <-rece
if !notclosed {
return
}
2018-03-18 10:08:03 -04:00
go func(conn net.Conn) {
ctx, cancel := context.WithCancel(uih.ctx)
if len(uih.tag) > 0 {
ctx = proxy.ContextWithInboundTag(ctx, uih.tag)
}
if err := uih.proxy.Process(ctx, net.Network_TCP, conn, uih.mux); err != nil {
newError("connection ends").Base(err).WriteToLog()
}
cancel()
conn.Close()
}(conn)
}
2018-03-11 00:44:21 -05:00
}
2018-04-04 05:12:09 -04:00
func (uih *UnixInboundHandler) Close() error {
2018-03-11 00:44:21 -05:00
if uih.listenerHolder != nil {
uih.listenerHolder.Down()
2018-04-04 05:12:09 -04:00
return nil
2018-03-11 00:44:21 -05:00
}
2018-04-04 05:12:09 -04:00
return newError("Called UnixInboundHandler.Close while listenerHolder is nil")
2018-03-11 00:44:21 -05:00
}
func (uih *UnixInboundHandler) Tag() string {
return uih.tag
}
func (uih *UnixInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
//It makes bo sense to support it
return nil, 0, 0
}
func NewUnixInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.UnixReceiverConfig, proxyConfig interface{}) (*UnixInboundHandler, error) {
rawProxy, err := common.CreateObject(ctx, proxyConfig)
if err != nil {
return nil, err
}
p, ok := rawProxy.(proxy.Inbound)
if !ok {
return nil, newError("not an inbound proxy.")
}
h := &UnixInboundHandler{
2018-03-28 08:09:43 -04:00
proxy: p,
mux: mux.NewServer(ctx),
tag: tag,
ctx: ctx,
path: receiverConfig.DomainSockSettings.GetPath(),
additional: receiverConfig,
2018-03-11 00:44:21 -05:00
}
return h, nil
}