2021-06-18 16:48:36 +08:00
|
|
|
package streamserver
|
2021-06-02 23:54:22 +08:00
|
|
|
|
|
|
|
import (
|
2021-06-21 19:27:26 +08:00
|
|
|
"bufio"
|
2021-06-02 23:54:22 +08:00
|
|
|
"fmt"
|
2021-06-21 19:27:26 +08:00
|
|
|
"io"
|
2021-06-02 23:54:22 +08:00
|
|
|
"net"
|
2022-02-09 15:19:35 +08:00
|
|
|
"strconv"
|
2021-06-02 23:54:22 +08:00
|
|
|
|
|
|
|
libnet "github.com/fatedier/frp/pkg/util/net"
|
2021-06-20 23:57:41 +08:00
|
|
|
"github.com/fatedier/frp/test/e2e/pkg/rpc"
|
2021-06-02 23:54:22 +08:00
|
|
|
)
|
|
|
|
|
2021-06-18 16:48:36 +08:00
|
|
|
type Type string
|
2021-06-02 23:54:22 +08:00
|
|
|
|
|
|
|
const (
|
2021-06-18 16:48:36 +08:00
|
|
|
TCP Type = "tcp"
|
|
|
|
UDP Type = "udp"
|
|
|
|
Unix Type = "unix"
|
2021-06-02 23:54:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-06-18 16:48:36 +08:00
|
|
|
netType Type
|
2021-06-02 23:54:22 +08:00
|
|
|
bindAddr string
|
|
|
|
bindPort int
|
|
|
|
respContent []byte
|
|
|
|
|
2021-06-21 19:27:26 +08:00
|
|
|
handler func(net.Conn)
|
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Server) *Server
|
|
|
|
|
2021-06-18 16:48:36 +08:00
|
|
|
func New(netType Type, options ...Option) *Server {
|
2021-06-02 23:54:22 +08:00
|
|
|
s := &Server{
|
|
|
|
netType: netType,
|
|
|
|
bindAddr: "127.0.0.1",
|
|
|
|
}
|
2021-06-21 19:27:26 +08:00
|
|
|
s.handler = s.handle
|
2021-06-02 23:54:22 +08:00
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
s = option(s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBindAddr(addr string) Option {
|
|
|
|
return func(s *Server) *Server {
|
|
|
|
s.bindAddr = addr
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBindPort(port int) Option {
|
|
|
|
return func(s *Server) *Server {
|
|
|
|
s.bindPort = port
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithRespContent(content []byte) Option {
|
|
|
|
return func(s *Server) *Server {
|
|
|
|
s.respContent = content
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 19:27:26 +08:00
|
|
|
func WithCustomHandler(handler func(net.Conn)) Option {
|
|
|
|
return func(s *Server) *Server {
|
|
|
|
s.handler = handler
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
func (s *Server) Run() error {
|
|
|
|
if err := s.initListener(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
c, err := s.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-21 19:27:26 +08:00
|
|
|
go s.handler(c)
|
2021-06-02 23:54:22 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
if s.l != nil {
|
|
|
|
return s.l.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initListener() (err error) {
|
|
|
|
switch s.netType {
|
|
|
|
case TCP:
|
2022-02-09 15:19:35 +08:00
|
|
|
s.l, err = net.Listen("tcp", net.JoinHostPort(s.bindAddr, strconv.Itoa(s.bindPort)))
|
2021-06-02 23:54:22 +08:00
|
|
|
case UDP:
|
|
|
|
s.l, err = libnet.ListenUDP(s.bindAddr, s.bindPort)
|
|
|
|
case Unix:
|
|
|
|
s.l, err = net.Listen("unix", s.bindAddr)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown server type: %s", s.netType)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handle(c net.Conn) {
|
|
|
|
defer c.Close()
|
|
|
|
|
2021-06-21 19:27:26 +08:00
|
|
|
var reader io.Reader = c
|
|
|
|
if s.netType == UDP {
|
|
|
|
reader = bufio.NewReader(c)
|
|
|
|
}
|
2021-06-02 23:54:22 +08:00
|
|
|
for {
|
2021-06-21 19:27:26 +08:00
|
|
|
buf, err := rpc.ReadBytes(reader)
|
2021-06-02 23:54:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:57:41 +08:00
|
|
|
if len(s.respContent) > 0 {
|
|
|
|
buf = s.respContent
|
2021-06-02 23:54:22 +08:00
|
|
|
}
|
2022-08-29 01:02:53 +08:00
|
|
|
_, _ = rpc.WriteBytes(c, buf)
|
2021-06-02 23:54:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) BindAddr() string {
|
|
|
|
return s.bindAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) BindPort() int {
|
|
|
|
return s.bindPort
|
|
|
|
}
|