add relay proxy

This commit is contained in:
ginuerzh
2021-11-18 22:54:23 +08:00
parent 1a1ee384b7
commit 8185d1124f
31 changed files with 747 additions and 106 deletions

View File

@ -64,13 +64,7 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
// standard UDP relay.
if pc, ok := conn.(net.PacketConn); ok {
if h.md.enableUDP {
h.handleUDP(ctx, conn.RemoteAddr(), pc)
return
} else {
h.logger.Error("UDP relay is diabled")
}
h.handleUDP(ctx, pc, conn.RemoteAddr())
return
}
@ -84,21 +78,17 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
br := bufio.NewReader(conn)
data, err := br.Peek(3)
conn.SetReadDeadline(time.Time{})
if err != nil {
h.logger.Error(err)
h.discard(conn)
return
}
conn.SetReadDeadline(time.Time{})
conn = handler.NewBufferReaderConn(conn, br)
if data[2] == 0xff {
if h.md.enableUDP {
// UDP-over-TCP relay
h.handleUDPTun(ctx, conn)
} else {
h.logger.Error("UDP relay is diabled")
}
// UDP-over-TCP relay
h.handleUDPTun(ctx, conn)
return
}
@ -110,8 +100,6 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
return
}
conn.SetReadDeadline(time.Time{})
h.logger = h.logger.WithFields(map[string]interface{}{
"dst": addr.String(),
})

View File

@ -11,7 +11,12 @@ import (
"github.com/go-gost/gost/pkg/common/util/ss"
)
func (h *ssHandler) handleUDP(ctx context.Context, raddr net.Addr, conn net.PacketConn) {
func (h *ssHandler) handleUDP(ctx context.Context, conn net.PacketConn, raddr net.Addr) {
if !h.md.enableUDP {
h.logger.Error("UDP relay is diabled")
return
}
if h.md.cipher != nil {
conn = h.md.cipher.PacketConn(conn)
}
@ -50,6 +55,11 @@ func (h *ssHandler) handleUDP(ctx context.Context, raddr net.Addr, conn net.Pack
}
func (h *ssHandler) handleUDPTun(ctx context.Context, conn net.Conn) {
if !h.md.enableUDP {
h.logger.Error("UDP relay is diabled")
return
}
// obtain a udp connection
r := (&chain.Router{}).
WithChain(h.chain).