chore: rebuild add adapter/inbound.Addition to simply Listener.New apis

This commit is contained in:
wwqgtxx
2022-12-05 00:20:50 +08:00
parent c7f83d3ff1
commit 2e22c712af
30 changed files with 290 additions and 225 deletions

View File

@ -6,6 +6,7 @@ import (
"net/netip"
"strconv"
"github.com/Dreamacro/clash/adapter/inbound"
C "github.com/Dreamacro/clash/constant"
)
@ -28,7 +29,7 @@ func NewBase(options *BaseOption) (*Base, error) {
return &Base{
name: options.Name(),
listenAddr: addr,
specialRules: options.PreferRulesName,
specialRules: options.SpecialRules,
port: options.Port,
config: options,
}, nil
@ -64,13 +65,17 @@ func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) er
return nil
}
func (b *Base) Additions() []inbound.Addition {
return b.config.Additions()
}
var _ C.InboundListener = (*Base)(nil)
type BaseOption struct {
NameStr string `inbound:"name"`
Listen string `inbound:"listen,omitempty"`
Port int `inbound:"port"`
PreferRulesName string `inbound:"rule,omitempty"`
NameStr string `inbound:"name"`
Listen string `inbound:"listen,omitempty"`
Port int `inbound:"port"`
SpecialRules string `inbound:"rule,omitempty"`
}
func (o BaseOption) Name() string {
@ -81,6 +86,13 @@ func (o BaseOption) Equal(config C.InboundConfig) bool {
return optionToString(o) == optionToString(config)
}
func (o BaseOption) Additions() []inbound.Addition {
return []inbound.Addition{{
InName: o.NameStr,
SpecialRules: o.SpecialRules,
}}
}
var _ C.InboundConfig = (*BaseOption)(nil)
func optionToString(option any) string {

View File

@ -44,7 +44,7 @@ func (h *HTTP) Address() string {
// Listen implements constant.InboundListener
func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.specialRules, tcpIn)
h.l, err = http.New(h.RawAddress(), tcpIn, h.Additions()...)
if err != nil {
return err
}

View File

@ -52,12 +52,12 @@ func (m *Mixed) Address() string {
// Listen implements constant.InboundListener
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.specialRules, tcpIn)
m.l, err = mixed.New(m.RawAddress(), tcpIn, m.Additions()...)
if err != nil {
return err
}
if m.udp {
m.lUDP, err = socks.NewUDPWithInfos(m.Address(), m.name, m.specialRules, udpIn)
m.lUDP, err = socks.NewUDP(m.Address(), udpIn, m.Additions()...)
if err != nil {
return err
}

View File

@ -44,7 +44,7 @@ func (r *Redir) Address() string {
// Listen implements constant.InboundListener
func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
r.l, err = redir.NewWithInfos(r.Address(), r.name, r.specialRules, tcpIn)
r.l, err = redir.New(r.Address(), tcpIn, r.Additions()...)
if err != nil {
return err
}

View File

@ -70,11 +70,11 @@ func (s *Socks) Address() string {
// Listen implements constant.InboundListener
func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.specialRules, tcpIn); err != nil {
if s.stl, err = socks.New(s.RawAddress(), tcpIn, s.Additions()...); err != nil {
return err
}
if s.udp {
if s.sul, err = socks.NewUDPWithInfos(s.RawAddress(), s.name, s.specialRules, udpIn); err != nil {
if s.sul, err = socks.NewUDP(s.RawAddress(), udpIn, s.Additions()...); err != nil {
return err
}
}

View File

@ -51,13 +51,13 @@ func (t *TProxy) Address() string {
// Listen implements constant.InboundListener
func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.specialRules, tcpIn)
t.lTCP, err = tproxy.New(t.RawAddress(), tcpIn, t.Additions()...)
if err != nil {
return err
}
if t.udp {
if t.lUDP != nil {
t.lUDP, err = tproxy.NewUDPWithInfos(t.Address(), t.name, t.specialRules, udpIn)
t.lUDP, err = tproxy.NewUDP(t.Address(), udpIn, t.Additions()...)
if err != nil {
return err
}

View File

@ -59,18 +59,23 @@ func (t *Tuic) Address() string {
// Listen implements constant.InboundListener
func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
t.l, err = tuic.New(LC.TuicServer{
Enable: true,
Listen: t.RawAddress(),
Token: t.config.Token,
Certificate: t.config.Certificate,
PrivateKey: t.config.PrivateKey,
CongestionController: t.config.CongestionController,
MaxIdleTime: t.config.MaxIdleTime,
AuthenticationTimeout: t.config.AuthenticationTimeout,
ALPN: t.config.ALPN,
MaxUdpRelayPacketSize: t.config.MaxUdpRelayPacketSize,
}, tcpIn, udpIn)
t.l, err = tuic.New(
LC.TuicServer{
Enable: true,
Listen: t.RawAddress(),
Token: t.config.Token,
Certificate: t.config.Certificate,
PrivateKey: t.config.PrivateKey,
CongestionController: t.config.CongestionController,
MaxIdleTime: t.config.MaxIdleTime,
AuthenticationTimeout: t.config.AuthenticationTimeout,
ALPN: t.config.ALPN,
MaxUdpRelayPacketSize: t.config.MaxUdpRelayPacketSize,
},
tcpIn,
udpIn,
t.Additions()...,
)
if err != nil {
return err
}