Refactor endpoint in started interface

This commit is contained in:
世界
2026-08-30 17:41:43 +08:00
parent 719676519d
commit b02ec478c5
20 changed files with 326 additions and 110 deletions
+2 -2
View File
@@ -151,8 +151,8 @@ func networkConnectionFromFileDescriptor(fileDescriptor int32) (net.Conn, error)
func localDialOptions(contextDialer func(context.Context, string) (net.Conn, error)) []grpc.DialOption {
options := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(unaryClientAuthInterceptor),
grpc.WithStreamInterceptor(streamClientAuthInterceptor),
grpc.WithChainUnaryInterceptor(daemon.UnaryClientLocaleInterceptor, unaryClientAuthInterceptor),
grpc.WithChainStreamInterceptor(daemon.StreamClientLocaleInterceptor, streamClientAuthInterceptor),
}
if contextDialer != nil {
options = append(options, grpc.WithContextDialer(contextDialer))
+17 -11
View File
@@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/sagernet/sing-box/daemon"
E "github.com/sagernet/sing/common/exceptions"
"google.golang.org/grpc"
@@ -62,19 +63,24 @@ func newRemoteConnection(options *RemoteConnectionOptions) (*remoteConnection, e
if port == "" {
port = defaultPort
}
authorization := ""
if options.Secret != "" {
authorization = "Bearer " + options.Secret
}
dialOptions := []grpc.DialOption{
grpc.WithTransportCredentials(transportCredentials),
}
if options.Secret != "" {
authorization := "Bearer " + options.Secret
dialOptions = append(dialOptions,
grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(metadata.AppendToOutgoingContext(ctx, "authorization", authorization), method, req, reply, cc, opts...)
}),
grpc.WithStreamInterceptor(func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return streamer(metadata.AppendToOutgoingContext(ctx, "authorization", authorization), desc, cc, method, opts...)
}),
)
grpc.WithChainUnaryInterceptor(daemon.UnaryClientLocaleInterceptor, func(ctx context.Context, method string, request, reply any, connection *grpc.ClientConn, invoker grpc.UnaryInvoker, options ...grpc.CallOption) error {
if authorization != "" {
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", authorization)
}
return invoker(ctx, method, request, reply, connection, options...)
}),
grpc.WithChainStreamInterceptor(daemon.StreamClientLocaleInterceptor, func(ctx context.Context, description *grpc.StreamDesc, connection *grpc.ClientConn, method string, streamer grpc.Streamer, options ...grpc.CallOption) (grpc.ClientStream, error) {
if authorization != "" {
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", authorization)
}
return streamer(ctx, description, connection, method, options...)
}),
}
return &remoteConnection{
target: net.JoinHostPort(host, port),
+2 -2
View File
@@ -161,8 +161,8 @@ func (s *CommandServer) Start() error {
}
s.listener = listener
serverOptions := []grpc.ServerOption{
grpc.UnaryInterceptor(unaryAuthInterceptor),
grpc.StreamInterceptor(streamAuthInterceptor),
grpc.ChainUnaryInterceptor(unaryAuthInterceptor, daemon.UnaryLocaleInterceptor),
grpc.ChainStreamInterceptor(streamAuthInterceptor, daemon.StreamLocaleInterceptor),
}
s.grpcServer = grpc.NewServer(serverOptions...)
daemon.RegisterStartedServiceServer(s.grpcServer, s.StartedService)
@@ -21,6 +21,7 @@ type OpenConnectEndpointStatusIterator interface {
type OpenConnectEndpointStatus struct {
EndpointTag string
State string
StateText string
AuthForm *OpenConnectAuthForm
Error string
TunnelInfo *OpenConnectTunnelInfo
@@ -121,6 +122,7 @@ func openConnectEndpointStatusFromGRPC(status *daemon.OpenConnectEndpointStatus)
result := &OpenConnectEndpointStatus{
EndpointTag: status.EndpointTag,
State: status.State,
StateText: status.StateText,
Error: status.Error,
}
if status.AuthForm != nil {
@@ -21,6 +21,7 @@ type OpenVPNEndpointStatusIterator interface {
type OpenVPNEndpointStatus struct {
EndpointTag string
State string
StateText string
Challenge *OpenVPNChallenge
Error string
TunnelInfo *OpenVPNTunnelInfo
@@ -86,6 +87,7 @@ func openVPNEndpointStatusFromGRPC(status *daemon.OpenVPNEndpointStatus) *OpenVP
result := &OpenVPNEndpointStatus{
EndpointTag: status.EndpointTag,
State: status.State,
StateText: status.StateText,
Error: status.Error,
}
if status.Challenge != nil {
@@ -18,6 +18,7 @@ type TailscaleEndpointStatusIterator interface {
type TailscaleEndpointStatus struct {
EndpointTag string
BackendState string
StateText string
AuthURL string
NetworkName string
MagicDNSSuffix string
@@ -105,6 +106,7 @@ func tailscaleEndpointStatusFromGRPC(status *daemon.TailscaleEndpointStatus) *Ta
result := &TailscaleEndpointStatus{
EndpointTag: status.EndpointTag,
BackendState: status.BackendState,
StateText: status.StateText,
AuthURL: status.AuthURL,
NetworkName: status.NetworkName,
MagicDNSSuffix: status.MagicDNSSuffix,