2023-12-01 13:24:12 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2024-06-07 15:55:21 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2023-12-01 13:24:12 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
"github.com/sagernet/sing/common/logger"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2024-06-07 15:55:21 +08:00
|
|
|
|
|
|
|
"go4.org/netipx"
|
2023-12-01 13:24:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewRuleSet(ctx context.Context, router adapter.Router, logger logger.ContextLogger, options option.RuleSet) (adapter.RuleSet, error) {
|
|
|
|
switch options.Type {
|
2024-06-26 00:43:51 +08:00
|
|
|
case C.RuleSetTypeInline, C.RuleSetTypeLocal, "":
|
|
|
|
return NewLocalRuleSet(ctx, router, logger, options)
|
2023-12-01 13:24:12 +08:00
|
|
|
case C.RuleSetTypeRemote:
|
|
|
|
return NewRemoteRuleSet(ctx, router, logger, options), nil
|
|
|
|
default:
|
2024-06-26 00:45:10 +08:00
|
|
|
return nil, E.New("unknown rule-set type: ", options.Type)
|
2023-12-01 13:24:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-07 15:55:21 +08:00
|
|
|
func extractIPSetFromRule(rawRule adapter.HeadlessRule) []*netipx.IPSet {
|
|
|
|
switch rule := rawRule.(type) {
|
|
|
|
case *DefaultHeadlessRule:
|
|
|
|
return common.FlatMap(rule.destinationIPCIDRItems, func(rawItem RuleItem) []*netipx.IPSet {
|
|
|
|
switch item := rawItem.(type) {
|
|
|
|
case *IPCIDRItem:
|
|
|
|
return []*netipx.IPSet{item.ipSet}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
})
|
|
|
|
case *LogicalHeadlessRule:
|
|
|
|
return common.FlatMap(rule.rules, extractIPSetFromRule)
|
|
|
|
default:
|
|
|
|
panic("unexpected rule type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 13:24:12 +08:00
|
|
|
var _ adapter.RuleSetStartContext = (*RuleSetStartContext)(nil)
|
|
|
|
|
|
|
|
type RuleSetStartContext struct {
|
|
|
|
access sync.Mutex
|
|
|
|
httpClientCache map[string]*http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRuleSetStartContext() *RuleSetStartContext {
|
|
|
|
return &RuleSetStartContext{
|
|
|
|
httpClientCache: make(map[string]*http.Client),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RuleSetStartContext) HTTPClient(detour string, dialer N.Dialer) *http.Client {
|
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
|
|
|
if httpClient, loaded := c.httpClientCache[detour]; loaded {
|
|
|
|
return httpClient
|
|
|
|
}
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
ForceAttemptHTTP2: true,
|
|
|
|
TLSHandshakeTimeout: C.TCPTimeout,
|
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.httpClientCache[detour] = httpClient
|
|
|
|
return httpClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RuleSetStartContext) Close() {
|
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
|
|
|
for _, client := range c.httpClientCache {
|
|
|
|
client.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
}
|