-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
unix.go
98 lines (87 loc) · 2.27 KB
/
unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
}
func (uih *UnixInboundHandler) Start() {
var err error
uih.listenerHolder, err = domainsocket.ListenDS(uih.ctx, uih.path)
if err != nil {
newError(err).AtError().WriteToLog()
}
err = uih.listenerHolder.LowerUP()
if err != nil {
newError(err).AtError().WriteToLog()
}
nchan := make(chan net.Conn, 2)
err = uih.listenerHolder.UP(nchan, false)
if err != nil {
newError(err).AtError().WriteToLog()
}
go uih.progressTraffic(nchan)
return
}
func (uih *UnixInboundHandler) progressTraffic(rece <-chan net.Conn) {
for {
conn, notclosed := <-rece
if !notclosed {
return
}
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)
}
}
func (uih *UnixInboundHandler) Close() {
if uih.listenerHolder != nil {
uih.listenerHolder.Down()
} else {
newError("Called UnixInboundHandler.Close while listenerHolder is nil").AtDebug().WriteToLog()
}
}
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{
proxy: p,
mux: mux.NewServer(ctx),
tag: tag,
ctx: ctx,
path: receiverConfig.DomainSockSettings.GetPath(),
}
return h, nil
}