Fix: listener patch diff

This commit is contained in:
gVisor bot 2021-08-01 00:35:37 +08:00
parent 6ba3a17d7b
commit 3317beb191
9 changed files with 158 additions and 88 deletions

7
constant/listener.go Normal file
View file

@ -0,0 +1,7 @@
package constant
type Listener interface {
RawAddress() string
Address() string
Close() error
}

View file

@ -10,9 +10,26 @@ import (
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string
closed bool closed bool
} }
// RawAddress implements C.Listener
func (l *Listener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
// Close implements C.Listener
func (l *Listener) Close() error {
l.closed = true
return l.listener.Close()
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithAuthenticate(addr, in, true) return NewWithAuthenticate(addr, in, true)
} }
@ -30,6 +47,7 @@ func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool
hl := &Listener{ hl := &Listener{
listener: l, listener: l,
addr: addr,
} }
go func() { go func() {
for { for {
@ -46,12 +64,3 @@ func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool
return hl, nil return hl, nil
} }
func (l *Listener) Close() {
l.closed = true
l.listener.Close()
}
func (l *Listener) Address() string {
return l.listener.Addr().String()
}

View file

@ -20,15 +20,15 @@ var (
allowLan = false allowLan = false
bindAddress = "*" bindAddress = "*"
socksListener *socks.Listener socksListener C.Listener
socksUDPListener *socks.UDPListener socksUDPListener C.Listener
httpListener *http.Listener httpListener C.Listener
redirListener *redir.Listener redirListener C.Listener
redirUDPListener *tproxy.UDPListener redirUDPListener C.Listener
tproxyListener *tproxy.Listener tproxyListener C.Listener
tproxyUDPListener *tproxy.UDPListener tproxyUDPListener C.Listener
mixedListener *mixed.Listener mixedListener C.Listener
mixedUDPLister *socks.UDPListener mixedUDPLister C.Listener
// lock for recreate function // lock for recreate function
socksMux sync.Mutex socksMux sync.Mutex
@ -69,7 +69,7 @@ func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) error {
addr := genAddr(bindAddress, port, allowLan) addr := genAddr(bindAddress, port, allowLan)
if httpListener != nil { if httpListener != nil {
if httpListener.Address() == addr { if httpListener.RawAddress() == addr {
return nil return nil
} }
httpListener.Close() httpListener.Close()
@ -100,7 +100,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
shouldUDPIgnore := false shouldUDPIgnore := false
if socksListener != nil { if socksListener != nil {
if socksListener.Address() != addr { if socksListener.RawAddress() != addr {
socksListener.Close() socksListener.Close()
socksListener = nil socksListener = nil
} else { } else {
@ -109,7 +109,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
} }
if socksUDPListener != nil { if socksUDPListener != nil {
if socksUDPListener.Address() != addr { if socksUDPListener.RawAddress() != addr {
socksUDPListener.Close() socksUDPListener.Close()
socksUDPListener = nil socksUDPListener = nil
} else { } else {
@ -150,7 +150,7 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
addr := genAddr(bindAddress, port, allowLan) addr := genAddr(bindAddress, port, allowLan)
if redirListener != nil { if redirListener != nil {
if redirListener.Address() == addr { if redirListener.RawAddress() == addr {
return nil return nil
} }
redirListener.Close() redirListener.Close()
@ -158,7 +158,7 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
} }
if redirUDPListener != nil { if redirUDPListener != nil {
if redirUDPListener.Address() == addr { if redirUDPListener.RawAddress() == addr {
return nil return nil
} }
redirUDPListener.Close() redirUDPListener.Close()
@ -191,7 +191,7 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
addr := genAddr(bindAddress, port, allowLan) addr := genAddr(bindAddress, port, allowLan)
if tproxyListener != nil { if tproxyListener != nil {
if tproxyListener.Address() == addr { if tproxyListener.RawAddress() == addr {
return nil return nil
} }
tproxyListener.Close() tproxyListener.Close()
@ -199,7 +199,7 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
} }
if tproxyUDPListener != nil { if tproxyUDPListener != nil {
if tproxyUDPListener.Address() == addr { if tproxyUDPListener.RawAddress() == addr {
return nil return nil
} }
tproxyUDPListener.Close() tproxyUDPListener.Close()
@ -235,7 +235,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
shouldUDPIgnore := false shouldUDPIgnore := false
if mixedListener != nil { if mixedListener != nil {
if mixedListener.Address() != addr { if mixedListener.RawAddress() != addr {
mixedListener.Close() mixedListener.Close()
mixedListener = nil mixedListener = nil
} else { } else {
@ -243,7 +243,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
} }
} }
if mixedUDPLister != nil { if mixedUDPLister != nil {
if mixedUDPLister.Address() != addr { if mixedUDPLister.RawAddress() != addr {
mixedUDPLister.Close() mixedUDPLister.Close()
mixedUDPLister = nil mixedUDPLister = nil
} else { } else {

View file

@ -15,8 +15,25 @@ import (
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
closed bool addr string
cache *cache.Cache cache *cache.Cache
closed bool
}
// RawAddress implements C.Listener
func (l *Listener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
// Close implements C.Listener
func (l *Listener) Close() error {
l.closed = true
return l.listener.Close()
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
@ -27,6 +44,7 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
ml := &Listener{ ml := &Listener{
listener: l, listener: l,
addr: addr,
cache: cache.New(30 * time.Second), cache: cache.New(30 * time.Second),
} }
go func() { go func() {
@ -45,15 +63,6 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return ml, nil return ml, nil
} }
func (l *Listener) Close() {
l.closed = true
l.listener.Close()
}
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) { func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
bufConn := N.NewBufferedConn(conn) bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1) head, err := bufConn.Peek(1)

View file

@ -9,9 +9,26 @@ import (
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string
closed bool closed bool
} }
// RawAddress implements C.Listener
func (l *Listener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
// Close implements C.Listener
func (l *Listener) Close() error {
l.closed = true
return l.listener.Close()
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
@ -19,6 +36,7 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
} }
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr,
} }
go func() { go func() {
@ -37,15 +55,6 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return rl, nil return rl, nil
} }
func (l *Listener) Close() {
l.closed = true
l.listener.Close()
}
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
func handleRedir(conn net.Conn, in chan<- C.ConnContext) { func handleRedir(conn net.Conn, in chan<- C.ConnContext) {
target, err := parserPacket(conn) target, err := parserPacket(conn)
if err != nil { if err != nil {

View file

@ -15,9 +15,26 @@ import (
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string
closed bool closed bool
} }
// RawAddress implements C.Listener
func (l *Listener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
// Close implements C.Listener
func (l *Listener) Close() error {
l.closed = true
return l.listener.Close()
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
@ -26,6 +43,7 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
sl := &Listener{ sl := &Listener{
listener: l, listener: l,
addr: addr,
} }
go func() { go func() {
for { for {
@ -43,15 +61,6 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return sl, nil return sl, nil
} }
func (l *Listener) Close() {
l.closed = true
l.listener.Close()
}
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
func handleSocks(conn net.Conn, in chan<- C.ConnContext) { func handleSocks(conn net.Conn, in chan<- C.ConnContext) {
bufConn := N.NewBufferedConn(conn) bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1) head, err := bufConn.Peek(1)

View file

@ -13,9 +13,26 @@ import (
type UDPListener struct { type UDPListener struct {
packetConn net.PacketConn packetConn net.PacketConn
addr string
closed bool closed bool
} }
// RawAddress implements C.Listener
func (l *UDPListener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *UDPListener) Address() string {
return l.packetConn.LocalAddr().String()
}
// Close implements C.Listener
func (l *UDPListener) Close() error {
l.closed = true
return l.packetConn.Close()
}
func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) { func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr) l, err := net.ListenPacket("udp", addr)
if err != nil { if err != nil {
@ -28,6 +45,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
sl := &UDPListener{ sl := &UDPListener{
packetConn: l, packetConn: l,
addr: addr,
} }
go func() { go func() {
for { for {
@ -47,15 +65,6 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
return sl, nil return sl, nil
} }
func (l *UDPListener) Close() error {
l.closed = true
return l.packetConn.Close()
}
func (l *UDPListener) Address() string {
return l.packetConn.LocalAddr().String()
}
func handleSocksUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, addr net.Addr) { func handleSocksUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, addr net.Addr) {
target, payload, err := socks5.DecodeUDPPacket(buf) target, payload, err := socks5.DecodeUDPPacket(buf)
if err != nil { if err != nil {

View file

@ -10,9 +10,32 @@ import (
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string
closed bool closed bool
} }
// RawAddress implements C.Listener
func (l *Listener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
// Close implements C.Listener
func (l *Listener) Close() error {
l.closed = true
return l.listener.Close()
}
func (l *Listener) handleTProxy(conn net.Conn, in chan<- C.ConnContext) {
target := socks5.ParseAddrToSocksAddr(conn.LocalAddr())
conn.(*net.TCPConn).SetKeepAlive(true)
in <- inbound.NewSocket(target, conn, C.TPROXY)
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
@ -32,6 +55,7 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr,
} }
go func() { go func() {
@ -49,18 +73,3 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return rl, nil return rl, nil
} }
func (l *Listener) Close() {
l.closed = true
l.listener.Close()
}
func (l *Listener) Address() string {
return l.listener.Addr().String()
}
func (l *Listener) handleTProxy(conn net.Conn, in chan<- C.ConnContext) {
target := socks5.ParseAddrToSocksAddr(conn.LocalAddr())
conn.(*net.TCPConn).SetKeepAlive(true)
in <- inbound.NewSocket(target, conn, C.TPROXY)
}

View file

@ -11,9 +11,26 @@ import (
type UDPListener struct { type UDPListener struct {
packetConn net.PacketConn packetConn net.PacketConn
addr string
closed bool closed bool
} }
// RawAddress implements C.Listener
func (l *UDPListener) RawAddress() string {
return l.addr
}
// Address implements C.Listener
func (l *UDPListener) Address() string {
return l.packetConn.LocalAddr().String()
}
// Close implements C.Listener
func (l *UDPListener) Close() error {
l.closed = true
return l.packetConn.Close()
}
func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) { func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr) l, err := net.ListenPacket("udp", addr)
if err != nil { if err != nil {
@ -22,6 +39,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
rl := &UDPListener{ rl := &UDPListener{
packetConn: l, packetConn: l,
addr: addr,
} }
c := l.(*net.UDPConn) c := l.(*net.UDPConn)
@ -60,15 +78,6 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
return rl, nil return rl, nil
} }
func (l *UDPListener) Close() error {
l.closed = true
return l.packetConn.Close()
}
func (l *UDPListener) Address() string {
return l.packetConn.LocalAddr().String()
}
func handlePacketConn(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) { func handlePacketConn(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {
target := socks5.ParseAddrToSocksAddr(rAddr) target := socks5.ParseAddrToSocksAddr(rAddr)
pkt := &packet{ pkt := &packet{