2015-10-04 14:22:52 -04:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2018-09-10 15:19:59 -04:00
|
|
|
"context"
|
2015-10-04 14:22:52 -04:00
|
|
|
"fmt"
|
2017-04-16 15:31:28 -04:00
|
|
|
"io"
|
2015-10-04 14:22:52 -04:00
|
|
|
|
2018-07-02 09:26:42 -04:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 08:32:54 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2018-07-02 09:26:42 -04:00
|
|
|
"v2ray.com/core/common/task"
|
2018-09-10 15:19:59 -04:00
|
|
|
"v2ray.com/core/transport/internet"
|
2018-07-02 09:26:42 -04:00
|
|
|
"v2ray.com/core/transport/pipe"
|
2015-10-04 14:22:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2017-08-29 08:32:54 -04:00
|
|
|
Port net.Port
|
2015-10-04 14:22:52 -04:00
|
|
|
MsgProcessor func(msg []byte) []byte
|
2018-04-18 17:57:28 -04:00
|
|
|
ShouldClose bool
|
2016-08-20 03:12:44 -04:00
|
|
|
SendFirst []byte
|
2017-08-29 08:32:54 -04:00
|
|
|
Listen net.Address
|
2018-09-15 15:35:32 -04:00
|
|
|
listener net.Listener
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
|
|
|
|
2017-08-29 08:32:54 -04:00
|
|
|
func (server *Server) Start() (net.Destination, error) {
|
2018-11-21 08:54:40 -05:00
|
|
|
return server.StartContext(context.Background(), nil)
|
2018-09-10 15:19:59 -04:00
|
|
|
}
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
func (server *Server) StartContext(ctx context.Context, sockopt *internet.SocketConfig) (net.Destination, error) {
|
2017-02-07 18:26:40 -05:00
|
|
|
listenerAddr := server.Listen
|
|
|
|
if listenerAddr == nil {
|
2017-08-29 08:32:54 -04:00
|
|
|
listenerAddr = net.LocalHostIP
|
2017-02-07 18:26:40 -05:00
|
|
|
}
|
2018-09-15 15:35:32 -04:00
|
|
|
listener, err := internet.ListenSystem(ctx, &net.TCPAddr{
|
2017-02-07 18:26:40 -05:00
|
|
|
IP: listenerAddr.IP(),
|
2015-10-04 14:22:52 -04:00
|
|
|
Port: int(server.Port),
|
2018-11-21 08:54:40 -05:00
|
|
|
}, sockopt)
|
2015-10-04 14:22:52 -04:00
|
|
|
if err != nil {
|
2017-08-29 08:32:54 -04:00
|
|
|
return net.Destination{}, err
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
2018-09-15 15:35:32 -04:00
|
|
|
|
2017-08-29 08:32:54 -04:00
|
|
|
localAddr := listener.Addr().(*net.TCPAddr)
|
2018-09-15 15:35:32 -04:00
|
|
|
server.Port = net.Port(localAddr.Port)
|
|
|
|
server.listener = listener
|
|
|
|
go server.acceptConnections(listener.(*net.TCPListener))
|
|
|
|
|
2017-08-29 08:32:54 -04:00
|
|
|
return net.TCPDestination(net.IPAddress(localAddr.IP), net.Port(localAddr.Port)), nil
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) acceptConnections(listener *net.TCPListener) {
|
2017-10-20 17:36:14 -04:00
|
|
|
for {
|
2015-10-04 14:22:52 -04:00
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2016-11-10 17:41:28 -05:00
|
|
|
fmt.Printf("Failed accept TCP connection: %v\n", err)
|
2017-10-20 17:36:14 -04:00
|
|
|
return
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
go server.handleConnection(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) handleConnection(conn net.Conn) {
|
2016-08-20 03:12:44 -04:00
|
|
|
if len(server.SendFirst) > 0 {
|
|
|
|
conn.Write(server.SendFirst)
|
|
|
|
}
|
2018-07-02 09:26:42 -04:00
|
|
|
|
|
|
|
pReader, pWriter := pipe.New(pipe.WithoutSizeLimit())
|
2018-12-06 05:35:02 -05:00
|
|
|
err := task.Run(context.Background(), func() error {
|
2020-10-11 07:22:46 -04:00
|
|
|
defer pWriter.Close()
|
2018-07-02 09:40:32 -04:00
|
|
|
|
2018-07-02 09:26:42 -04:00
|
|
|
for {
|
|
|
|
b := buf.New()
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFrom(conn); err != nil {
|
2018-07-02 09:26:42 -04:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(b.Bytes(), server.MsgProcessor(b.Bytes()))
|
2018-11-16 05:08:12 -05:00
|
|
|
if err := pWriter.WriteMultiBuffer(buf.MultiBuffer{b}); err != nil {
|
2018-07-02 09:26:42 -04:00
|
|
|
return err
|
2017-04-16 15:31:28 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-02 09:26:42 -04:00
|
|
|
}, func() error {
|
2018-12-31 15:25:10 -05:00
|
|
|
defer pReader.Interrupt()
|
2018-07-02 09:40:32 -04:00
|
|
|
|
2018-07-02 09:26:42 -04:00
|
|
|
w := buf.NewWriter(conn)
|
|
|
|
for {
|
|
|
|
mb, err := pReader.ReadMultiBuffer()
|
|
|
|
if err != nil {
|
2018-07-31 10:05:44 -04:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-02 09:26:42 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := w.WriteMultiBuffer(mb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-18 17:57:28 -04:00
|
|
|
}
|
2018-12-06 05:35:02 -05:00
|
|
|
})
|
2018-07-02 09:26:42 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("failed to transfer data: ", err.Error())
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
2018-07-02 09:26:42 -04:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
conn.Close()
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (server *Server) Close() error {
|
|
|
|
return server.listener.Close()
|
2015-12-15 10:00:47 -05:00
|
|
|
}
|