|
| 1 | +// ============================================================================== |
| 2 | +// MasterDnsVPN |
| 3 | +// Author: MasterkinG32 |
| 4 | +// Github: https://github.com/masterking32 |
| 5 | +// Year: 2026 |
| 6 | +// ============================================================================== |
| 7 | + |
| 8 | +package client |
| 9 | + |
| 10 | +import ( |
| 11 | + "sync" |
| 12 | + "time" |
| 13 | + |
| 14 | + Enums "masterdnsvpn-go/internal/enums" |
| 15 | + VpnProto "masterdnsvpn-go/internal/vpnproto" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + sessionCloseFanoutLimit = 10 |
| 20 | + sessionCloseDefaultWindow = time.Second |
| 21 | +) |
| 22 | + |
| 23 | +func (c *Client) BestEffortSessionClose(timeout time.Duration) { |
| 24 | + if c == nil || !c.sessionReady || c.sessionID == 0 { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + targets := c.activeSessionCloseTargets(sessionCloseFanoutLimit) |
| 29 | + if len(targets) == 0 { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + timeout = normalizeTimeout(timeout, sessionCloseDefaultWindow) |
| 34 | + deadline := time.Now().Add(timeout) |
| 35 | + queries := make(map[string][]byte, len(targets)) |
| 36 | + |
| 37 | + var wg sync.WaitGroup |
| 38 | + done := make(chan struct{}) |
| 39 | + for _, conn := range targets { |
| 40 | + query, ok := queries[conn.Domain] |
| 41 | + if !ok { |
| 42 | + built, err := c.buildSessionCloseQuery(conn.Domain) |
| 43 | + if err != nil { |
| 44 | + continue |
| 45 | + } |
| 46 | + query = built |
| 47 | + queries[conn.Domain] = query |
| 48 | + } |
| 49 | + |
| 50 | + connCopy := conn |
| 51 | + packetCopy := query |
| 52 | + wg.Go(func() { |
| 53 | + _ = c.sendOneWaySessionPacket(connCopy, packetCopy, deadline) |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + go func() { |
| 58 | + wg.Wait() |
| 59 | + close(done) |
| 60 | + }() |
| 61 | + |
| 62 | + timer := time.NewTimer(timeout) |
| 63 | + defer timer.Stop() |
| 64 | + |
| 65 | + select { |
| 66 | + case <-done: |
| 67 | + case <-timer.C: |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (c *Client) activeSessionCloseTargets(limit int) []Connection { |
| 72 | + if c == nil || limit <= 0 { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + limit = min(limit, sessionCloseFanoutLimit) |
| 77 | + seen := make(map[string]struct{}, limit) |
| 78 | + targets := make([]Connection, 0, limit) |
| 79 | + candidateCount := min(len(c.connections), max(limit, limit*max(1, len(c.cfg.Domains)))) |
| 80 | + |
| 81 | + for _, conn := range c.balancer.GetUniqueConnections(candidateCount) { |
| 82 | + if !conn.IsValid || conn.ResolverLabel == "" { |
| 83 | + continue |
| 84 | + } |
| 85 | + if _, ok := seen[conn.ResolverLabel]; ok { |
| 86 | + continue |
| 87 | + } |
| 88 | + seen[conn.ResolverLabel] = struct{}{} |
| 89 | + targets = append(targets, conn) |
| 90 | + if len(targets) >= limit { |
| 91 | + return targets |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + for _, conn := range c.connections { |
| 96 | + if !conn.IsValid || conn.ResolverLabel == "" { |
| 97 | + continue |
| 98 | + } |
| 99 | + if _, ok := seen[conn.ResolverLabel]; ok { |
| 100 | + continue |
| 101 | + } |
| 102 | + seen[conn.ResolverLabel] = struct{}{} |
| 103 | + targets = append(targets, conn) |
| 104 | + if len(targets) >= limit { |
| 105 | + break |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return targets |
| 110 | +} |
| 111 | + |
| 112 | +func (c *Client) buildSessionCloseQuery(domain string) ([]byte, error) { |
| 113 | + return c.buildTunnelTXTQueryRaw(domain, VpnProto.BuildOptions{ |
| 114 | + SessionID: c.sessionID, |
| 115 | + PacketType: Enums.PACKET_SESSION_CLOSE, |
| 116 | + SessionCookie: c.sessionCookie, |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func (c *Client) sendOneWaySessionPacket(connection Connection, packet []byte, deadline time.Time) error { |
| 121 | + if c != nil && c.sendOneWayPacketFn != nil { |
| 122 | + return c.sendOneWayPacketFn(connection, packet, deadline) |
| 123 | + } |
| 124 | + return sendOneWayUDPQuery(connection.ResolverLabel, packet, deadline) |
| 125 | +} |
0 commit comments