-
Notifications
You must be signed in to change notification settings - Fork 0
/
dokodemo.go
110 lines (91 loc) · 2.77 KB
/
dokodemo.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
99
100
101
102
103
104
105
106
107
108
109
110
package dokodemo
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg dokodemo -path Proxy,Dokodemo
import (
"context"
"runtime"
"time"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/signal"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
)
type DokodemoDoor struct {
config *Config
address net.Address
port net.Port
}
func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, newError("no space in context")
}
if config.NetworkList == nil || config.NetworkList.Size() == 0 {
return nil, newError("no network specified")
}
d := &DokodemoDoor{
config: config,
address: config.GetPredefinedAddress(),
port: net.Port(config.Port),
}
return d, nil
}
func (d *DokodemoDoor) Network() net.NetworkList {
return *(d.config.NetworkList)
}
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
log.Trace(newError("processing connection from: ", conn.RemoteAddr()).AtDebug())
dest := net.Destination{
Network: network,
Address: d.address,
Port: d.port,
}
if d.config.FollowRedirect {
if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
dest = origDest
}
}
if !dest.IsValid() || dest.Address == nil {
return newError("unable to get destination")
}
timeout := time.Second * time.Duration(d.config.Timeout)
if timeout == 0 {
timeout = time.Minute * 2
}
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
inboundRay, err := dispatcher.Dispatch(ctx, dest)
if err != nil {
return newError("failed to dispatch request").Base(err)
}
requestDone := signal.ExecuteAsync(func() error {
defer inboundRay.InboundInput().Close()
chunkReader := buf.NewReader(conn)
if err := buf.Copy(timer, chunkReader, inboundRay.InboundInput()); err != nil {
return newError("failed to transport request").Base(err)
}
return nil
})
responseDone := signal.ExecuteAsync(func() error {
v2writer := buf.NewWriter(conn)
if err := buf.Copy(timer, inboundRay.InboundOutput(), v2writer); err != nil {
return newError("failed to transport response").Base(err)
}
return nil
})
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
inboundRay.InboundInput().CloseError()
inboundRay.InboundOutput().CloseError()
return newError("connection ends").Base(err)
}
runtime.KeepAlive(timer)
return nil
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
}