2015-10-04 18:22:52 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-04-16 19:31:28 +00:00
|
|
|
"io"
|
2015-10-04 18:22:52 +00:00
|
|
|
|
2017-08-29 12:32:54 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2015-10-04 18:22:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2017-08-29 12:32:54 +00:00
|
|
|
Port net.Port
|
2015-10-04 18:22:52 +00:00
|
|
|
MsgProcessor func(msg []byte) []byte
|
2016-08-20 07:12:44 +00:00
|
|
|
SendFirst []byte
|
2017-08-29 12:32:54 +00:00
|
|
|
Listen net.Address
|
2016-02-03 11:18:52 +00:00
|
|
|
listener *net.TCPListener
|
2015-10-04 18:22:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:32:54 +00:00
|
|
|
func (server *Server) Start() (net.Destination, error) {
|
2017-02-07 23:26:40 +00:00
|
|
|
listenerAddr := server.Listen
|
|
|
|
if listenerAddr == nil {
|
2017-08-29 12:32:54 +00:00
|
|
|
listenerAddr = net.LocalHostIP
|
2017-02-07 23:26:40 +00:00
|
|
|
}
|
2015-10-04 18:22:52 +00:00
|
|
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
2017-02-07 23:26:40 +00:00
|
|
|
IP: listenerAddr.IP(),
|
2015-10-04 18:22:52 +00:00
|
|
|
Port: int(server.Port),
|
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-08-29 12:32:54 +00:00
|
|
|
return net.Destination{}, err
|
2015-10-04 18:22:52 +00:00
|
|
|
}
|
2017-08-29 12:32:54 +00:00
|
|
|
server.Port = net.Port(listener.Addr().(*net.TCPAddr).Port)
|
2016-02-02 15:03:56 +00:00
|
|
|
server.listener = listener
|
2015-10-04 18:22:52 +00:00
|
|
|
go server.acceptConnections(listener)
|
2017-08-29 12:32:54 +00:00
|
|
|
localAddr := listener.Addr().(*net.TCPAddr)
|
|
|
|
return net.TCPDestination(net.IPAddress(localAddr.IP), net.Port(localAddr.Port)), nil
|
2015-10-04 18:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) acceptConnections(listener *net.TCPListener) {
|
2017-10-20 21:36:14 +00:00
|
|
|
for {
|
2015-10-04 18:22:52 +00:00
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2016-11-10 22:41:28 +00:00
|
|
|
fmt.Printf("Failed accept TCP connection: %v\n", err)
|
2017-10-20 21:36:14 +00:00
|
|
|
return
|
2015-10-04 18:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go server.handleConnection(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) handleConnection(conn net.Conn) {
|
2016-08-20 07:12:44 +00:00
|
|
|
if len(server.SendFirst) > 0 {
|
|
|
|
conn.Write(server.SendFirst)
|
|
|
|
}
|
2016-07-17 10:18:34 +00:00
|
|
|
request := make([]byte, 4096)
|
2017-04-16 19:31:28 +00:00
|
|
|
for {
|
2016-07-17 10:18:34 +00:00
|
|
|
nBytes, err := conn.Read(request)
|
2015-12-15 15:00:47 +00:00
|
|
|
if err != nil {
|
2017-04-16 19:31:28 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
fmt.Println("Failed to read request:", err)
|
|
|
|
}
|
2015-12-15 15:00:47 +00:00
|
|
|
break
|
|
|
|
}
|
2016-07-17 10:18:34 +00:00
|
|
|
response := server.MsgProcessor(request[:nBytes])
|
2017-04-16 19:31:28 +00:00
|
|
|
if _, err := conn.Write(response); err != nil {
|
|
|
|
fmt.Println("Failed to write response:", err)
|
|
|
|
break
|
|
|
|
}
|
2015-10-04 18:22:52 +00:00
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}
|
2015-12-15 15:00:47 +00:00
|
|
|
|
2017-10-20 21:36:14 +00:00
|
|
|
func (server *Server) Close() {
|
|
|
|
server.listener.Close()
|
2015-12-15 15:00:47 +00:00
|
|
|
}
|