chore: support skip the same config Listeners' Close and Listen

This commit is contained in:
gVisor bot 2022-12-04 17:20:24 +08:00
parent 1bcd6cac83
commit e01cfc5627
9 changed files with 94 additions and 32 deletions

View file

@ -20,4 +20,5 @@ type NewListener interface {
Close() error Close() error
Address() string Address() string
RawAddress() string RawAddress() string
Config() string
} }

View file

@ -138,13 +138,25 @@ func GetGeneral() *config.General {
func updateListeners(listeners map[string]C.NewListener) { func updateListeners(listeners map[string]C.NewListener) {
tcpIn := tunnel.TCPIn() tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn() udpIn := tunnel.UDPIn()
for _, listener := range tunnel.Listeners() {
_ = listener.Close() skipNames := map[string]struct{}{}
for name, oldListener := range tunnel.Listeners() {
if newListener, ok := listeners[name]; ok {
if newListener.Config() == oldListener.Config() {
listeners[name] = oldListener
skipNames[name] = struct{}{}
continue
}
}
_ = oldListener.Close()
} }
for _, listener := range listeners { for name, newListener := range listeners {
if err := listener.Listen(tcpIn, udpIn); err != nil { if _, ok := skipNames[name]; ok {
log.Errorln("Listener %s listen err: %s", listener.Name(), err.Error()) continue
}
if err := newListener.Listen(tcpIn, udpIn); err != nil {
log.Errorln("Listener %s listen err: %s", newListener.Name(), err.Error())
} }
} }
tunnel.UpdateListeners(listeners) tunnel.UpdateListeners(listeners)

View file

@ -1,6 +1,7 @@
package inbound package inbound
import ( import (
"encoding/json"
"net" "net"
"net/netip" "net/netip"
"strconv" "strconv"
@ -9,6 +10,7 @@ import (
) )
type Base struct { type Base struct {
config *BaseOption
name string name string
preferRulesName string preferRulesName string
listenAddr netip.Addr listenAddr netip.Addr
@ -28,9 +30,15 @@ func NewBase(options *BaseOption) (*Base, error) {
listenAddr: addr, listenAddr: addr,
preferRulesName: options.PreferRulesName, preferRulesName: options.PreferRulesName,
port: options.Port, port: options.Port,
config: options,
}, nil }, nil
} }
// Config implements constant.NewListener
func (b *Base) Config() string {
return optionToString(b.config)
}
// Address implements constant.NewListener // Address implements constant.NewListener
func (b *Base) Address() string { func (b *Base) Address() string {
return b.RawAddress() return b.RawAddress()
@ -51,7 +59,7 @@ func (b *Base) RawAddress() string {
return net.JoinHostPort(b.listenAddr.String(), strconv.Itoa(int(b.port))) return net.JoinHostPort(b.listenAddr.String(), strconv.Itoa(int(b.port)))
} }
// ReCreate implements constant.NewListener // Listen implements constant.NewListener
func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
return nil return nil
} }
@ -64,3 +72,8 @@ type BaseOption struct {
} }
var _ C.NewListener = (*Base)(nil) var _ C.NewListener = (*Base)(nil)
func optionToString(option any) string {
str, _ := json.Marshal(option)
return string(str)
}

View file

@ -11,6 +11,7 @@ type HTTPOption struct {
} }
type HTTP struct { type HTTP struct {
*Base *Base
config *HTTPOption
l *http.Listener l *http.Listener
} }
@ -21,15 +22,21 @@ func NewHTTP(options *HTTPOption) (*HTTP, error) {
} }
return &HTTP{ return &HTTP{
Base: base, Base: base,
config: options,
}, nil }, nil
} }
// Config implements constant.NewListener
func (h *HTTP) Config() string {
return optionToString(h.config)
}
// Address implements constant.NewListener // Address implements constant.NewListener
func (h *HTTP) Address() string { func (h *HTTP) Address() string {
return h.l.Address() return h.l.Address()
} }
// ReCreate implements constant.NewListener // Listen implements constant.NewListener
func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.preferRulesName, tcpIn) h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.preferRulesName, tcpIn)

View file

