add sshd listener
This commit is contained in:
@ -11,27 +11,28 @@ import (
|
||||
"github.com/go-gost/gosocks5"
|
||||
"github.com/go-gost/gost/pkg/common/util/socks"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn) {
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, log logger.Logger) {
|
||||
log = log.WithFields(map[string]interface{}{
|
||||
"cmd": "udp",
|
||||
})
|
||||
|
||||
if !h.md.enableUDP {
|
||||
reply := gosocks5.NewReply(gosocks5.NotAllowed, nil)
|
||||
reply.Write(conn)
|
||||
h.logger.Debug(reply)
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
log.Debug(reply)
|
||||
log.Error("UDP relay is diabled")
|
||||
return
|
||||
}
|
||||
|
||||
cc, err := net.ListenUDP("udp", nil)
|
||||
if err != nil {
|
||||
h.logger.Error(err)
|
||||
log.Error(err)
|
||||
reply := gosocks5.NewReply(gosocks5.Failure, nil)
|
||||
reply.Write(conn)
|
||||
h.logger.Debug(reply)
|
||||
log.Debug(reply)
|
||||
return
|
||||
}
|
||||
defer cc.Close()
|
||||
@ -42,41 +43,40 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn) {
|
||||
saddr.Host, _, _ = net.SplitHostPort(conn.LocalAddr().String()) // replace the IP to the out-going interface's
|
||||
reply := gosocks5.NewReply(gosocks5.Succeeded, &saddr)
|
||||
if err := reply.Write(conn); err != nil {
|
||||
h.logger.Error(err)
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
h.logger.Debug(reply)
|
||||
log.Debug(reply)
|
||||
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
log = log.WithFields(map[string]interface{}{
|
||||
"bind": fmt.Sprintf("%s/%s", cc.LocalAddr(), cc.LocalAddr().Network()),
|
||||
})
|
||||
h.logger.Debugf("bind on %s OK", cc.LocalAddr())
|
||||
log.Debugf("bind on %s OK", cc.LocalAddr())
|
||||
|
||||
// obtain a udp connection
|
||||
c, err := h.router.Dial(ctx, "udp", "") // UDP association
|
||||
if err != nil {
|
||||
h.logger.Error(err)
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
pc, ok := c.(net.PacketConn)
|
||||
if !ok {
|
||||
h.logger.Errorf("wrong connection type")
|
||||
log.Errorf("wrong connection type")
|
||||
return
|
||||
}
|
||||
|
||||
relay := handler.NewUDPRelay(socks.UDPConn(cc, h.md.udpBufferSize), pc).
|
||||
WithBypass(h.options.Bypass).
|
||||
WithLogger(h.logger)
|
||||
WithLogger(log)
|
||||
relay.SetBufferSize(h.md.udpBufferSize)
|
||||
|
||||
go relay.Run()
|
||||
|
||||
t := time.Now()
|
||||
h.logger.Infof("%s <-> %s", conn.RemoteAddr(), cc.LocalAddr())
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), cc.LocalAddr())
|
||||
io.Copy(ioutil.Discard, conn)
|
||||
h.logger.
|
||||
WithFields(map[string]interface{}{"duration": time.Since(t)}).
|
||||
log.WithFields(map[string]interface{}{"duration": time.Since(t)}).
|
||||
Infof("%s >-< %s", conn.RemoteAddr(), cc.LocalAddr())
|
||||
}
|
||||
|
Reference in New Issue
Block a user