From 2f9920ad6de48f18f80c37198e691fbac5a229a0 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 3 Feb 2026 13:06:58 +0800 Subject: [PATCH] fix --- Tun/Punchnet/SDLMessage.pb.swift | 58 +++++++++++++++++++-- Tun/Punchnet/SDLTunnelProviderAdapter.swift | 2 +- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/Tun/Punchnet/SDLMessage.pb.swift b/Tun/Punchnet/SDLMessage.pb.swift index 9d0b6c3..0cd6b90 100644 --- a/Tun/Punchnet/SDLMessage.pb.swift +++ b/Tun/Punchnet/SDLMessage.pb.swift @@ -51,14 +51,25 @@ struct SDLV6Info: @unchecked Sendable { init() {} } -/// 客户端层面通过token或者账号密码登录, 服务器端不管这个, -/// 只是验证凭证是否合法 (access_token) +struct SDLEmpty: Sendable { + // SwiftProtobuf.Message conformance is added in an extension below. See the + // `Message` and `Message+*Additions` files in the SwiftProtobuf library for + // methods supported on all messages. + + var pktID: UInt32 = 0 + + var unknownFields = SwiftProtobuf.UnknownStorage() + + init() {} +} + /// 这里修改成了扁平的结构, 否则有些字段不好找放的位置 struct SDLRegisterSuper: @unchecked Sendable { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for // methods supported on all messages. + /// 所有需要建立请求和响应对应关系的,都是通过4字节的pktId来对应 var pktID: UInt32 = 0 var clientID: String = String() @@ -75,6 +86,8 @@ struct SDLRegisterSuper: @unchecked Sendable { var pubKey: String = String() + /// 客户端使用http协议请求后端,通过token或者账号密码登录时, 统一返回一个access_token; + /// RegisterSuper的时候,验证凭证是否合法 (access_token) var accessToken: String = String() var unknownFields = SwiftProtobuf.UnknownStorage() @@ -82,7 +95,6 @@ struct SDLRegisterSuper: @unchecked Sendable { init() {} } -/// 这里修改成了udp的方式,后续需要验证: session_token struct SDLRegisterSuperAck: @unchecked Sendable { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for @@ -92,6 +104,8 @@ struct SDLRegisterSuperAck: @unchecked Sendable { var aesKey: Data = Data() + /// 验证通过后,返回session_token; 后续通讯的合法行需哟啊通过session_token来保证 + /// 在SDLQueryInfo,SDLStunRequest, SDLData, SDLArpRequest等需要服务器端介入的地方都增加了session_token的验证(端和端之间的SDLData不需要校验) var sessionToken: Data = Data() var unknownFields = SwiftProtobuf.UnknownStorage() @@ -207,6 +221,7 @@ struct SDLNatChangedEvent: @unchecked Sendable { } /// 被清理掉的Endpoints +/// 协议改成udp后,服务端只能通过定时器来集中清理掉线的端(通过心跳机制SDLStunRequest) struct SDLDropMacsEvent: @unchecked Sendable { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for @@ -222,6 +237,7 @@ struct SDLDropMacsEvent: @unchecked Sendable { } /// 通知端上必须重新校验 +/// 服务器端认为端已经掉线了,但是这个时候还在向服务端发送消息;服务端要求端上重新校验(SDLRegisterSuper) struct SDLRefreshAuthEvent: Sendable { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for @@ -332,6 +348,8 @@ struct SDLData: @unchecked Sendable { var sessionToken: Data = Data() + /// 端通过https登录的时候,服务端会分配该端对应的权限标识 + /// 后续的请求过程中需要带上这个值,对端通过这个值要判断对数据包是否放行 var identityID: UInt32 = 0 var unknownFields = SwiftProtobuf.UnknownStorage() @@ -405,6 +423,8 @@ struct SDLStunProbeReply: Sendable { init() {} } +/// ARP查询相关 +/// 真实的arp请求是通过广播的形式获取到的,但是针对于macos这种tun的实现;是能够分析出arp请求包的;对于当前网络来说,服务端是知道mac对应的ip地址的,因此没有必要广播;直接通过服务器端返回 struct SDLArpRequest: @unchecked Sendable { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for @@ -521,6 +541,38 @@ extension SDLV6Info: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementation } } +extension SDLEmpty: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { + static let protoMessageName: String = "SDLEmpty" + static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ + 1: .standard(proto: "pkt_id"), + ] + + mutating func decodeMessage(decoder: inout D) throws { + while let fieldNumber = try decoder.nextFieldNumber() { + // The use of inline closures is to circumvent an issue where the compiler + // allocates stack space for every case branch when no optimizations are + // enabled. https://github.com/apple/swift-protobuf/issues/1034 + switch fieldNumber { + case 1: try { try decoder.decodeSingularUInt32Field(value: &self.pktID) }() + default: break + } + } + } + + func traverse(visitor: inout V) throws { + if self.pktID != 0 { + try visitor.visitSingularUInt32Field(value: self.pktID, fieldNumber: 1) + } + try unknownFields.traverse(visitor: &visitor) + } + + static func ==(lhs: SDLEmpty, rhs: SDLEmpty) -> Bool { + if lhs.pktID != rhs.pktID {return false} + if lhs.unknownFields != rhs.unknownFields {return false} + return true + } +} + extension SDLRegisterSuper: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { static let protoMessageName: String = "SDLRegisterSuper" static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ diff --git a/Tun/Punchnet/SDLTunnelProviderAdapter.swift b/Tun/Punchnet/SDLTunnelProviderAdapter.swift index 77feacb..b472f00 100644 --- a/Tun/Punchnet/SDLTunnelProviderAdapter.swift +++ b/Tun/Punchnet/SDLTunnelProviderAdapter.swift @@ -50,7 +50,7 @@ final class SDLTunnelProviderAdapter { // Add code here to start the process of connecting the tunnel. let networkSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "8.8.8.8") - networkSettings.mtu = 1460 + networkSettings.mtu = 1250 // 设置网卡的DNS解析