@ -17,6 +17,7 @@ type MixedOption struct {
type Mixed struct { type Mixed struct {
*Base *Base
config *MixedOption
l *mixed.Listener l *mixed.Listener
lUDP *socks.UDPListener lUDP *socks.UDPListener
udp bool udp bool
@ -29,16 +30,22 @@ func NewMixed(options *MixedOption) (*Mixed, error) {
} }
return &Mixed{ return &Mixed{
Base: base, Base: base,
config: options,
udp: options.UDP == nil || *options.UDP, udp: options.UDP == nil || *options.UDP,
}, nil }, nil
} }
// Config implements constant.NewListener
func (m *Mixed) Config() string {
return optionToString(m.config)
}
// Address implements constant.NewListener // Address implements constant.NewListener
func (m *Mixed) Address() string { func (m *Mixed) Address() string {
return m.l.Address() return m.l.Address()
} }
// ReCreate implements constant.NewListener // Listen implements constant.NewListener
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.preferRulesName, tcpIn) m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.preferRulesName, tcpIn)

View file

@ -12,6 +12,7 @@ type RedirOption struct {
type Redir struct { type Redir struct {
*Base *Base
config *RedirOption
l *redir.Listener l *redir.Listener
} }
@ -22,15 +23,21 @@ func NewRedir(options *RedirOption) (*Redir, error) {
} }
return &Redir{ return &Redir{
Base: base, Base: base,
config: options,
}, nil }, nil
} }
// Config implements constant.NewListener
func (r *Redir) Config() string {
return optionToString(r.config)
}
// Address implements constant.NewListener // Address implements constant.NewListener
func (r *Redir) Address() string { func (r *Redir) Address() string {
return r.l.Address() return r.l.Address()
} }
// ReCreate implements constant.NewListener // Listen implements constant.NewListener
func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
r.l, err = redir.NewWithInfos(r.Address(), r.name, r.preferRulesName, tcpIn) r.l, err = redir.NewWithInfos(r.Address(), r.name, r.preferRulesName, tcpIn)

View file

@ -14,6 +14,7 @@ type SocksOption struct {
type Socks struct { type Socks struct {
*Base *Base
config *SocksOption
udp bool udp bool
stl *socks.Listener stl *socks.Listener
sul *socks.UDPListener sul *socks.UDPListener
@ -26,10 +27,16 @@ func NewSocks(options *SocksOption) (*Socks, error) {
} }
return &Socks{ return &Socks{
Base: base, Base: base,
config: options,
udp: options.UDP == nil || *options.UDP, udp: options.UDP == nil || *options.UDP,
}, nil }, nil
} }
// Config implements constant.NewListener
func (s *Socks) Config() string {
return optionToString(s.config)
}
// Close implements constant.NewListener // Close implements constant.NewListener
func (s *Socks) Close() error { func (s *Socks) Close() error {
var err error var err error
@ -56,7 +63,7 @@ func (s *Socks) Address() string {
return s.stl.Address() return s.stl.Address()
} }
// ReCreate implements constant.NewListener // Listen implements constant.NewListener
func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.preferRulesName, tcpIn); err != nil { if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.preferRulesName, tcpIn); err != nil {

View file

@ -15,6 +15,7 @@ type TProxyOption struct {
type TProxy struct { type TProxy struct {
*Base *Base
config *TProxyOption
lUDP *tproxy.UDPListener lUDP *tproxy.UDPListener
lTCP *tproxy.Listener lTCP *tproxy.Listener
udp bool udp bool
@ -27,17 +28,23 @@ func NewTProxy(options *TProxyOption) (*TProxy, error) {
} }
return &TProxy{ return &TProxy{
Base: base, Base: base,
config: options,
udp: options.UDP == nil || *options.UDP, udp: options.UDP == nil || *options.UDP,
}, nil }, nil
} }
// Config implements constant.NewListener
func (t *TProxy) Config() string {
return optionToString(t.config)
}
// Address implements constant.NewListener // Address implements constant.NewListener
func (t *TProxy) Address() string { func (t *TProxy) Address() string {
return t.lTCP.Address() return t.lTCP.Address()
} }
// ReCreate implements constant.NewListener // Listen implements constant.NewListener
func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.preferRulesName, tcpIn) t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.preferRulesName, tcpIn)

View file

@ -90,6 +90,7 @@ func Rules() []C.Rule {
func Listeners() map[string]C.NewListener { func Listeners() map[string]C.NewListener {
return listeners return listeners
} }
// UpdateRules handle update rules // UpdateRules handle update rules
func UpdateRules(newRules []C.Rule, newSubRule map[string][]C.Rule, rp map[string]provider.RuleProvider) { func UpdateRules(newRules []C.Rule, newSubRule map[string][]C.Rule, rp map[string]provider.RuleProvider) {
configMux.Lock() configMux.Lock()