1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-12 15:27:16 -05:00
v2fly/proxy/dokodemo/dokodemo.go

179 lines
4.1 KiB
Go
Raw Normal View History

2015-10-30 10:56:46 -04:00
package dokodemo
import (
"io"
"net"
"sync"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry"
)
type DokodemoDoor struct {
2016-01-03 18:33:25 -05:00
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
config Config
accepting bool
address v2net.Address
port v2net.Port
space app.Space
tcpListener *net.TCPListener
udpConn *net.UDPConn
2015-10-30 10:56:46 -04:00
}
func NewDokodemoDoor(space app.Space, config Config) *DokodemoDoor {
2015-12-06 12:21:15 -05:00
return &DokodemoDoor{
config: config,
space: space,
address: config.Address(),
2015-12-16 17:53:38 -05:00
port: config.Port(),
2015-10-30 10:56:46 -04:00
}
}
func (this *DokodemoDoor) Close() {
this.accepting = false
if this.tcpListener != nil {
this.tcpListener.Close()
2016-01-03 18:33:25 -05:00
this.tcpMutex.Lock()
this.tcpListener = nil
2016-01-03 18:33:25 -05:00
this.tcpMutex.Unlock()
}
if this.udpConn != nil {
this.udpConn.Close()
2016-01-03 18:33:25 -05:00
this.udpMutex.Lock()
this.udpConn = nil
2016-01-03 18:33:25 -05:00
this.udpMutex.Unlock()
}
}
2015-12-02 15:44:01 -05:00
func (this *DokodemoDoor) Listen(port v2net.Port) error {
2015-11-10 18:08:43 -05:00
this.accepting = true
2015-12-06 12:21:15 -05:00
if this.config.Network().HasNetwork(v2net.TCPNetwork) {
2015-10-30 10:56:46 -04:00
err := this.ListenTCP(port)
if err != nil {
return err
}
}
2015-12-06 12:21:15 -05:00
if this.config.Network().HasNetwork(v2net.UDPNetwork) {
2015-11-10 18:08:43 -05:00
err := this.ListenUDP(port)
if err != nil {
return err
}
}
2015-10-30 10:56:46 -04:00
return nil
}
2015-12-02 15:44:01 -05:00
func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
2015-11-10 18:08:43 -05:00
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: []byte{0, 0, 0, 0},
Port: int(port),
Zone: "",
})
if err != nil {
log.Error("Dokodemo failed to listen on port %d: %v", port, err)
return err
}
this.udpConn = udpConn
go this.handleUDPPackets()
2015-11-10 18:08:43 -05:00
return nil
}
func (this *DokodemoDoor) handleUDPPackets() {
2015-11-10 18:08:43 -05:00
for this.accepting {
buffer := alloc.NewBuffer()
2016-01-03 18:33:25 -05:00
this.udpMutex.RLock()
if !this.accepting {
2016-01-03 19:19:27 -05:00
this.udpMutex.RUnlock()
2016-01-03 18:33:25 -05:00
return
}
2016-01-03 18:33:25 -05:00
nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
this.udpMutex.RUnlock()
2015-11-10 18:08:43 -05:00
buffer.Slice(0, nBytes)
if err != nil {
buffer.Release()
log.Error("Dokodemo failed to read from UDP: %v", err)
return
}
2015-12-16 17:53:38 -05:00
packet := v2net.NewPacket(v2net.UDPDestination(this.address, this.port), buffer, false)
2015-12-05 16:55:45 -05:00
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
2015-11-10 18:08:43 -05:00
close(ray.InboundInput())
for payload := range ray.InboundOutput() {
2016-01-03 18:33:25 -05:00
this.udpMutex.RLock()
if !this.accepting {
this.udpMutex.RUnlock()
return
}
this.udpConn.WriteToUDP(payload.Value, addr)
this.udpMutex.RUnlock()
2015-11-10 18:08:43 -05:00
}
}
}
2015-12-02 15:44:01 -05:00
func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
2015-10-30 10:56:46 -04:00
tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: int(port),
Zone: "",
})
if err != nil {
log.Error("Dokodemo failed to listen on port %d: %v", port, err)
return err
}
this.tcpListener = tcpListener
go this.AcceptTCPConnections()
2015-10-30 10:56:46 -04:00
return nil
}
func (this *DokodemoDoor) AcceptTCPConnections() {
2015-10-30 10:56:46 -04:00
for this.accepting {
retry.Timed(100, 100).On(func() error {
2016-01-03 19:19:27 -05:00
this.tcpMutex.RLock()
defer this.tcpMutex.RUnlock()
2016-01-03 18:33:25 -05:00
if !this.accepting {
return nil
}
2016-01-03 19:19:27 -05:00
connection, err := this.tcpListener.AcceptTCP()
if err != nil {
log.Error("Dokodemo failed to accept new connections: %v", err)
return err
2015-10-30 10:56:46 -04:00
}
2016-01-03 19:19:27 -05:00
go this.HandleTCPConnection(connection)
2015-10-30 10:56:46 -04:00
return nil
})
}
}
func (this *DokodemoDoor) HandleTCPConnection(conn *net.TCPConn) {
defer conn.Close()
2015-12-16 17:53:38 -05:00
packet := v2net.NewPacket(v2net.TCPDestination(this.address, this.port), nil, true)
2015-12-05 16:55:45 -05:00
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
2015-10-30 10:56:46 -04:00
var inputFinish, outputFinish sync.Mutex
inputFinish.Lock()
outputFinish.Lock()
2015-12-06 12:21:15 -05:00
reader := v2net.NewTimeOutReader(this.config.Timeout(), conn)
2015-10-30 10:56:46 -04:00
go dumpInput(reader, ray.InboundInput(), &inputFinish)
go dumpOutput(conn, ray.InboundOutput(), &outputFinish)
outputFinish.Lock()
}
func dumpInput(reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
v2net.ReaderToChan(input, reader)
finish.Unlock()
close(input)
}
func dumpOutput(writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) {
v2net.ChanToWriter(writer, output)
finish.Unlock()
}