Files
dae/component/outbound/dialer/dialer.go

124 lines
3.1 KiB
Go
Raw Normal View History

/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
*/
2023-01-23 19:54:21 +08:00
package dialer
import (
"context"
2023-01-23 19:54:21 +08:00
"fmt"
"sync"
"time"
"unsafe"
"github.com/daeuniverse/dae/common"
"github.com/daeuniverse/dae/config"
D "github.com/daeuniverse/outbound/dialer"
"github.com/daeuniverse/outbound/netproxy"
"github.com/sirupsen/logrus"
2023-01-23 19:54:21 +08:00
)
var (
UnexpectedFieldErr = fmt.Errorf("unexpected field")
InvalidParameterErr = fmt.Errorf("invalid parameters")
2023-01-23 19:54:21 +08:00
)
type Dialer struct {
2023-01-28 01:50:21 +08:00
*GlobalOption
InstanceOption
netproxy.Dialer
property *Property
collectionFineMu sync.Mutex
collections [6]*collection
2023-01-23 19:54:21 +08:00
tickerMu sync.Mutex
ticker *time.Ticker
checkCh chan time.Time
ctx context.Context
cancel context.CancelFunc
checkActivated bool
2023-01-23 19:54:21 +08:00
}
2023-01-28 01:50:21 +08:00
type GlobalOption struct {
D.ExtraOption
Log *logrus.Logger
TcpCheckOptionRaw TcpCheckOptionRaw // Lazy parse
CheckDnsOptionRaw CheckDnsOptionRaw // Lazy parse
CheckInterval time.Duration
CheckTolerance time.Duration
CheckDnsTcp bool
2023-01-28 01:50:21 +08:00
}
2023-01-28 15:47:43 +08:00
type InstanceOption struct {
DisableCheck bool
2023-01-23 19:54:21 +08:00
}
2023-02-28 21:25:15 +08:00
type Property struct {
D.Property
SubscriptionTag string
2023-02-28 21:25:15 +08:00
}
type AliveDialerSetSet map[*AliveDialerSet]int
func NewGlobalOption(global *config.Global, log *logrus.Logger) *GlobalOption {
return &GlobalOption{
ExtraOption: D.ExtraOption{
AllowInsecure: global.AllowInsecure,
TlsImplementation: global.TlsImplementation,
UtlsImitate: global.UtlsImitate},
Log: log,
TcpCheckOptionRaw: TcpCheckOptionRaw{Raw: global.TcpCheckUrl, Log: log, ResolverNetwork: common.MagicNetwork("udp", global.SoMarkFromDae, global.Mptcp), Method: global.TcpCheckHttpMethod},
CheckDnsOptionRaw: CheckDnsOptionRaw{Raw: global.UdpCheckDns, ResolverNetwork: common.MagicNetwork("udp", global.SoMarkFromDae, global.Mptcp), Somark: global.SoMarkFromDae},
CheckInterval: global.CheckInterval,
CheckTolerance: global.CheckTolerance,
CheckDnsTcp: true,
}
}
2023-01-28 15:47:43 +08:00
// NewDialer is for register in general.
func NewDialer(dialer netproxy.Dialer, option *GlobalOption, iOption InstanceOption, property *Property) *Dialer {
var collections [6]*collection
for i := range collections {
collections[i] = newCollection()
}
ctx, cancel := context.WithCancel(context.Background())
2023-01-23 19:54:21 +08:00
d := &Dialer{
GlobalOption: option,
2023-07-10 19:44:56 +08:00
InstanceOption: iOption,
Dialer: dialer,
2023-02-28 21:25:15 +08:00
property: property,
collectionFineMu: sync.Mutex{},
collections: collections,
tickerMu: sync.Mutex{},
ticker: nil,
checkCh: make(chan time.Time, 1),
ctx: ctx,
cancel: cancel,
2023-01-23 19:54:21 +08:00
}
option.Log.WithField("dialer", d.Property().Name).
WithField("p", unsafe.Pointer(d)).
Traceln("NewDialer")
2023-01-23 19:54:21 +08:00
return d
}
func (d *Dialer) Clone() *Dialer {
return NewDialer(d.Dialer, d.GlobalOption, d.InstanceOption, d.property)
}
2023-01-23 19:54:21 +08:00
func (d *Dialer) Close() error {
d.cancel()
2023-01-23 19:54:21 +08:00
d.tickerMu.Lock()
if d.ticker != nil {
d.ticker.Stop()
}
2023-01-23 19:54:21 +08:00
d.tickerMu.Unlock()
return nil
}
func (d *Dialer) Property() *Property {
2023-02-28 21:25:15 +08:00
return d.property
2023-01-23 19:54:21 +08:00
}