diff --git a/adapter/router.go b/adapter/router.go index a0df7124d..3d5310c4e 100644 --- a/adapter/router.go +++ b/adapter/router.go @@ -25,6 +25,7 @@ type Router interface { ConnectionRouterEx RuleSet(tag string) (RuleSet, bool) Rules() []Rule + NeedFindProcess() bool AppendTracker(tracker ConnectionTracker) ResetNetwork() } diff --git a/daemon/helper.pb.go b/daemon/helper.pb.go deleted file mode 100644 index 9a2641c52..000000000 --- a/daemon/helper.pb.go +++ /dev/null @@ -1,702 +0,0 @@ -package daemon - -import ( - reflect "reflect" - sync "sync" - unsafe "unsafe" - - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - emptypb "google.golang.org/protobuf/types/known/emptypb" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type SubscribeHelperRequestRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - AcceptGetWIFIStateRequests bool `protobuf:"varint,1,opt,name=acceptGetWIFIStateRequests,proto3" json:"acceptGetWIFIStateRequests,omitempty"` - AcceptFindConnectionOwnerRequests bool `protobuf:"varint,2,opt,name=acceptFindConnectionOwnerRequests,proto3" json:"acceptFindConnectionOwnerRequests,omitempty"` - AcceptSendNotificationRequests bool `protobuf:"varint,3,opt,name=acceptSendNotificationRequests,proto3" json:"acceptSendNotificationRequests,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *SubscribeHelperRequestRequest) Reset() { - *x = SubscribeHelperRequestRequest{} - mi := &file_daemon_helper_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *SubscribeHelperRequestRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*SubscribeHelperRequestRequest) ProtoMessage() {} - -func (x *SubscribeHelperRequestRequest) ProtoReflect() protoreflect.Message { - mi := &file_daemon_helper_proto_msgTypes[0] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use SubscribeHelperRequestRequest.ProtoReflect.Descriptor instead. -func (*SubscribeHelperRequestRequest) Descriptor() ([]byte, []int) { - return file_daemon_helper_proto_rawDescGZIP(), []int{0} -} - -func (x *SubscribeHelperRequestRequest) GetAcceptGetWIFIStateRequests() bool { - if x != nil { - return x.AcceptGetWIFIStateRequests - } - return false -} - -func (x *SubscribeHelperRequestRequest) GetAcceptFindConnectionOwnerRequests() bool { - if x != nil { - return x.AcceptFindConnectionOwnerRequests - } - return false -} - -func (x *SubscribeHelperRequestRequest) GetAcceptSendNotificationRequests() bool { - if x != nil { - return x.AcceptSendNotificationRequests - } - return false -} - -type HelperRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` - // Types that are valid to be assigned to Request: - // - // *HelperRequest_GetWIFIState - // *HelperRequest_FindConnectionOwner - // *HelperRequest_SendNotification - Request isHelperRequest_Request `protobuf_oneof:"request"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *HelperRequest) Reset() { - *x = HelperRequest{} - mi := &file_daemon_helper_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *HelperRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*HelperRequest) ProtoMessage() {} - -func (x *HelperRequest) ProtoReflect() protoreflect.Message { - mi := &file_daemon_helper_proto_msgTypes[1] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use HelperRequest.ProtoReflect.Descriptor instead. -func (*HelperRequest) Descriptor() ([]byte, []int) { - return file_daemon_helper_proto_rawDescGZIP(), []int{1} -} - -func (x *HelperRequest) GetId() int64 { - if x != nil { - return x.Id - } - return 0 -} - -func (x *HelperRequest) GetRequest() isHelperRequest_Request { - if x != nil { - return x.Request - } - return nil -} - -func (x *HelperRequest) GetGetWIFIState() *emptypb.Empty { - if x != nil { - if x, ok := x.Request.(*HelperRequest_GetWIFIState); ok { - return x.GetWIFIState - } - } - return nil -} - -func (x *HelperRequest) GetFindConnectionOwner() *FindConnectionOwnerRequest { - if x != nil { - if x, ok := x.Request.(*HelperRequest_FindConnectionOwner); ok { - return x.FindConnectionOwner - } - } - return nil -} - -func (x *HelperRequest) GetSendNotification() *Notification { - if x != nil { - if x, ok := x.Request.(*HelperRequest_SendNotification); ok { - return x.SendNotification - } - } - return nil -} - -type isHelperRequest_Request interface { - isHelperRequest_Request() -} - -type HelperRequest_GetWIFIState struct { - GetWIFIState *emptypb.Empty `protobuf:"bytes,2,opt,name=getWIFIState,proto3,oneof"` -} - -type HelperRequest_FindConnectionOwner struct { - FindConnectionOwner *FindConnectionOwnerRequest `protobuf:"bytes,3,opt,name=findConnectionOwner,proto3,oneof"` -} - -type HelperRequest_SendNotification struct { - SendNotification *Notification `protobuf:"bytes,4,opt,name=sendNotification,proto3,oneof"` -} - -func (*HelperRequest_GetWIFIState) isHelperRequest_Request() {} - -func (*HelperRequest_FindConnectionOwner) isHelperRequest_Request() {} - -func (*HelperRequest_SendNotification) isHelperRequest_Request() {} - -type FindConnectionOwnerRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - IpProtocol int32 `protobuf:"varint,1,opt,name=ipProtocol,proto3" json:"ipProtocol,omitempty"` - SourceAddress string `protobuf:"bytes,2,opt,name=sourceAddress,proto3" json:"sourceAddress,omitempty"` - SourcePort int32 `protobuf:"varint,3,opt,name=sourcePort,proto3" json:"sourcePort,omitempty"` - DestinationAddress string `protobuf:"bytes,4,opt,name=destinationAddress,proto3" json:"destinationAddress,omitempty"` - DestinationPort int32 `protobuf:"varint,5,opt,name=destinationPort,proto3" json:"destinationPort,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *FindConnectionOwnerRequest) Reset() { - *x = FindConnectionOwnerRequest{} - mi := &file_daemon_helper_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *FindConnectionOwnerRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*FindConnectionOwnerRequest) ProtoMessage() {} - -func (x *FindConnectionOwnerRequest) ProtoReflect() protoreflect.Message { - mi := &file_daemon_helper_proto_msgTypes[2] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use FindConnectionOwnerRequest.ProtoReflect.Descriptor instead. -func (*FindConnectionOwnerRequest) Descriptor() ([]byte, []int) { - return file_daemon_helper_proto_rawDescGZIP(), []int{2} -} - -func (x *FindConnectionOwnerRequest) GetIpProtocol() int32 { - if x != nil { - return x.IpProtocol - } - return 0 -} - -func (x *FindConnectionOwnerRequest) GetSourceAddress() string { - if x != nil { - return x.SourceAddress - } - return "" -} - -func (x *FindConnectionOwnerRequest) GetSourcePort() int32 { - if x != nil { - return x.SourcePort - } - return 0 -} - -func (x *FindConnectionOwnerRequest) GetDestinationAddress() string { - if x != nil { - return x.DestinationAddress - } - return "" -} - -func (x *FindConnectionOwnerRequest) GetDestinationPort() int32 { - if x != nil { - return x.DestinationPort - } - return 0 -} - -type Notification struct { - state protoimpl.MessageState `protogen:"open.v1"` - Identifier string `protobuf:"bytes,1,opt,name=identifier,proto3" json:"identifier,omitempty"` - TypeName string `protobuf:"bytes,2,opt,name=typeName,proto3" json:"typeName,omitempty"` - TypeId int32 `protobuf:"varint,3,opt,name=typeId,proto3" json:"typeId,omitempty"` - Title string `protobuf:"bytes,4,opt,name=title,proto3" json:"title,omitempty"` - Subtitle string `protobuf:"bytes,5,opt,name=subtitle,proto3" json:"subtitle,omitempty"` - Body string `protobuf:"bytes,6,opt,name=body,proto3" json:"body,omitempty"` - OpenURL string `protobuf:"bytes,7,opt,name=openURL,proto3" json:"openURL,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *Notification) Reset() { - *x = Notification{} - mi := &file_daemon_helper_proto_msgTypes[3] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *Notification) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Notification) ProtoMessage() {} - -func (x *Notification) ProtoReflect() protoreflect.Message { - mi := &file_daemon_helper_proto_msgTypes[3] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Notification.ProtoReflect.Descriptor instead. -func (*Notification) Descriptor() ([]byte, []int) { - return file_daemon_helper_proto_rawDescGZIP(), []int{3} -} - -func (x *Notification) GetIdentifier() string { - if x != nil { - return x.Identifier - } - return "" -} - -func (x *Notification) GetTypeName() string { - if x != nil { - return x.TypeName - } - return "" -} - -func (x *Notification) GetTypeId() int32 { - if x != nil { - return x.TypeId - } - return 0 -} - -func (x *Notification) GetTitle() string { - if x != nil { - return x.Title - } - return "" -} - -func (x *Notification) GetSubtitle() string { - if x != nil { - return x.Subtitle - } - return "" -} - -func (x *Notification) GetBody() string { - if x != nil { - return x.Body - } - return "" -} - -func (x *Notification) GetOpenURL() string { - if x != nil { - return x.OpenURL - } - return "" -} - -type HelperResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` - // Types that are valid to be assigned to Response: - // - // *HelperResponse_WifiState - // *HelperResponse_Error - // *HelperResponse_ConnectionOwner - Response isHelperResponse_Response `protobuf_oneof:"response"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *HelperResponse) Reset() { - *x = HelperResponse{} - mi := &file_daemon_helper_proto_msgTypes[4] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *HelperResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*HelperResponse) ProtoMessage() {} - -func (x *HelperResponse) ProtoReflect() protoreflect.Message { - mi := &file_daemon_helper_proto_msgTypes[4] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use HelperResponse.ProtoReflect.Descriptor instead. -func (*HelperResponse) Descriptor() ([]byte, []int) { - return file_daemon_helper_proto_rawDescGZIP(), []int{4} -} - -func (x *HelperResponse) GetId() int64 { - if x != nil { - return x.Id - } - return 0 -} - -func (x *HelperResponse) GetResponse() isHelperResponse_Response { - if x != nil { - return x.Response - } - return nil -} - -func (x *HelperResponse) GetWifiState() *WIFIState { - if x != nil { - if x, ok := x.Response.(*HelperResponse_WifiState); ok { - return x.WifiState - } - } - return nil -} - -func (x *HelperResponse) GetError() string { - if x != nil { - if x, ok := x.Response.(*HelperResponse_Error); ok { - return x.Error - } - } - return "" -} - -func (x *HelperResponse) GetConnectionOwner() *ConnectionOwner { - if x != nil { - if x, ok := x.Response.(*HelperResponse_ConnectionOwner); ok { - return x.ConnectionOwner - } - } - return nil -} - -type isHelperResponse_Response interface { - isHelperResponse_Response() -} - -type HelperResponse_WifiState struct { - WifiState *WIFIState `protobuf:"bytes,2,opt,name=wifiState,proto3,oneof"` -} - -type HelperResponse_Error struct { - Error string `protobuf:"bytes,3,opt,name=error,proto3,oneof"` -} - -type HelperResponse_ConnectionOwner struct { - ConnectionOwner *ConnectionOwner `protobuf:"bytes,4,opt,name=connectionOwner,proto3,oneof"` -} - -func (*HelperResponse_WifiState) isHelperResponse_Response() {} - -func (*HelperResponse_Error) isHelperResponse_Response() {} - -func (*HelperResponse_ConnectionOwner) isHelperResponse_Response() {} - -type ConnectionOwner struct { - state protoimpl.MessageState `protogen:"open.v1"` - UserId int32 `protobuf:"varint,1,opt,name=userId,proto3" json:"userId,omitempty"` - UserName string `protobuf:"bytes,2,opt,name=userName,proto3" json:"userName,omitempty"` - ProcessPath string `protobuf:"bytes,3,opt,name=processPath,proto3" json:"processPath,omitempty"` - AndroidPackageName string `protobuf:"bytes,4,opt,name=androidPackageName,proto3" json:"androidPackageName,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ConnectionOwner) Reset() { - *x = ConnectionOwner{} - mi := &file_daemon_helper_proto_msgTypes[5] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ConnectionOwner) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ConnectionOwner) ProtoMessage() {} - -func (x *ConnectionOwner) ProtoReflect() protoreflect.Message { - mi := &file_daemon_helper_proto_msgTypes[5] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ConnectionOwner.ProtoReflect.Descriptor instead. -func (*ConnectionOwner) Descriptor() ([]byte, []int) { - return file_daemon_helper_proto_rawDescGZIP(), []int{5} -} - -func (x *ConnectionOwner) GetUserId() int32 { - if x != nil { - return x.UserId - } - return 0 -} - -func (x *ConnectionOwner) GetUserName() string { - if x != nil { - return x.UserName - } - return "" -} - -func (x *ConnectionOwner) GetProcessPath() string { - if x != nil { - return x.ProcessPath - } - return "" -} - -func (x *ConnectionOwner) GetAndroidPackageName() string { - if x != nil { - return x.AndroidPackageName - } - return "" -} - -type WIFIState struct { - state protoimpl.MessageState `protogen:"open.v1"` - Ssid string `protobuf:"bytes,1,opt,name=ssid,proto3" json:"ssid,omitempty"` - Bssid string `protobuf:"bytes,2,opt,name=bssid,proto3" json:"bssid,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *WIFIState) Reset() { - *x = WIFIState{} - mi := &file_daemon_helper_proto_msgTypes[6] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *WIFIState) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*WIFIState) ProtoMessage() {} - -func (x *WIFIState) ProtoReflect() protoreflect.Message { - mi := &file_daemon_helper_proto_msgTypes[6] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use WIFIState.ProtoReflect.Descriptor instead. -func (*WIFIState) Descriptor() ([]byte, []int) { - return file_daemon_helper_proto_rawDescGZIP(), []int{6} -} - -func (x *WIFIState) GetSsid() string { - if x != nil { - return x.Ssid - } - return "" -} - -func (x *WIFIState) GetBssid() string { - if x != nil { - return x.Bssid - } - return "" -} - -var File_daemon_helper_proto protoreflect.FileDescriptor - -const file_daemon_helper_proto_rawDesc = "" + - "\n" + - "\x13daemon/helper.proto\x12\x06daemon\x1a\x1bgoogle/protobuf/empty.proto\"\xf5\x01\n" + - "\x1dSubscribeHelperRequestRequest\x12>\n" + - "\x1aacceptGetWIFIStateRequests\x18\x01 \x01(\bR\x1aacceptGetWIFIStateRequests\x12L\n" + - "!acceptFindConnectionOwnerRequests\x18\x02 \x01(\bR!acceptFindConnectionOwnerRequests\x12F\n" + - "\x1eacceptSendNotificationRequests\x18\x03 \x01(\bR\x1eacceptSendNotificationRequests\"\x84\x02\n" + - "\rHelperRequest\x12\x0e\n" + - "\x02id\x18\x01 \x01(\x03R\x02id\x12<\n" + - "\fgetWIFIState\x18\x02 \x01(\v2\x16.google.protobuf.EmptyH\x00R\fgetWIFIState\x12V\n" + - "\x13findConnectionOwner\x18\x03 \x01(\v2\".daemon.FindConnectionOwnerRequestH\x00R\x13findConnectionOwner\x12B\n" + - "\x10sendNotification\x18\x04 \x01(\v2\x14.daemon.NotificationH\x00R\x10sendNotificationB\t\n" + - "\arequest\"\xdc\x01\n" + - "\x1aFindConnectionOwnerRequest\x12\x1e\n" + - "\n" + - "ipProtocol\x18\x01 \x01(\x05R\n" + - "ipProtocol\x12$\n" + - "\rsourceAddress\x18\x02 \x01(\tR\rsourceAddress\x12\x1e\n" + - "\n" + - "sourcePort\x18\x03 \x01(\x05R\n" + - "sourcePort\x12.\n" + - "\x12destinationAddress\x18\x04 \x01(\tR\x12destinationAddress\x12(\n" + - "\x0fdestinationPort\x18\x05 \x01(\x05R\x0fdestinationPort\"\xc2\x01\n" + - "\fNotification\x12\x1e\n" + - "\n" + - "identifier\x18\x01 \x01(\tR\n" + - "identifier\x12\x1a\n" + - "\btypeName\x18\x02 \x01(\tR\btypeName\x12\x16\n" + - "\x06typeId\x18\x03 \x01(\x05R\x06typeId\x12\x14\n" + - "\x05title\x18\x04 \x01(\tR\x05title\x12\x1a\n" + - "\bsubtitle\x18\x05 \x01(\tR\bsubtitle\x12\x12\n" + - "\x04body\x18\x06 \x01(\tR\x04body\x12\x18\n" + - "\aopenURL\x18\a \x01(\tR\aopenURL\"\xbc\x01\n" + - "\x0eHelperResponse\x12\x0e\n" + - "\x02id\x18\x01 \x01(\x03R\x02id\x121\n" + - "\twifiState\x18\x02 \x01(\v2\x11.daemon.WIFIStateH\x00R\twifiState\x12\x16\n" + - "\x05error\x18\x03 \x01(\tH\x00R\x05error\x12C\n" + - "\x0fconnectionOwner\x18\x04 \x01(\v2\x17.daemon.ConnectionOwnerH\x00R\x0fconnectionOwnerB\n" + - "\n" + - "\bresponse\"\x97\x01\n" + - "\x0fConnectionOwner\x12\x16\n" + - "\x06userId\x18\x01 \x01(\x05R\x06userId\x12\x1a\n" + - "\buserName\x18\x02 \x01(\tR\buserName\x12 \n" + - "\vprocessPath\x18\x03 \x01(\tR\vprocessPath\x12.\n" + - "\x12androidPackageName\x18\x04 \x01(\tR\x12androidPackageName\"5\n" + - "\tWIFIState\x12\x12\n" + - "\x04ssid\x18\x01 \x01(\tR\x04ssid\x12\x14\n" + - "\x05bssid\x18\x02 \x01(\tR\x05bssidB%Z#github.com/sagernet/sing-box/daemonb\x06proto3" - -var ( - file_daemon_helper_proto_rawDescOnce sync.Once - file_daemon_helper_proto_rawDescData []byte -) - -func file_daemon_helper_proto_rawDescGZIP() []byte { - file_daemon_helper_proto_rawDescOnce.Do(func() { - file_daemon_helper_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_daemon_helper_proto_rawDesc), len(file_daemon_helper_proto_rawDesc))) - }) - return file_daemon_helper_proto_rawDescData -} - -var ( - file_daemon_helper_proto_msgTypes = make([]protoimpl.MessageInfo, 7) - file_daemon_helper_proto_goTypes = []any{ - (*SubscribeHelperRequestRequest)(nil), // 0: daemon.SubscribeHelperRequestRequest - (*HelperRequest)(nil), // 1: daemon.HelperRequest - (*FindConnectionOwnerRequest)(nil), // 2: daemon.FindConnectionOwnerRequest - (*Notification)(nil), // 3: daemon.Notification - (*HelperResponse)(nil), // 4: daemon.HelperResponse - (*ConnectionOwner)(nil), // 5: daemon.ConnectionOwner - (*WIFIState)(nil), // 6: daemon.WIFIState - (*emptypb.Empty)(nil), // 7: google.protobuf.Empty - } -) - -var file_daemon_helper_proto_depIdxs = []int32{ - 7, // 0: daemon.HelperRequest.getWIFIState:type_name -> google.protobuf.Empty - 2, // 1: daemon.HelperRequest.findConnectionOwner:type_name -> daemon.FindConnectionOwnerRequest - 3, // 2: daemon.HelperRequest.sendNotification:type_name -> daemon.Notification - 6, // 3: daemon.HelperResponse.wifiState:type_name -> daemon.WIFIState - 5, // 4: daemon.HelperResponse.connectionOwner:type_name -> daemon.ConnectionOwner - 5, // [5:5] is the sub-list for method output_type - 5, // [5:5] is the sub-list for method input_type - 5, // [5:5] is the sub-list for extension type_name - 5, // [5:5] is the sub-list for extension extendee - 0, // [0:5] is the sub-list for field type_name -} - -func init() { file_daemon_helper_proto_init() } -func file_daemon_helper_proto_init() { - if File_daemon_helper_proto != nil { - return - } - file_daemon_helper_proto_msgTypes[1].OneofWrappers = []any{ - (*HelperRequest_GetWIFIState)(nil), - (*HelperRequest_FindConnectionOwner)(nil), - (*HelperRequest_SendNotification)(nil), - } - file_daemon_helper_proto_msgTypes[4].OneofWrappers = []any{ - (*HelperResponse_WifiState)(nil), - (*HelperResponse_Error)(nil), - (*HelperResponse_ConnectionOwner)(nil), - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: unsafe.Slice(unsafe.StringData(file_daemon_helper_proto_rawDesc), len(file_daemon_helper_proto_rawDesc)), - NumEnums: 0, - NumMessages: 7, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_daemon_helper_proto_goTypes, - DependencyIndexes: file_daemon_helper_proto_depIdxs, - MessageInfos: file_daemon_helper_proto_msgTypes, - }.Build() - File_daemon_helper_proto = out.File - file_daemon_helper_proto_goTypes = nil - file_daemon_helper_proto_depIdxs = nil -} diff --git a/daemon/helper.proto b/daemon/helper.proto deleted file mode 100644 index 8cf28075b..000000000 --- a/daemon/helper.proto +++ /dev/null @@ -1,61 +0,0 @@ -syntax = "proto3"; - -package daemon; -option go_package = "github.com/sagernet/sing-box/daemon"; - -import "google/protobuf/empty.proto"; - -message SubscribeHelperRequestRequest { - bool acceptGetWIFIStateRequests = 1; - bool acceptFindConnectionOwnerRequests = 2; - bool acceptSendNotificationRequests = 3; -} - -message HelperRequest { - int64 id = 1; - oneof request { - google.protobuf.Empty getWIFIState = 2; - FindConnectionOwnerRequest findConnectionOwner = 3; - Notification sendNotification = 4; - } -} - -message FindConnectionOwnerRequest { - int32 ipProtocol = 1; - string sourceAddress = 2; - int32 sourcePort = 3; - string destinationAddress = 4; - int32 destinationPort = 5; -} - -message Notification { - string identifier = 1; - string typeName = 2; - int32 typeId = 3; - string title = 4; - string subtitle = 5; - string body = 6; - string openURL = 7; -} - -message HelperResponse { - int64 id = 1; - oneof response { - WIFIState wifiState = 2; - string error = 3; - ConnectionOwner connectionOwner = 4; - } -} - -message ConnectionOwner { - int32 userId = 1; - string userName = 2; - string processPath = 3; - string androidPackageName = 4; -} - -message WIFIState { - string ssid = 1; - string bssid = 2; -} - diff --git a/daemon/started_service.go b/daemon/started_service.go index 2eb46106f..7176f058e 100644 --- a/daemon/started_service.go +++ b/daemon/started_service.go @@ -823,14 +823,6 @@ func (s *StartedService) GetStartedAt(ctx context.Context, empty *emptypb.Empty) return &StartedAt{StartedAt: s.startedAt.UnixMilli()}, nil } -func (s *StartedService) SubscribeHelperEvents(empty *emptypb.Empty, server grpc.ServerStreamingServer[HelperRequest]) error { - return os.ErrInvalid -} - -func (s *StartedService) SendHelperResponse(ctx context.Context, response *HelperResponse) (*emptypb.Empty, error) { - return nil, os.ErrInvalid -} - func (s *StartedService) mustEmbedUnimplementedStartedServiceServer() { } diff --git a/daemon/started_service.pb.go b/daemon/started_service.pb.go index 5f3726fef..b00a1fb2e 100644 --- a/daemon/started_service.pb.go +++ b/daemon/started_service.pb.go @@ -1751,7 +1751,7 @@ var File_daemon_started_service_proto protoreflect.FileDescriptor const file_daemon_started_service_proto_rawDesc = "" + "\n" + - "\x1cdaemon/started_service.proto\x12\x06daemon\x1a\x1bgoogle/protobuf/empty.proto\x1a\x13daemon/helper.proto\"\xad\x01\n" + + "\x1cdaemon/started_service.proto\x12\x06daemon\x1a\x1bgoogle/protobuf/empty.proto\"\xad\x01\n" + "\rServiceStatus\x122\n" + "\x06status\x18\x01 \x01(\x0e2\x1a.daemon.ServiceStatus.TypeR\x06status\x12\"\n" + "\ferrorMessage\x18\x02 \x01(\tR\ferrorMessage\"D\n" + @@ -1883,7 +1883,7 @@ const file_daemon_started_service_proto_rawDesc = "" + "\x10ConnectionSortBy\x12\b\n" + "\x04DATE\x10\x00\x12\v\n" + "\aTRAFFIC\x10\x01\x12\x11\n" + - "\rTOTAL_TRAFFIC\x10\x022\xf4\f\n" + + "\rTOTAL_TRAFFIC\x10\x022\xe0\v\n" + "\x0eStartedService\x12=\n" + "\vStopService\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.Empty\x12?\n" + "\rReloadService\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.Empty\x12K\n" + @@ -1905,9 +1905,7 @@ const file_daemon_started_service_proto_rawDesc = "" + "\x0fCloseConnection\x12\x1e.daemon.CloseConnectionRequest\x1a\x16.google.protobuf.Empty\"\x00\x12G\n" + "\x13CloseAllConnections\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.Empty\"\x00\x12M\n" + "\x15GetDeprecatedWarnings\x12\x16.google.protobuf.Empty\x1a\x1a.daemon.DeprecatedWarnings\"\x00\x12;\n" + - "\fGetStartedAt\x12\x16.google.protobuf.Empty\x1a\x11.daemon.StartedAt\"\x00\x12J\n" + - "\x15SubscribeHelperEvents\x12\x16.google.protobuf.Empty\x1a\x15.daemon.HelperRequest\"\x000\x01\x12F\n" + - "\x12SendHelperResponse\x12\x16.daemon.HelperResponse\x1a\x16.google.protobuf.Empty\"\x00B%Z#github.com/sagernet/sing-box/daemonb\x06proto3" + "\fGetStartedAt\x12\x16.google.protobuf.Empty\x1a\x11.daemon.StartedAt\"\x00B%Z#github.com/sagernet/sing-box/daemonb\x06proto3" var ( file_daemon_started_service_proto_rawDescOnce sync.Once @@ -1955,8 +1953,6 @@ var ( (*StartedAt)(nil), // 27: daemon.StartedAt (*Log_Message)(nil), // 28: daemon.Log.Message (*emptypb.Empty)(nil), // 29: google.protobuf.Empty - (*HelperResponse)(nil), // 30: daemon.HelperResponse - (*HelperRequest)(nil), // 31: daemon.HelperRequest } ) @@ -1993,33 +1989,29 @@ var file_daemon_started_service_proto_depIdxs = []int32{ 29, // 29: daemon.StartedService.CloseAllConnections:input_type -> google.protobuf.Empty 29, // 30: daemon.StartedService.GetDeprecatedWarnings:input_type -> google.protobuf.Empty 29, // 31: daemon.StartedService.GetStartedAt:input_type -> google.protobuf.Empty - 29, // 32: daemon.StartedService.SubscribeHelperEvents:input_type -> google.protobuf.Empty - 30, // 33: daemon.StartedService.SendHelperResponse:input_type -> daemon.HelperResponse - 29, // 34: daemon.StartedService.StopService:output_type -> google.protobuf.Empty - 29, // 35: daemon.StartedService.ReloadService:output_type -> google.protobuf.Empty - 4, // 36: daemon.StartedService.SubscribeServiceStatus:output_type -> daemon.ServiceStatus - 7, // 37: daemon.StartedService.SubscribeLog:output_type -> daemon.Log - 8, // 38: daemon.StartedService.GetDefaultLogLevel:output_type -> daemon.DefaultLogLevel - 29, // 39: daemon.StartedService.ClearLogs:output_type -> google.protobuf.Empty - 9, // 40: daemon.StartedService.SubscribeStatus:output_type -> daemon.Status - 10, // 41: daemon.StartedService.SubscribeGroups:output_type -> daemon.Groups - 17, // 42: daemon.StartedService.GetClashModeStatus:output_type -> daemon.ClashModeStatus - 16, // 43: daemon.StartedService.SubscribeClashMode:output_type -> daemon.ClashMode - 29, // 44: daemon.StartedService.SetClashMode:output_type -> google.protobuf.Empty - 29, // 45: daemon.StartedService.URLTest:output_type -> google.protobuf.Empty - 29, // 46: daemon.StartedService.SelectOutbound:output_type -> google.protobuf.Empty - 29, // 47: daemon.StartedService.SetGroupExpand:output_type -> google.protobuf.Empty - 18, // 48: daemon.StartedService.GetSystemProxyStatus:output_type -> daemon.SystemProxyStatus - 29, // 49: daemon.StartedService.SetSystemProxyEnabled:output_type -> google.protobuf.Empty - 21, // 50: daemon.StartedService.SubscribeConnections:output_type -> daemon.Connections - 29, // 51: daemon.StartedService.CloseConnection:output_type -> google.protobuf.Empty - 29, // 52: daemon.StartedService.CloseAllConnections:output_type -> google.protobuf.Empty - 25, // 53: daemon.StartedService.GetDeprecatedWarnings:output_type -> daemon.DeprecatedWarnings - 27, // 54: daemon.StartedService.GetStartedAt:output_type -> daemon.StartedAt - 31, // 55: daemon.StartedService.SubscribeHelperEvents:output_type -> daemon.HelperRequest - 29, // 56: daemon.StartedService.SendHelperResponse:output_type -> google.protobuf.Empty - 34, // [34:57] is the sub-list for method output_type - 11, // [11:34] is the sub-list for method input_type + 29, // 32: daemon.StartedService.StopService:output_type -> google.protobuf.Empty + 29, // 33: daemon.StartedService.ReloadService:output_type -> google.protobuf.Empty + 4, // 34: daemon.StartedService.SubscribeServiceStatus:output_type -> daemon.ServiceStatus + 7, // 35: daemon.StartedService.SubscribeLog:output_type -> daemon.Log + 8, // 36: daemon.StartedService.GetDefaultLogLevel:output_type -> daemon.DefaultLogLevel + 29, // 37: daemon.StartedService.ClearLogs:output_type -> google.protobuf.Empty + 9, // 38: daemon.StartedService.SubscribeStatus:output_type -> daemon.Status + 10, // 39: daemon.StartedService.SubscribeGroups:output_type -> daemon.Groups + 17, // 40: daemon.StartedService.GetClashModeStatus:output_type -> daemon.ClashModeStatus + 16, // 41: daemon.StartedService.SubscribeClashMode:output_type -> daemon.ClashMode + 29, // 42: daemon.StartedService.SetClashMode:output_type -> google.protobuf.Empty + 29, // 43: daemon.StartedService.URLTest:output_type -> google.protobuf.Empty + 29, // 44: daemon.StartedService.SelectOutbound:output_type -> google.protobuf.Empty + 29, // 45: daemon.StartedService.SetGroupExpand:output_type -> google.protobuf.Empty + 18, // 46: daemon.StartedService.GetSystemProxyStatus:output_type -> daemon.SystemProxyStatus + 29, // 47: daemon.StartedService.SetSystemProxyEnabled:output_type -> google.protobuf.Empty + 21, // 48: daemon.StartedService.SubscribeConnections:output_type -> daemon.Connections + 29, // 49: daemon.StartedService.CloseConnection:output_type -> google.protobuf.Empty + 29, // 50: daemon.StartedService.CloseAllConnections:output_type -> google.protobuf.Empty + 25, // 51: daemon.StartedService.GetDeprecatedWarnings:output_type -> daemon.DeprecatedWarnings + 27, // 52: daemon.StartedService.GetStartedAt:output_type -> daemon.StartedAt + 32, // [32:53] is the sub-list for method output_type + 11, // [11:32] is the sub-list for method input_type 11, // [11:11] is the sub-list for extension type_name 11, // [11:11] is the sub-list for extension extendee 0, // [0:11] is the sub-list for field type_name @@ -2030,7 +2022,6 @@ func file_daemon_started_service_proto_init() { if File_daemon_started_service_proto != nil { return } - file_daemon_helper_proto_init() type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/daemon/started_service.proto b/daemon/started_service.proto index a063d7a37..cd501cb5d 100644 --- a/daemon/started_service.proto +++ b/daemon/started_service.proto @@ -4,7 +4,6 @@ package daemon; option go_package = "github.com/sagernet/sing-box/daemon"; import "google/protobuf/empty.proto"; -import "daemon/helper.proto"; service StartedService { rpc StopService(google.protobuf.Empty) returns (google.protobuf.Empty); @@ -33,9 +32,6 @@ service StartedService { rpc CloseAllConnections(google.protobuf.Empty) returns(google.protobuf.Empty) {} rpc GetDeprecatedWarnings(google.protobuf.Empty) returns(DeprecatedWarnings) {} rpc GetStartedAt(google.protobuf.Empty) returns(StartedAt) {} - - rpc SubscribeHelperEvents(google.protobuf.Empty) returns(stream HelperRequest) {} - rpc SendHelperResponse(HelperResponse) returns(google.protobuf.Empty) {} } message ServiceStatus { diff --git a/daemon/started_service_grpc.pb.go b/daemon/started_service_grpc.pb.go index a95804691..1fd09e405 100644 --- a/daemon/started_service_grpc.pb.go +++ b/daemon/started_service_grpc.pb.go @@ -36,8 +36,6 @@ const ( StartedService_CloseAllConnections_FullMethodName = "/daemon.StartedService/CloseAllConnections" StartedService_GetDeprecatedWarnings_FullMethodName = "/daemon.StartedService/GetDeprecatedWarnings" StartedService_GetStartedAt_FullMethodName = "/daemon.StartedService/GetStartedAt" - StartedService_SubscribeHelperEvents_FullMethodName = "/daemon.StartedService/SubscribeHelperEvents" - StartedService_SendHelperResponse_FullMethodName = "/daemon.StartedService/SendHelperResponse" ) // StartedServiceClient is the client API for StartedService service. @@ -65,8 +63,6 @@ type StartedServiceClient interface { CloseAllConnections(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error) GetDeprecatedWarnings(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*DeprecatedWarnings, error) GetStartedAt(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*StartedAt, error) - SubscribeHelperEvents(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (grpc.ServerStreamingClient[HelperRequest], error) - SendHelperResponse(ctx context.Context, in *HelperResponse, opts ...grpc.CallOption) (*emptypb.Empty, error) } type startedServiceClient struct { @@ -341,35 +337,6 @@ func (c *startedServiceClient) GetStartedAt(ctx context.Context, in *emptypb.Emp return out, nil } -func (c *startedServiceClient) SubscribeHelperEvents(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (grpc.ServerStreamingClient[HelperRequest], error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - stream, err := c.cc.NewStream(ctx, &StartedService_ServiceDesc.Streams[6], StartedService_SubscribeHelperEvents_FullMethodName, cOpts...) - if err != nil { - return nil, err - } - x := &grpc.GenericClientStream[emptypb.Empty, HelperRequest]{ClientStream: stream} - if err := x.ClientStream.SendMsg(in); err != nil { - return nil, err - } - if err := x.ClientStream.CloseSend(); err != nil { - return nil, err - } - return x, nil -} - -// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. -type StartedService_SubscribeHelperEventsClient = grpc.ServerStreamingClient[HelperRequest] - -func (c *startedServiceClient) SendHelperResponse(ctx context.Context, in *HelperResponse, opts ...grpc.CallOption) (*emptypb.Empty, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(emptypb.Empty) - err := c.cc.Invoke(ctx, StartedService_SendHelperResponse_FullMethodName, in, out, cOpts...) - if err != nil { - return nil, err - } - return out, nil -} - // StartedServiceServer is the server API for StartedService service. // All implementations must embed UnimplementedStartedServiceServer // for forward compatibility. @@ -395,8 +362,6 @@ type StartedServiceServer interface { CloseAllConnections(context.Context, *emptypb.Empty) (*emptypb.Empty, error) GetDeprecatedWarnings(context.Context, *emptypb.Empty) (*DeprecatedWarnings, error) GetStartedAt(context.Context, *emptypb.Empty) (*StartedAt, error) - SubscribeHelperEvents(*emptypb.Empty, grpc.ServerStreamingServer[HelperRequest]) error - SendHelperResponse(context.Context, *HelperResponse) (*emptypb.Empty, error) mustEmbedUnimplementedStartedServiceServer() } @@ -490,14 +455,6 @@ func (UnimplementedStartedServiceServer) GetDeprecatedWarnings(context.Context, func (UnimplementedStartedServiceServer) GetStartedAt(context.Context, *emptypb.Empty) (*StartedAt, error) { return nil, status.Errorf(codes.Unimplemented, "method GetStartedAt not implemented") } - -func (UnimplementedStartedServiceServer) SubscribeHelperEvents(*emptypb.Empty, grpc.ServerStreamingServer[HelperRequest]) error { - return status.Errorf(codes.Unimplemented, "method SubscribeHelperEvents not implemented") -} - -func (UnimplementedStartedServiceServer) SendHelperResponse(context.Context, *HelperResponse) (*emptypb.Empty, error) { - return nil, status.Errorf(codes.Unimplemented, "method SendHelperResponse not implemented") -} func (UnimplementedStartedServiceServer) mustEmbedUnimplementedStartedServiceServer() {} func (UnimplementedStartedServiceServer) testEmbeddedByValue() {} @@ -855,35 +812,6 @@ func _StartedService_GetStartedAt_Handler(srv interface{}, ctx context.Context, return interceptor(ctx, in, info, handler) } -func _StartedService_SubscribeHelperEvents_Handler(srv interface{}, stream grpc.ServerStream) error { - m := new(emptypb.Empty) - if err := stream.RecvMsg(m); err != nil { - return err - } - return srv.(StartedServiceServer).SubscribeHelperEvents(m, &grpc.GenericServerStream[emptypb.Empty, HelperRequest]{ServerStream: stream}) -} - -// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. -type StartedService_SubscribeHelperEventsServer = grpc.ServerStreamingServer[HelperRequest] - -func _StartedService_SendHelperResponse_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(HelperResponse) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(StartedServiceServer).SendHelperResponse(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: StartedService_SendHelperResponse_FullMethodName, - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(StartedServiceServer).SendHelperResponse(ctx, req.(*HelperResponse)) - } - return interceptor(ctx, in, info, handler) -} - // StartedService_ServiceDesc is the grpc.ServiceDesc for StartedService service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -951,10 +879,6 @@ var StartedService_ServiceDesc = grpc.ServiceDesc{ MethodName: "GetStartedAt", Handler: _StartedService_GetStartedAt_Handler, }, - { - MethodName: "SendHelperResponse", - Handler: _StartedService_SendHelperResponse_Handler, - }, }, Streams: []grpc.StreamDesc{ { @@ -987,11 +911,6 @@ var StartedService_ServiceDesc = grpc.ServiceDesc{ Handler: _StartedService_SubscribeConnections_Handler, ServerStreams: true, }, - { - StreamName: "SubscribeHelperEvents", - Handler: _StartedService_SubscribeHelperEvents_Handler, - ServerStreams: true, - }, }, Metadata: "daemon/started_service.proto", } diff --git a/experimental/libbox/command_client.go b/experimental/libbox/command_client.go index 8b60332ec..6f8b5accd 100644 --- a/experimental/libbox/command_client.go +++ b/experimental/libbox/command_client.go @@ -62,6 +62,16 @@ type LogIterator interface { Next() *LogEntry } +type XPCDialer interface { + DialXPC() (int32, error) +} + +var sXPCDialer XPCDialer + +func SetXPCDialer(dialer XPCDialer) { + sXPCDialer = dialer +} + func NewStandaloneCommandClient() *CommandClient { return new(CommandClient) } @@ -117,13 +127,113 @@ func (c *CommandClient) Connect() error { c.clientMutex.Lock() common.Close(common.PtrOrNil(c.grpcConn)) - conn, err := c.grpcDial() + if sXPCDialer != nil { + fd, err := sXPCDialer.DialXPC() + if err != nil { + c.clientMutex.Unlock() + return err + } + file := os.NewFile(uintptr(fd), "xpc-command-socket") + if file == nil { + c.clientMutex.Unlock() + return E.New("invalid file descriptor") + } + netConn, err := net.FileConn(file) + if err != nil { + file.Close() + c.clientMutex.Unlock() + return E.Cause(err, "create connection from fd") + } + file.Close() + + clientOptions := []grpc.DialOption{ + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { + return netConn, nil + }), + grpc.WithUnaryInterceptor(unaryClientAuthInterceptor), + grpc.WithStreamInterceptor(streamClientAuthInterceptor), + } + + grpcConn, err := grpc.NewClient("passthrough:///xpc", clientOptions...) + if err != nil { + netConn.Close() + c.clientMutex.Unlock() + return err + } + + c.grpcConn = grpcConn + c.grpcClient = daemon.NewStartedServiceClient(grpcConn) + c.ctx, c.cancel = context.WithCancel(context.Background()) + c.clientMutex.Unlock() + } else { + conn, err := c.grpcDial() + if err != nil { + c.clientMutex.Unlock() + return err + } + c.grpcConn = conn + c.grpcClient = daemon.NewStartedServiceClient(conn) + c.ctx, c.cancel = context.WithCancel(context.Background()) + c.clientMutex.Unlock() + } + + c.handler.Connected() + for _, command := range c.options.commands { + switch command { + case CommandLog: + go c.handleLogStream() + case CommandStatus: + go c.handleStatusStream() + case CommandGroup: + go c.handleGroupStream() + case CommandClashMode: + go c.handleClashModeStream() + case CommandConnections: + go c.handleConnectionsStream() + default: + return E.New("unknown command: ", command) + } + } + return nil +} + +func (c *CommandClient) ConnectWithFD(fd int32) error { + c.clientMutex.Lock() + common.Close(common.PtrOrNil(c.grpcConn)) + + file := os.NewFile(uintptr(fd), "xpc-command-socket") + if file == nil { + c.clientMutex.Unlock() + return E.New("invalid file descriptor") + } + + netConn, err := net.FileConn(file) if err != nil { + file.Close() + c.clientMutex.Unlock() + return E.Cause(err, "create connection from fd") + } + file.Close() + + clientOptions := []grpc.DialOption{ + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { + return netConn, nil + }), + grpc.WithUnaryInterceptor(unaryClientAuthInterceptor), + grpc.WithStreamInterceptor(streamClientAuthInterceptor), + } + + grpcConn, err := grpc.NewClient("passthrough:///xpc", clientOptions...) + if err != nil { + netConn.Close() c.clientMutex.Unlock() return err } - c.grpcConn = conn - c.grpcClient = daemon.NewStartedServiceClient(conn) + + c.grpcConn = grpcConn + c.grpcClient = daemon.NewStartedServiceClient(grpcConn) c.ctx, c.cancel = context.WithCancel(context.Background()) c.clientMutex.Unlock() @@ -171,6 +281,45 @@ func (c *CommandClient) getClientForCall() (daemon.StartedServiceClient, error) return c.grpcClient, nil } + if sXPCDialer != nil { + fd, err := sXPCDialer.DialXPC() + if err != nil { + return nil, err + } + file := os.NewFile(uintptr(fd), "xpc-command-socket") + if file == nil { + return nil, E.New("invalid file descriptor") + } + netConn, err := net.FileConn(file) + if err != nil { + file.Close() + return nil, E.Cause(err, "create connection from fd") + } + file.Close() + + clientOptions := []grpc.DialOption{ + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { + return netConn, nil + }), + grpc.WithUnaryInterceptor(unaryClientAuthInterceptor), + grpc.WithStreamInterceptor(streamClientAuthInterceptor), + } + + grpcConn, err := grpc.NewClient("passthrough:///xpc", clientOptions...) + if err != nil { + netConn.Close() + return nil, err + } + + c.grpcConn = grpcConn + c.grpcClient = daemon.NewStartedServiceClient(grpcConn) + if c.ctx == nil { + c.ctx, c.cancel = context.WithCancel(context.Background()) + } + return c.grpcClient, nil + } + conn, err := c.grpcDial() if err != nil { return nil, err diff --git a/experimental/libbox/command_server.go b/experimental/libbox/command_server.go index b25f30658..f889c381e 100644 --- a/experimental/libbox/command_server.go +++ b/experimental/libbox/command_server.go @@ -195,6 +195,14 @@ func (s *CommandServer) NeedWIFIState() bool { return instance.Box().Network().NeedWIFIState() } +func (s *CommandServer) NeedFindProcess() bool { + instance := s.StartedService.Instance() + if instance == nil || instance.Box() == nil { + return false + } + return instance.Box().Router().NeedFindProcess() +} + func (s *CommandServer) Pause() { instance := s.StartedService.Instance() if instance == nil || instance.PauseManager() == nil { diff --git a/experimental/libbox/platform.go b/experimental/libbox/platform.go index 22345201f..fcfbe76b0 100644 --- a/experimental/libbox/platform.go +++ b/experimental/libbox/platform.go @@ -11,9 +11,7 @@ type PlatformInterface interface { AutoDetectInterfaceControl(fd int32) error OpenTun(options TunOptions) (int32, error) UseProcFS() bool - FindConnectionOwner(ipProtocol int32, sourceAddress string, sourcePort int32, destinationAddress string, destinationPort int32) (int32, error) - PackageNameByUid(uid int32) (string, error) - UIDByPackageName(packageName string) (int32, error) + FindConnectionOwner(ipProtocol int32, sourceAddress string, sourcePort int32, destinationAddress string, destinationPort int32) (*ConnectionOwner, error) StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error GetInterfaces() (NetworkInterfaceIterator, error) @@ -25,6 +23,12 @@ type PlatformInterface interface { SendNotification(notification *Notification) error } +type ConnectionOwner struct { + UserId int32 + UserName string + ProcessPath string +} + type InterfaceUpdateListener interface { UpdateDefaultInterface(interfaceName string, interfaceIndex int32, isExpensive bool, isConstrained bool) } diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index 2cc270f4c..8d3de4608 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -166,7 +166,6 @@ func (w *platformInterfaceWrapper) UsePlatformConnectionOwnerFinder() bool { } func (w *platformInterfaceWrapper) FindConnectionOwner(request *adapter.FindConnectionOwnerRequest) (*adapter.ConnectionOwner, error) { - var uid int32 if w.useProcFS { var source netip.AddrPort var destination netip.AddrPort @@ -185,21 +184,23 @@ func (w *platformInterfaceWrapper) FindConnectionOwner(request *adapter.FindConn return nil, E.New("unknown protocol: ", request.IpProtocol) } - uid = procfs.ResolveSocketByProcSearch(network, source, destination) + uid := procfs.ResolveSocketByProcSearch(network, source, destination) if uid == -1 { return nil, E.New("procfs: not found") } - } else { - var err error - uid, err = w.iif.FindConnectionOwner(request.IpProtocol, request.SourceAddress, request.SourcePort, request.DestinationAddress, request.DestinationPort) - if err != nil { - return nil, err - } + return &adapter.ConnectionOwner{ + UserId: uid, + }, nil + } + + result, err := w.iif.FindConnectionOwner(request.IpProtocol, request.SourceAddress, request.SourcePort, request.DestinationAddress, request.DestinationPort) + if err != nil { + return nil, err } - packageName, _ := w.iif.PackageNameByUid(uid) return &adapter.ConnectionOwner{ - UserId: uid, - AndroidPackageName: packageName, + UserId: result.UserId, + UserName: result.UserName, + ProcessPath: result.ProcessPath, }, nil } diff --git a/route/router.go b/route/router.go index fe72c1c5e..5c73cb1c9 100644 --- a/route/router.go +++ b/route/router.go @@ -121,6 +121,7 @@ func (r *Router) Start(stage adapter.StartStage) error { if C.IsAndroid && r.platformInterface != nil { needFindProcess = true } + r.needFindProcess = needFindProcess if needFindProcess { if r.platformInterface != nil && r.platformInterface.UsePlatformConnectionOwnerFinder() { r.processSearcher = newPlatformSearcher(r.platformInterface) @@ -201,6 +202,10 @@ func (r *Router) AppendTracker(tracker adapter.ConnectionTracker) { r.trackers = append(r.trackers, tracker) } +func (r *Router) NeedFindProcess() bool { + return r.needFindProcess +} + func (r *Router) ResetNetwork() { r.network.ResetNetwork() r.dns.ResetNetwork()