MCPcopy
hub / github.com/XTLS/Xray-core / Init

Method Init

proxy/tun/handler.go:46–101  ·  view source on GitHub ↗

Init the Handler instance with necessary parameters

(ctx context.Context, pm policy.Manager, dispatcher routing.Dispatcher)

Source from the content-addressed store, hash-verified

44
45// Init the Handler instance with necessary parameters
46func (t *Handler) Init(ctx context.Context, pm policy.Manager, dispatcher routing.Dispatcher) error {
47 var err error
48
49 // Retrieve tag and sniffing config from context (set by AlwaysOnInboundHandler)
50 if inbound := session.InboundFromContext(ctx); inbound != nil {
51 t.tag = inbound.Tag
52 }
53 if content := session.ContentFromContext(ctx); content != nil {
54 t.sniffingRequest = content.SniffingRequest
55 }
56
57 t.ctx = core.ToBackgroundDetachedContext(ctx)
58 t.policyManager = pm
59 t.dispatcher = dispatcher
60
61 tunName := t.config.Name
62 tunOptions := TunOptions{
63 Name: tunName,
64 MTU: t.config.MTU,
65 }
66 tunInterface, err := NewTun(tunOptions)
67 if err != nil {
68 return err
69 }
70
71 errors.LogInfo(t.ctx, tunName, " created")
72
73 tunStackOptions := StackOptions{
74 Tun: tunInterface,
75 IdleTimeout: pm.ForLevel(t.config.UserLevel).Timeouts.ConnectionIdle,
76 }
77 tunStack, err := NewStack(t.ctx, tunStackOptions, t)
78 if err != nil {
79 _ = tunInterface.Close()
80 return err
81 }
82
83 err = tunStack.Start()
84 if err != nil {
85 _ = tunStack.Close()
86 _ = tunInterface.Close()
87 return err
88 }
89
90 err = tunInterface.Start()
91 if err != nil {
92 _ = tunStack.Close()
93 _ = tunInterface.Close()
94 return err
95 }
96
97 t.stack = tunStack
98
99 errors.LogInfo(t.ctx, tunName, " up")
100 return nil
101}
102
103// HandleConnection pass the connection coming from the ip stack to the routing dispatcher

Callers 1

initFunction · 0.95

Calls 11

CloseMethod · 0.95
StartMethod · 0.95
CloseMethod · 0.95
StartMethod · 0.95
InboundFromContextFunction · 0.92
ContentFromContextFunction · 0.92
LogInfoFunction · 0.92
NewStackFunction · 0.85
NewTunFunction · 0.70
ForLevelMethod · 0.65

Tested by

no test coverage detected