1
0

inbound.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package shadowtls
  2. import (
  3. "context"
  4. "os"
  5. "github.com/sagernet/sing-shadowtls"
  6. sing_common "github.com/sagernet/sing/common"
  7. "github.com/sagernet/sing/common/auth"
  8. E "github.com/sagernet/sing/common/exceptions"
  9. M "github.com/sagernet/sing/common/metadata"
  10. N "github.com/sagernet/sing/common/network"
  11. "github.com/xtls/xray-core/common"
  12. "github.com/xtls/xray-core/common/log"
  13. "github.com/xtls/xray-core/common/net"
  14. "github.com/xtls/xray-core/common/protocol"
  15. "github.com/xtls/xray-core/common/session"
  16. "github.com/xtls/xray-core/common/singbridge"
  17. "github.com/xtls/xray-core/core"
  18. "github.com/xtls/xray-core/features/inbound"
  19. "github.com/xtls/xray-core/features/routing"
  20. "github.com/xtls/xray-core/proxy"
  21. "github.com/xtls/xray-core/transport/internet/stat"
  22. )
  23. func init() {
  24. common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  25. return NewServer(ctx, config.(*ServerConfig))
  26. }))
  27. }
  28. type Inbound struct {
  29. service *shadowtls.Service
  30. inboundManager inbound.Manager
  31. detour string
  32. }
  33. func NewServer(ctx context.Context, config *ServerConfig) (*Inbound, error) {
  34. v := core.MustFromContext(ctx)
  35. inbound := &Inbound{
  36. inboundManager: v.GetFeature(inbound.ManagerType()).(inbound.Manager),
  37. detour: config.Detour,
  38. }
  39. var handshakeForServerName map[string]shadowtls.HandshakeConfig
  40. if config.Version > 1 {
  41. handshakeForServerName = make(map[string]shadowtls.HandshakeConfig)
  42. for serverName, serverConfig := range config.HandshakeForServerName {
  43. handshakeForServerName[serverName] = shadowtls.HandshakeConfig{
  44. Server: singbridge.ToSocksaddr(net.Destination{
  45. Address: serverConfig.Address.AsAddress(),
  46. Port: net.Port(serverConfig.Port),
  47. }),
  48. Dialer: N.SystemDialer,
  49. }
  50. }
  51. }
  52. service, err := shadowtls.NewService(shadowtls.ServiceConfig{
  53. Version: int(config.Version),
  54. Password: config.Password,
  55. Users: sing_common.Map(config.Users, func(it *User) shadowtls.User {
  56. return shadowtls.User{
  57. Name: it.Email,
  58. Password: it.Password,
  59. }
  60. }),
  61. Handshake: shadowtls.HandshakeConfig{
  62. Server: singbridge.ToSocksaddr(net.Destination{
  63. Address: config.Handshake.Address.AsAddress(),
  64. Port: net.Port(config.Handshake.Port),
  65. }),
  66. Dialer: N.SystemDialer,
  67. },
  68. HandshakeForServerName: handshakeForServerName,
  69. StrictMode: config.StrictMode,
  70. Handler: inbound,
  71. Logger: singbridge.NewLogger(newError),
  72. })
  73. if err != nil {
  74. return nil, E.Cause(err, "create service")
  75. }
  76. inbound.service = service
  77. return inbound, nil
  78. }
  79. func (i *Inbound) Network() []net.Network {
  80. return []net.Network{net.Network_TCP}
  81. }
  82. func (i *Inbound) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
  83. inbound := session.InboundFromContext(ctx)
  84. var metadata M.Metadata
  85. if inbound.Source.IsValid() {
  86. metadata.Source = M.ParseSocksaddr(inbound.Source.NetAddr())
  87. }
  88. ctx = session.ContextWithDispatcher(ctx, dispatcher)
  89. return singbridge.ReturnError(i.service.NewConnection(ctx, connection, metadata))
  90. }
  91. func (i *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
  92. inboundHandler, err := i.inboundManager.GetHandler(ctx, i.detour)
  93. if err != nil {
  94. return E.Cause(err, "detour not found")
  95. }
  96. inboundWrapper, loaded := inboundHandler.(proxy.GetInbound)
  97. if !loaded {
  98. return newError("can't get inbound proxy from handler.")
  99. }
  100. inboundDetour := inboundWrapper.GetInbound()
  101. email, _ := auth.UserFromContext[string](ctx)
  102. inbound := session.InboundFromContext(ctx)
  103. inbound.User = &protocol.MemoryUser{
  104. Email: email,
  105. }
  106. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  107. From: metadata.Source,
  108. To: metadata.Destination,
  109. Status: log.AccessAccepted,
  110. Email: email,
  111. })
  112. newError("tunnelling request to detour").WriteToLog(session.ExportIDToError(ctx))
  113. return inboundDetour.Process(ctx, net.Network_TCP, conn, session.DispatcherFromContext(ctx))
  114. }
  115. func (i *Inbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
  116. return os.ErrInvalid
  117. }
  118. func (i *Inbound) NewError(ctx context.Context, err error) {
  119. if E.IsClosed(err) {
  120. return
  121. }
  122. newError(err).AtWarning().WriteToLog()
  123. }