2018-02-05 23:38:24 +01:00
|
|
|
package commander
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-02-08 15:39:46 +01:00
|
|
|
"sync"
|
2018-02-05 23:38:24 +01:00
|
|
|
|
2021-02-17 04:31:50 +08:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/signal/done"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport"
|
2018-02-05 23:38:24 +01:00
|
|
|
)
|
|
|
|
|
2018-04-14 13:09:58 +02:00
|
|
|
// OutboundListener is a net.Listener for listening gRPC connections.
|
2018-02-05 23:38:24 +01:00
|
|
|
type OutboundListener struct {
|
|
|
|
buffer chan net.Conn
|
2018-05-27 14:42:53 +02:00
|
|
|
done *done.Instance
|
2018-02-05 23:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *OutboundListener) add(conn net.Conn) {
|
|
|
|
select {
|
|
|
|
case l.buffer <- conn:
|
2018-04-15 20:40:47 +02:00
|
|
|
case <-l.done.Wait():
|
2020-10-11 19:22:46 +08:00
|
|
|
conn.Close()
|
2018-02-05 23:38:24 +01:00
|
|
|
default:
|
2020-10-11 19:22:46 +08:00
|
|
|
conn.Close()
|
2018-02-05 23:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:32:40 +02:00
|
|
|
// Accept implements net.Listener.
|
2018-02-05 23:38:24 +01:00
|
|
|
func (l *OutboundListener) Accept() (net.Conn, error) {
|
2018-02-08 23:37:47 +01:00
|
|
|
select {
|
2018-04-15 20:40:47 +02:00
|
|
|
case <-l.done.Wait():
|
2018-03-15 10:32:10 +08:00
|
|
|
return nil, newError("listen closed")
|
2018-02-08 23:37:47 +01:00
|
|
|
case c := <-l.buffer:
|
|
|
|
return c, nil
|
2018-02-05 23:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:32:40 +02:00
|
|
|
// Close implement net.Listener.
|
2018-02-05 23:38:24 +01:00
|
|
|
func (l *OutboundListener) Close() error {
|
2018-04-04 21:32:40 +02:00
|
|
|
common.Must(l.done.Close())
|
2018-02-08 23:37:47 +01:00
|
|
|
L:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-l.buffer:
|
2020-10-11 19:22:46 +08:00
|
|
|
c.Close()
|
2018-02-08 23:37:47 +01:00
|
|
|
default:
|
|
|
|
break L
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 23:38:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:32:40 +02:00
|
|
|
// Addr implements net.Listener.
|
2018-02-05 23:38:24 +01:00
|
|
|
func (l *OutboundListener) Addr() net.Addr {
|
|
|
|
return &net.TCPAddr{
|
|
|
|
IP: net.IP{0, 0, 0, 0},
|
|
|
|
Port: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 20:43:37 +02:00
|
|
|
// Outbound is a outbound.Handler that handles gRPC connections.
|
2018-04-04 21:32:40 +02:00
|
|
|
type Outbound struct {
|
2018-02-05 23:38:24 +01:00
|
|
|
tag string
|
|
|
|
listener *OutboundListener
|
2018-02-08 15:39:46 +01:00
|
|
|
access sync.RWMutex
|
|
|
|
closed bool
|
2018-02-05 23:38:24 +01:00
|
|
|
}
|
|
|
|
|
2018-10-11 20:43:37 +02:00
|
|
|
// Dispatch implements outbound.Handler.
|
2018-11-03 12:36:29 +01:00
|
|
|
func (co *Outbound) Dispatch(ctx context.Context, link *transport.Link) {
|
2018-02-08 15:39:46 +01:00
|
|
|
co.access.RLock()
|
|
|
|
|
|
|
|
if co.closed {
|
2018-12-31 21:25:10 +01:00
|
|
|
common.Interrupt(link.Reader)
|
|
|
|
common.Interrupt(link.Writer)
|
2018-02-08 15:39:46 +01:00
|
|
|
co.access.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-09 04:29:40 +02:00
|
|
|
closeSignal := done.New()
|
|
|
|
c := net.NewConnection(net.ConnectionInputMulti(link.Writer), net.ConnectionOutputMulti(link.Reader), net.ConnectionOnClose(closeSignal))
|
2018-02-05 23:38:24 +01:00
|
|
|
co.listener.add(c)
|
2018-02-08 15:39:46 +01:00
|
|
|
co.access.RUnlock()
|
2018-02-05 23:38:24 +01:00
|
|
|
<-closeSignal.Wait()
|
|
|
|
}
|
|
|
|
|
2018-10-11 20:43:37 +02:00
|
|
|
// Tag implements outbound.Handler.
|
2018-04-04 21:32:40 +02:00
|
|
|
func (co *Outbound) Tag() string {
|
2018-02-05 23:38:24 +01:00
|
|
|
return co.tag
|
|
|
|
}
|
|
|
|
|
2018-04-04 00:57:44 +02:00
|
|
|
// Start implements common.Runnable.
|
2018-04-04 21:32:40 +02:00
|
|
|
func (co *Outbound) Start() error {
|
2018-02-08 15:39:46 +01:00
|
|
|
co.access.Lock()
|
|
|
|
co.closed = false
|
|
|
|
co.access.Unlock()
|
2018-02-05 23:38:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 00:57:44 +02:00
|
|
|
// Close implements common.Closable.
|
2018-04-04 21:32:40 +02:00
|
|
|
func (co *Outbound) Close() error {
|
2018-02-08 15:39:46 +01:00
|
|
|
co.access.Lock()
|
2018-04-04 21:32:40 +02:00
|
|
|
defer co.access.Unlock()
|
2018-02-08 15:39:46 +01:00
|
|
|
|
2018-04-04 21:32:40 +02:00
|
|
|
co.closed = true
|
|
|
|
return co.listener.Close()
|
2018-02-08 15:39:46 +01:00
|
|
|
}
|