github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/redir/server/server.go (about) 1 package server 2 3 import ( 4 "io" 5 "net" 6 7 "github.com/Asutorufa/yuhaiin/pkg/log" 8 "github.com/Asutorufa/yuhaiin/pkg/net/netapi" 9 "github.com/Asutorufa/yuhaiin/pkg/protos/config/listener" 10 ) 11 12 type redir struct { 13 lis net.Listener 14 *netapi.ChannelServer 15 } 16 17 func (r *redir) Close() error { 18 r.ChannelServer.Close() 19 return r.lis.Close() 20 } 21 22 func (r *redir) AcceptPacket() (*netapi.Packet, error) { 23 return nil, io.EOF 24 } 25 26 func NewServer(o *listener.Inbound_Redir) func(netapi.Listener) (netapi.Accepter, error) { 27 return func(ii netapi.Listener) (netapi.Accepter, error) { 28 channel := netapi.NewChannelServer() 29 lis, err := ii.Stream(channel.Context()) 30 if err != nil { 31 channel.Close() 32 return nil, err 33 } 34 35 t := &redir{ 36 lis: lis, 37 ChannelServer: channel, 38 } 39 40 go func() { 41 for { 42 conn, err := lis.Accept() 43 if err != nil { 44 log.Error("redir accept failed", "err", err) 45 break 46 } 47 48 go func() { 49 if err := t.handle(conn); err != nil { 50 log.Error("redir handle failed", "err", err) 51 } 52 }() 53 } 54 }() 55 56 return t, nil 57 } 58 }