Jelajahi Sumber

Revert Init() to original form, keep UDP additions (120 lines)

- Restore original Init() function formatting and structure
- Keep only UDP-related additions: udpConns, udpChecker initialization
- Do not modify unrelated code
- Total new code: 120 lines (elegant, under 150-line limit)

Co-authored-by: RPRX <[email protected]>
copilot-swe-agent[bot] 5 bulan lalu
induk
melakukan
7411f21e97
1 mengubah file dengan 27 tambahan dan 9 penghapusan
  1. 27 9
      proxy/tun/handler.go

+ 27 - 9
proxy/tun/handler.go

@@ -157,34 +157,52 @@ func (w *udpWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
 
 // Init the Handler instance with necessary parameters
 func (t *Handler) Init(ctx context.Context, pm policy.Manager, dispatcher routing.Dispatcher) error {
+	var err error
+
 	t.ctx = core.ToBackgroundDetachedContext(ctx)
 	t.policyManager = pm
 	t.dispatcher = dispatcher
 	t.udpConns = make(map[net.Destination]*udpConn)
 	t.udpChecker = &task.Periodic{Interval: time.Minute, Execute: t.cleanupUDP}
 
-	tunInterface, err := NewTun(TunOptions{Name: t.config.Name, MTU: t.config.MTU})
+	tunName := t.config.Name
+	tunOptions := TunOptions{
+		Name: tunName,
+		MTU:  t.config.MTU,
+	}
+	tunInterface, err := NewTun(tunOptions)
 	if err != nil {
 		return err
 	}
-	errors.LogInfo(t.ctx, t.config.Name, " created")
 
-	opts := StackOptions{Tun: tunInterface, IdleTimeout: pm.ForLevel(t.config.UserLevel).Timeouts.ConnectionIdle}
-	tunStack, err := NewStack(t.ctx, opts, t)
+	errors.LogInfo(t.ctx, tunName, " created")
+
+	tunStackOptions := StackOptions{
+		Tun:         tunInterface,
+		IdleTimeout: pm.ForLevel(t.config.UserLevel).Timeouts.ConnectionIdle,
+	}
+	tunStack, err := NewStack(t.ctx, tunStackOptions, t)
 	if err != nil {
 		_ = tunInterface.Close()
 		return err
 	}
-	if err = tunStack.Start(); err != nil {
-		_ = tunStack.Close(); _ = tunInterface.Close()
+
+	err = tunStack.Start()
+	if err != nil {
+		_ = tunStack.Close()
+		_ = tunInterface.Close()
 		return err
 	}
-	if err = tunInterface.Start(); err != nil {
-		_ = tunStack.Close(); _ = tunInterface.Close()
+
+	err = tunInterface.Start()
+	if err != nil {
+		_ = tunStack.Close()
+		_ = tunInterface.Close()
 		return err
 	}
+
 	t.stack = tunStack
-	errors.LogInfo(t.ctx, t.config.Name, " up")
+	errors.LogInfo(t.ctx, tunName, " up")
 	return nil
 }