mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-25 18:11:28 +00:00
Fix read source address from grpc-go
This commit is contained in:
parent
cbab86ae38
commit
c6586f19fa
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
"github.com/sagernet/sing-box/common/tls"
|
"github.com/sagernet/sing-box/common/tls"
|
||||||
|
@ -13,6 +14,8 @@ import (
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials"
|
"google.golang.org/grpc/credentials"
|
||||||
|
gM "google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/peer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
||||||
|
@ -41,7 +44,22 @@ func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig t
|
||||||
func (s *Server) Tun(server GunService_TunServer) error {
|
func (s *Server) Tun(server GunService_TunServer) error {
|
||||||
ctx, cancel := context.WithCancel(s.ctx)
|
ctx, cancel := context.WithCancel(s.ctx)
|
||||||
conn := NewGRPCConn(server, cancel)
|
conn := NewGRPCConn(server, cancel)
|
||||||
go s.handler.NewConnection(ctx, conn, M.Metadata{})
|
var metadata M.Metadata
|
||||||
|
if remotePeer, loaded := peer.FromContext(server.Context()); loaded {
|
||||||
|
metadata.Source = M.SocksaddrFromNet(remotePeer.Addr)
|
||||||
|
}
|
||||||
|
if grpcMetadata, loaded := gM.FromIncomingContext(server.Context()); loaded {
|
||||||
|
forwardFrom := strings.Join(grpcMetadata.Get("X-Forwarded-For"), ",")
|
||||||
|
if forwardFrom != "" {
|
||||||
|
for _, from := range strings.Split(forwardFrom, ",") {
|
||||||
|
originAddr := M.ParseSocksaddr(from)
|
||||||
|
if originAddr.IsValid() {
|
||||||
|
metadata.Source = originAddr.Unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
go s.handler.NewConnection(ctx, conn, metadata)
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue