2022-06-30 13:27:56 +00:00
|
|
|
package adapter
|
|
|
|
|
2022-07-01 11:34:02 +00:00
|
|
|
import (
|
2022-07-07 13:47:21 +00:00
|
|
|
"context"
|
2022-08-29 11:43:13 +00:00
|
|
|
"net"
|
2022-07-07 15:36:32 +00:00
|
|
|
"net/netip"
|
2022-07-07 13:47:21 +00:00
|
|
|
|
2022-07-23 11:01:41 +00:00
|
|
|
"github.com/sagernet/sing-box/common/process"
|
2022-10-07 12:30:27 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-08 15:03:57 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-08-29 11:43:13 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-01 11:34:02 +00:00
|
|
|
)
|
|
|
|
|
2022-06-30 13:27:56 +00:00
|
|
|
type Inbound interface {
|
|
|
|
Service
|
|
|
|
Type() string
|
|
|
|
Tag() string
|
|
|
|
}
|
2022-07-01 11:34:02 +00:00
|
|
|
|
2022-08-29 11:43:13 +00:00
|
|
|
type InjectableInbound interface {
|
|
|
|
Inbound
|
|
|
|
Network() []string
|
|
|
|
NewConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
|
|
|
NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
|
|
|
}
|
|
|
|
|
2022-07-01 11:34:02 +00:00
|
|
|
type InboundContext struct {
|
|
|
|
Inbound string
|
2022-07-19 14:16:49 +00:00
|
|
|
InboundType string
|
2022-08-16 15:46:05 +00:00
|
|
|
IPVersion int
|
2022-07-01 11:34:02 +00:00
|
|
|
Network string
|
2022-07-02 06:07:50 +00:00
|
|
|
Source M.Socksaddr
|
|
|
|
Destination M.Socksaddr
|
2022-07-01 11:34:02 +00:00
|
|
|
Domain string
|
2022-07-02 06:07:50 +00:00
|
|
|
Protocol string
|
2022-07-17 07:11:26 +00:00
|
|
|
User string
|
2022-07-07 13:47:21 +00:00
|
|
|
Outbound string
|
2022-07-02 14:55:10 +00:00
|
|
|
|
|
|
|
// cache
|
|
|
|
|
2022-10-07 12:30:27 +00:00
|
|
|
InboundDetour string
|
|
|
|
LastInbound string
|
|
|
|
OriginDestination M.Socksaddr
|
|
|
|
InboundOptions option.InboundOptions
|
|
|
|
DestinationAddresses []netip.Addr
|
|
|
|
SourceGeoIPCode string
|
|
|
|
GeoIPCode string
|
|
|
|
ProcessInfo *process.Info
|
2022-07-01 11:34:02 +00:00
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
|
|
|
|
type inboundContextKey struct{}
|
|
|
|
|
|
|
|
func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
|
|
|
|
return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextFrom(ctx context.Context) *InboundContext {
|
|
|
|
metadata := ctx.Value((*inboundContextKey)(nil))
|
|
|
|
if metadata == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return metadata.(*InboundContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendContext(ctx context.Context) (context.Context, *InboundContext) {
|
|
|
|
metadata := ContextFrom(ctx)
|
|
|
|
if metadata != nil {
|
|
|
|
return ctx, metadata
|
|
|
|
}
|
|
|
|
metadata = new(InboundContext)
|
2022-07-07 15:36:32 +00:00
|
|
|
return WithContext(ctx, metadata), metadata
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|