package http import ( "context" "net" "net/http" "net/http/httputil" "time" "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 *httpHandler) handleUDP(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) { log = log.WithFields(map[string]interface{}{ "cmd": "udp", }) resp := &http.Response{ ProtoMajor: 1, ProtoMinor: 1, Header: h.md.header, } if resp.Header == nil { resp.Header = http.Header{} } if !h.md.enableUDP { resp.StatusCode = http.StatusForbidden resp.Write(conn) if log.IsLevelEnabled(logger.DebugLevel) { dump, _ := httputil.DumpResponse(resp, false) log.Debug(string(dump)) } log.Error("UDP relay is diabled") return } resp.StatusCode = http.StatusOK if log.IsLevelEnabled(logger.DebugLevel) { dump, _ := httputil.DumpResponse(resp, false) log.Debug(string(dump)) } if err := resp.Write(conn); err != nil { log.Error(err) return } // obtain a udp connection c, err := h.router.Dial(ctx, "udp", "") // UDP association if err != nil { log.Error(err) return } defer c.Close() pc, ok := c.(net.PacketConn) if !ok { log.Errorf("wrong connection type") return } relay := handler.NewUDPRelay(socks.UDPTunServerConn(conn), pc). WithBypass(h.options.Bypass). WithLogger(log) t := time.Now() log.Infof("%s <-> %s", conn.RemoteAddr(), pc.LocalAddr()) relay.Run() log.WithFields(map[string]interface{}{ "duration": time.Since(t), }).Infof("%s >-< %s", conn.RemoteAddr(), pc.LocalAddr()) }