mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-26 02:21:28 +00:00
Minor fixes
This commit is contained in:
parent
29f78248dc
commit
7f84191748
|
@ -37,7 +37,7 @@ func (w *ResolveUDPConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
|
||||||
|
|
||||||
func (w *ResolveUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
func (w *ResolveUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
if destination.Family().IsFqdn() {
|
if destination.IsFqdn() {
|
||||||
addresses, err := w.router.Lookup(context.Background(), destination.Fqdn, w.strategy)
|
addresses, err := w.router.Lookup(context.Background(), destination.Fqdn, w.strategy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -36,21 +36,10 @@ func QUICClientHello(ctx context.Context, packet []byte) (*adapter.InboundContex
|
||||||
if versionNumber != qtls.VersionDraft29 && versionNumber != qtls.Version1 && versionNumber != qtls.Version2 {
|
if versionNumber != qtls.VersionDraft29 && versionNumber != qtls.Version1 && versionNumber != qtls.Version2 {
|
||||||
return nil, E.New("bad version")
|
return nil, E.New("bad version")
|
||||||
}
|
}
|
||||||
if versionNumber == qtls.Version2 {
|
packetType := (typeByte & 0x30) >> 4
|
||||||
if (typeByte&0x30)>>4 == 0b01 {
|
if packetType == 0 && versionNumber == qtls.Version2 || packetType == 2 && versionNumber != qtls.Version2 || packetType > 2 {
|
||||||
} else if (typeByte&0x30)>>4 != 0b10 {
|
|
||||||
// 0-rtt
|
|
||||||
} else {
|
|
||||||
return nil, E.New("bad packet type")
|
return nil, E.New("bad packet type")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (typeByte&0x30)>>4 == 0x0 {
|
|
||||||
} else if (typeByte&0x30)>>4 != 0x01 {
|
|
||||||
// 0-rtt
|
|
||||||
} else {
|
|
||||||
return nil, E.New("bad packet type")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
destConnIDLen, err := reader.ReadByte()
|
destConnIDLen, err := reader.ReadByte()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -28,7 +28,6 @@ func PeekPacket(ctx context.Context, packet []byte, sniffers ...PacketSniffer) (
|
||||||
for _, sniffer := range sniffers {
|
for _, sniffer := range sniffers {
|
||||||
sniffMetadata, err := sniffer(ctx, packet)
|
sniffMetadata, err := sniffer(ctx, packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println(err.Error())
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return sniffMetadata, nil
|
return sniffMetadata, nil
|
||||||
|
|
|
@ -64,10 +64,12 @@ func (c *Client) Exchange(ctx context.Context, transport adapter.DNSTransport, m
|
||||||
}
|
}
|
||||||
return nil, ErrNoRawSupport
|
return nil, ErrNoRawSupport
|
||||||
}
|
}
|
||||||
|
messageId := message.ID
|
||||||
response, err := transport.Exchange(ctx, message)
|
response, err := transport.Exchange(ctx, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
response.ID = messageId
|
||||||
if !c.disableCache {
|
if !c.disableCache {
|
||||||
c.storeCache(question, response)
|
c.storeCache(question, response)
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,8 +60,8 @@ func NewTun(ctx context.Context, router adapter.Router, logger log.Logger, tag s
|
||||||
inboundOptions: options.InboundOptions,
|
inboundOptions: options.InboundOptions,
|
||||||
tunName: tunName,
|
tunName: tunName,
|
||||||
tunMTU: tunMTU,
|
tunMTU: tunMTU,
|
||||||
inet4Address: netip.Prefix(options.Inet4Address),
|
inet4Address: options.Inet4Address.Build(),
|
||||||
inet6Address: netip.Prefix(options.Inet6Address),
|
inet6Address: options.Inet6Address.Build(),
|
||||||
autoRoute: options.AutoRoute,
|
autoRoute: options.AutoRoute,
|
||||||
hijackDNS: options.HijackDNS,
|
hijackDNS: options.HijackDNS,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
@ -146,8 +146,8 @@ type ShadowsocksDestination struct {
|
||||||
type TunInboundOptions struct {
|
type TunInboundOptions struct {
|
||||||
InterfaceName string `json:"interface_name,omitempty"`
|
InterfaceName string `json:"interface_name,omitempty"`
|
||||||
MTU uint32 `json:"mtu,omitempty,omitempty"`
|
MTU uint32 `json:"mtu,omitempty,omitempty"`
|
||||||
Inet4Address ListenPrefix `json:"inet4_address,omitempty"`
|
Inet4Address *ListenPrefix `json:"inet4_address,omitempty"`
|
||||||
Inet6Address ListenPrefix `json:"inet6_address,omitempty"`
|
Inet6Address *ListenPrefix `json:"inet6_address,omitempty"`
|
||||||
AutoRoute bool `json:"auto_route,omitempty"`
|
AutoRoute bool `json:"auto_route,omitempty"`
|
||||||
HijackDNS bool `json:"hijack_dns,omitempty"`
|
HijackDNS bool `json:"hijack_dns,omitempty"`
|
||||||
InboundOptions
|
InboundOptions
|
||||||
|
|
|
@ -161,7 +161,7 @@ type ListenPrefix netip.Prefix
|
||||||
func (p ListenPrefix) MarshalJSON() ([]byte, error) {
|
func (p ListenPrefix) MarshalJSON() ([]byte, error) {
|
||||||
prefix := netip.Prefix(p)
|
prefix := netip.Prefix(p)
|
||||||
if !prefix.IsValid() {
|
if !prefix.IsValid() {
|
||||||
return json.Marshal("")
|
return json.Marshal(nil)
|
||||||
}
|
}
|
||||||
return json.Marshal(prefix.String())
|
return json.Marshal(prefix.String())
|
||||||
}
|
}
|
||||||
|
@ -179,3 +179,10 @@ func (p *ListenPrefix) UnmarshalJSON(bytes []byte) error {
|
||||||
*p = ListenPrefix(prefix)
|
*p = ListenPrefix(prefix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *ListenPrefix) Build() netip.Prefix {
|
||||||
|
if p == nil {
|
||||||
|
return netip.Prefix{}
|
||||||
|
}
|
||||||
|
return netip.Prefix(*p)
|
||||||
|
}
|
||||||
|
|
|
@ -72,11 +72,11 @@ func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr)
|
||||||
metadata.Outbound = h.tag
|
metadata.Outbound = h.tag
|
||||||
metadata.Destination = destination
|
metadata.Destination = destination
|
||||||
h.logger.WithContext(ctx).Info("outbound packet connection to ", h.serverAddr)
|
h.logger.WithContext(ctx).Info("outbound packet connection to ", h.serverAddr)
|
||||||
outConn, err := h.dialer.ListenPacket(ctx, destination)
|
outConn, err := h.dialer.DialContext(ctx, "udp", h.serverAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return h.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: outConn, Addr: h.serverAddr.UDPAddr()}), nil
|
return h.method.DialPacketConn(outConn), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
||||||
|
|
|
@ -482,7 +482,7 @@ func (r *Router) match(ctx context.Context, metadata adapter.InboundContext, def
|
||||||
for i, rule := range r.rules {
|
for i, rule := range r.rules {
|
||||||
if rule.Match(&metadata) {
|
if rule.Match(&metadata) {
|
||||||
detour := rule.Outbound()
|
detour := rule.Outbound()
|
||||||
r.logger.WithContext(ctx).Info("match[", i, "] ", rule.String(), " => ", detour)
|
r.logger.WithContext(ctx).Debug("match[", i, "] ", rule.String(), " => ", detour)
|
||||||
if outbound, loaded := r.Outbound(detour); loaded {
|
if outbound, loaded := r.Outbound(detour); loaded {
|
||||||
return outbound
|
return outbound
|
||||||
}
|
}
|
||||||
|
@ -501,7 +501,7 @@ func (r *Router) matchDNS(ctx context.Context) adapter.DNSTransport {
|
||||||
for i, rule := range r.dnsRules {
|
for i, rule := range r.dnsRules {
|
||||||
if rule.Match(metadata) {
|
if rule.Match(metadata) {
|
||||||
detour := rule.Outbound()
|
detour := rule.Outbound()
|
||||||
r.dnsLogger.WithContext(ctx).Info("match[", i, "] ", rule.String(), " => ", detour)
|
r.dnsLogger.WithContext(ctx).Debug("match[", i, "] ", rule.String(), " => ", detour)
|
||||||
if transport, loaded := r.transportMap[detour]; loaded {
|
if transport, loaded := r.transportMap[detour]; loaded {
|
||||||
return transport
|
return transport
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,8 @@ var _ adapter.Rule = (*DefaultDNSRule)(nil)
|
||||||
|
|
||||||
type DefaultDNSRule struct {
|
type DefaultDNSRule struct {
|
||||||
items []RuleItem
|
items []RuleItem
|
||||||
|
addressItems []RuleItem
|
||||||
|
allItems []RuleItem
|
||||||
outbound string
|
outbound string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,12 +54,14 @@ func NewDefaultDNSRule(router adapter.Router, logger log.Logger, options option.
|
||||||
if len(options.Inbound) > 0 {
|
if len(options.Inbound) > 0 {
|
||||||
item := NewInboundRule(options.Inbound)
|
item := NewInboundRule(options.Inbound)
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if options.Network != "" {
|
if options.Network != "" {
|
||||||
switch options.Network {
|
switch options.Network {
|
||||||
case C.NetworkTCP, C.NetworkUDP:
|
case C.NetworkTCP, C.NetworkUDP:
|
||||||
item := NewNetworkItem(options.Network)
|
item := NewNetworkItem(options.Network)
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
default:
|
default:
|
||||||
return nil, E.New("invalid network: ", options.Network)
|
return nil, E.New("invalid network: ", options.Network)
|
||||||
}
|
}
|
||||||
|
@ -65,29 +69,35 @@ func NewDefaultDNSRule(router adapter.Router, logger log.Logger, options option.
|
||||||
if len(options.Protocol) > 0 {
|
if len(options.Protocol) > 0 {
|
||||||
item := NewProtocolItem(options.Protocol)
|
item := NewProtocolItem(options.Protocol)
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
|
if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
|
||||||
item := NewDomainItem(options.Domain, options.DomainSuffix)
|
item := NewDomainItem(options.Domain, options.DomainSuffix)
|
||||||
rule.items = append(rule.items, item)
|
rule.addressItems = append(rule.addressItems, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.DomainKeyword) > 0 {
|
if len(options.DomainKeyword) > 0 {
|
||||||
item := NewDomainKeywordItem(options.DomainKeyword)
|
item := NewDomainKeywordItem(options.DomainKeyword)
|
||||||
rule.items = append(rule.items, item)
|
rule.addressItems = append(rule.addressItems, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.DomainRegex) > 0 {
|
if len(options.DomainRegex) > 0 {
|
||||||
item, err := NewDomainRegexItem(options.DomainRegex)
|
item, err := NewDomainRegexItem(options.DomainRegex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "domain_regex")
|
return nil, E.Cause(err, "domain_regex")
|
||||||
}
|
}
|
||||||
rule.items = append(rule.items, item)
|
rule.addressItems = append(rule.addressItems, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.Geosite) > 0 {
|
if len(options.Geosite) > 0 {
|
||||||
item := NewGeositeItem(router, logger, options.Geosite)
|
item := NewGeositeItem(router, logger, options.Geosite)
|
||||||
rule.items = append(rule.items, item)
|
rule.addressItems = append(rule.addressItems, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.SourceGeoIP) > 0 {
|
if len(options.SourceGeoIP) > 0 {
|
||||||
item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
|
item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.SourceIPCIDR) > 0 {
|
if len(options.SourceIPCIDR) > 0 {
|
||||||
item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
|
item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
|
||||||
|
@ -95,24 +105,28 @@ func NewDefaultDNSRule(router adapter.Router, logger log.Logger, options option.
|
||||||
return nil, E.Cause(err, "source_ipcidr")
|
return nil, E.Cause(err, "source_ipcidr")
|
||||||
}
|
}
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.SourcePort) > 0 {
|
if len(options.SourcePort) > 0 {
|
||||||
item := NewPortItem(true, options.SourcePort)
|
item := NewPortItem(true, options.SourcePort)
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.Port) > 0 {
|
if len(options.Port) > 0 {
|
||||||
item := NewPortItem(false, options.Port)
|
item := NewPortItem(false, options.Port)
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
if len(options.Outbound) > 0 {
|
if len(options.Outbound) > 0 {
|
||||||
item := NewOutboundRule(options.Outbound)
|
item := NewOutboundRule(options.Outbound)
|
||||||
rule.items = append(rule.items, item)
|
rule.items = append(rule.items, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
return rule, nil
|
return rule, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultDNSRule) Start() error {
|
func (r *DefaultDNSRule) Start() error {
|
||||||
for _, item := range r.items {
|
for _, item := range r.allItems {
|
||||||
err := common.Start(item)
|
err := common.Start(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -122,7 +136,7 @@ func (r *DefaultDNSRule) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultDNSRule) Close() error {
|
func (r *DefaultDNSRule) Close() error {
|
||||||
for _, item := range r.items {
|
for _, item := range r.allItems {
|
||||||
err := common.Close(item)
|
err := common.Close(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -132,7 +146,7 @@ func (r *DefaultDNSRule) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultDNSRule) UpdateGeosite() error {
|
func (r *DefaultDNSRule) UpdateGeosite() error {
|
||||||
for _, item := range r.items {
|
for _, item := range r.allItems {
|
||||||
if geositeItem, isSite := item.(*GeositeItem); isSite {
|
if geositeItem, isSite := item.(*GeositeItem); isSite {
|
||||||
err := geositeItem.Update()
|
err := geositeItem.Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -149,6 +163,18 @@ func (r *DefaultDNSRule) Match(metadata *adapter.InboundContext) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(r.addressItems) > 0 {
|
||||||
|
var addressMatch bool
|
||||||
|
for _, item := range r.addressItems {
|
||||||
|
if item.Match(metadata) {
|
||||||
|
addressMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !addressMatch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +183,7 @@ func (r *DefaultDNSRule) Outbound() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DefaultDNSRule) String() string {
|
func (r *DefaultDNSRule) String() string {
|
||||||
return strings.Join(common.Map(r.items, F.ToString0[RuleItem]), " ")
|
return strings.Join(common.Map(r.allItems, F.ToString0[RuleItem]), " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ adapter.Rule = (*LogicalRule)(nil)
|
var _ adapter.Rule = (*LogicalRule)(nil)
|
||||||
|
|
|
@ -7,14 +7,18 @@ import (
|
||||||
|
|
||||||
"github.com/sagernet/sing-box"
|
"github.com/sagernet/sing-box"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
|
"github.com/sagernet/sing/common/control"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
"github.com/sagernet/sing/protocol/socks"
|
"github.com/sagernet/sing/protocol/socks"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mkPort(t *testing.T) uint16 {
|
func mkPort(t *testing.T) uint16 {
|
||||||
|
var lc net.ListenConfig
|
||||||
|
lc.Control = control.ReuseAddr()
|
||||||
for {
|
for {
|
||||||
tcpListener, err := net.ListenTCP("tcp", nil)
|
tcpListener, err := net.ListenTCP("tcp", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -36,6 +40,7 @@ func startInstance(t *testing.T, options option.Options) {
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
instance.Close()
|
instance.Close()
|
||||||
})
|
})
|
||||||
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
|
func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DockerOptions struct {
|
type DockerOptions struct {
|
||||||
|
@ -64,6 +65,7 @@ func startDockerContainer(t *testing.T, options DockerOptions) {
|
||||||
go func() {
|
go func() {
|
||||||
attach.Reader.WriteTo(os.Stderr)
|
attach.Reader.WriteTo(os.Stderr)
|
||||||
}()*/
|
}()*/
|
||||||
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanContainer(id string) error {
|
func cleanContainer(id string) error {
|
||||||
|
|
Loading…
Reference in a new issue