add interface xnet.SrcAddr/DstAddr
This commit is contained in:
+7
-10
@@ -3,7 +3,6 @@ package http2
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
@@ -11,13 +10,11 @@ import (
|
||||
|
||||
// a dummy HTTP2 server conn used by HTTP2 handler
|
||||
type conn struct {
|
||||
md mdata.Metadata
|
||||
r *http.Request
|
||||
w http.ResponseWriter
|
||||
laddr net.Addr
|
||||
raddr net.Addr
|
||||
clientAddr net.Addr
|
||||
closed chan struct{}
|
||||
md mdata.Metadata
|
||||
laddr net.Addr
|
||||
raddr net.Addr
|
||||
srcAddr net.Addr
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
@@ -45,8 +42,8 @@ func (c *conn) RemoteAddr() net.Addr {
|
||||
return c.raddr
|
||||
}
|
||||
|
||||
func (c *conn) ClientAddr() net.Addr {
|
||||
return c.clientAddr
|
||||
func (c *conn) SrcAddr() net.Addr {
|
||||
return c.srcAddr
|
||||
}
|
||||
|
||||
func (c *conn) SetDeadline(t time.Time) error {
|
||||
|
||||
@@ -14,7 +14,7 @@ type conn struct {
|
||||
w io.Writer
|
||||
remoteAddr net.Addr
|
||||
localAddr net.Addr
|
||||
clientAddr net.Addr
|
||||
srcAddr net.Addr
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
@@ -50,8 +50,8 @@ func (c *conn) RemoteAddr() net.Addr {
|
||||
return c.remoteAddr
|
||||
}
|
||||
|
||||
func (c *conn) ClientAddr() net.Addr {
|
||||
return c.clientAddr
|
||||
func (c *conn) SrcAddr() net.Addr {
|
||||
return c.srcAddr
|
||||
}
|
||||
|
||||
func (c *conn) SetDeadline(t time.Time) error {
|
||||
|
||||
@@ -157,7 +157,17 @@ func (l *h2Listener) Close() (err error) {
|
||||
}
|
||||
|
||||
func (l *h2Listener) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
if l.logger.IsLevelEnabled(logger.TraceLevel) {
|
||||
clientIP := xhttp.GetClientIP(r)
|
||||
cip := ""
|
||||
if clientIP != nil {
|
||||
cip = clientIP.String()
|
||||
}
|
||||
log := l.logger.WithFields(map[string]any{
|
||||
"local": l.addr.String(),
|
||||
"remote": r.RemoteAddr,
|
||||
"client": cip,
|
||||
})
|
||||
if log.IsLevelEnabled(logger.TraceLevel) {
|
||||
dump, _ := httputil.DumpRequest(r, false)
|
||||
l.logger.Trace(string(dump))
|
||||
}
|
||||
@@ -195,14 +205,13 @@ func (l *h2Listener) upgrade(w http.ResponseWriter, r *http.Request) (*conn, err
|
||||
remoteAddr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||
if remoteAddr == nil {
|
||||
remoteAddr = &net.TCPAddr{
|
||||
IP: net.IPv4zero,
|
||||
Port: 0,
|
||||
IP: net.IPv4zero,
|
||||
}
|
||||
}
|
||||
|
||||
var clientAddr net.Addr
|
||||
var srcAddr net.Addr
|
||||
if clientIP := xhttp.GetClientIP(r); clientIP != nil {
|
||||
clientAddr = &net.IPAddr{IP: clientIP}
|
||||
srcAddr = &net.TCPAddr{IP: clientIP}
|
||||
}
|
||||
|
||||
return &conn{
|
||||
@@ -210,7 +219,7 @@ func (l *h2Listener) upgrade(w http.ResponseWriter, r *http.Request) (*conn, err
|
||||
w: flushWriter{w},
|
||||
localAddr: l.addr,
|
||||
remoteAddr: remoteAddr,
|
||||
clientAddr: clientAddr,
|
||||
srcAddr: srcAddr,
|
||||
closed: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -139,21 +139,29 @@ func (l *http2Listener) Close() (err error) {
|
||||
}
|
||||
|
||||
func (l *http2Listener) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
raddr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||
remoteAddr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||
if remoteAddr == nil {
|
||||
remoteAddr = &net.TCPAddr{
|
||||
IP: net.IPv4zero,
|
||||
}
|
||||
}
|
||||
|
||||
var srcAddr net.Addr
|
||||
if clientIP := xhttp.GetClientIP(r); clientIP != nil {
|
||||
srcAddr = &net.TCPAddr{IP: clientIP}
|
||||
}
|
||||
|
||||
conn := &conn{
|
||||
laddr: l.addr,
|
||||
raddr: raddr,
|
||||
closed: make(chan struct{}),
|
||||
laddr: l.addr,
|
||||
raddr: remoteAddr,
|
||||
srcAddr: srcAddr,
|
||||
closed: make(chan struct{}),
|
||||
md: mdx.NewMetadata(map[string]any{
|
||||
"r": r,
|
||||
"w": w,
|
||||
}),
|
||||
}
|
||||
|
||||
if clientIP := xhttp.GetClientIP(r); clientIP != nil {
|
||||
conn.clientAddr = &net.IPAddr{IP: clientIP}
|
||||
}
|
||||
|
||||
select {
|
||||
case l.cqueue <- conn:
|
||||
default:
|
||||
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
|
||||
// a dummy HTTP3 server conn used by HTTP3 handler
|
||||
type conn struct {
|
||||
md mdata.Metadata
|
||||
laddr net.Addr
|
||||
raddr net.Addr
|
||||
clientAddr net.Addr
|
||||
closed chan struct{}
|
||||
md mdata.Metadata
|
||||
laddr net.Addr
|
||||
raddr net.Addr
|
||||
srcAddr net.Addr
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
@@ -42,8 +42,8 @@ func (c *conn) RemoteAddr() net.Addr {
|
||||
return c.raddr
|
||||
}
|
||||
|
||||
func (c *conn) ClientAddr() net.Addr {
|
||||
return c.clientAddr
|
||||
func (c *conn) SrcAddr() net.Addr {
|
||||
return c.srcAddr
|
||||
}
|
||||
|
||||
func (c *conn) SetDeadline(t time.Time) error {
|
||||
|
||||
@@ -125,10 +125,15 @@ func (l *http3Listener) Close() (err error) {
|
||||
}
|
||||
|
||||
func (l *http3Listener) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
raddr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||
remoteAddr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||
if remoteAddr == nil {
|
||||
remoteAddr = &net.TCPAddr{
|
||||
IP: net.IPv4zero,
|
||||
}
|
||||
}
|
||||
conn := &conn{
|
||||
laddr: l.addr,
|
||||
raddr: raddr,
|
||||
raddr: remoteAddr,
|
||||
closed: make(chan struct{}),
|
||||
md: mdx.NewMetadata(map[string]any{
|
||||
"r": r,
|
||||
@@ -137,7 +142,7 @@ func (l *http3Listener) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if clientIP := xhttp.GetClientIP(r); clientIP != nil {
|
||||
conn.clientAddr = &net.IPAddr{IP: clientIP}
|
||||
conn.srcAddr = &net.UDPAddr{IP: clientIP}
|
||||
}
|
||||
|
||||
select {
|
||||
|
||||
@@ -176,15 +176,15 @@ func (l *wtListener) upgrade(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var clientAddr net.Addr
|
||||
var srcAddr net.Addr
|
||||
if clientIP != nil {
|
||||
clientAddr = &net.IPAddr{IP: clientIP}
|
||||
srcAddr = &net.UDPAddr{IP: clientIP}
|
||||
}
|
||||
|
||||
l.mux(s, clientAddr, log)
|
||||
l.mux(s, srcAddr, log)
|
||||
}
|
||||
|
||||
func (l *wtListener) mux(s *wt.Session, clientAddr net.Addr, log logger.Logger) (err error) {
|
||||
func (l *wtListener) mux(s *wt.Session, srcAddr net.Addr, log logger.Logger) (err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
s.CloseWithError(1, err.Error())
|
||||
@@ -202,7 +202,7 @@ func (l *wtListener) mux(s *wt.Session, clientAddr net.Addr, log logger.Logger)
|
||||
}
|
||||
|
||||
select {
|
||||
case l.cqueue <- wt_util.ConnWithClientAddr(s, stream, clientAddr):
|
||||
case l.cqueue <- wt_util.ConnWithSrcAddr(s, stream, srcAddr):
|
||||
default:
|
||||
stream.Close()
|
||||
l.logger.Warnf("connection queue is full, stream %v discarded", stream.StreamID())
|
||||
|
||||
@@ -184,15 +184,15 @@ func (l *mwsListener) upgrade(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var clientAddr net.Addr
|
||||
var srcAddr net.Addr
|
||||
if clientIP != nil {
|
||||
clientAddr = &net.IPAddr{IP: clientIP}
|
||||
srcAddr = &net.TCPAddr{IP: clientIP}
|
||||
}
|
||||
|
||||
l.mux(ws_util.Conn(conn), clientAddr, log)
|
||||
l.mux(ws_util.Conn(conn), srcAddr, log)
|
||||
}
|
||||
|
||||
func (l *mwsListener) mux(conn net.Conn, clientAddr net.Addr, log logger.Logger) {
|
||||
func (l *mwsListener) mux(conn net.Conn, srcAddr net.Addr, log logger.Logger) {
|
||||
defer conn.Close()
|
||||
|
||||
session, err := mux.ServerSession(conn, l.md.muxCfg)
|
||||
@@ -210,7 +210,7 @@ func (l *mwsListener) mux(conn net.Conn, clientAddr net.Addr, log logger.Logger)
|
||||
}
|
||||
|
||||
select {
|
||||
case l.cqueue <- &connWithClientAddr{Conn: stream, clientAddr: clientAddr}:
|
||||
case l.cqueue <- &connWithSrcAddr{Conn: stream, srcAddr: srcAddr}:
|
||||
default:
|
||||
stream.Close()
|
||||
log.Warnf("connection queue is full, client %s discarded", stream.RemoteAddr())
|
||||
@@ -218,11 +218,11 @@ func (l *mwsListener) mux(conn net.Conn, clientAddr net.Addr, log logger.Logger)
|
||||
}
|
||||
}
|
||||
|
||||
type connWithClientAddr struct {
|
||||
type connWithSrcAddr struct {
|
||||
net.Conn
|
||||
clientAddr net.Addr
|
||||
srcAddr net.Addr
|
||||
}
|
||||
|
||||
func (c *connWithClientAddr) ClientAddr() net.Addr {
|
||||
return c.clientAddr
|
||||
func (c *connWithSrcAddr) SrcAddr() net.Addr {
|
||||
return c.srcAddr
|
||||
}
|
||||
|
||||
@@ -179,13 +179,13 @@ func (l *wsListener) upgrade(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var clientAddr net.Addr
|
||||
var srcAddr net.Addr
|
||||
if clientIP != nil {
|
||||
clientAddr = &net.IPAddr{IP: clientIP}
|
||||
srcAddr = &net.TCPAddr{IP: clientIP}
|
||||
}
|
||||
|
||||
select {
|
||||
case l.cqueue <- ws_util.ConnWithClientAddr(conn, clientAddr):
|
||||
case l.cqueue <- ws_util.ConnWithSrcAddr(conn, srcAddr):
|
||||
default:
|
||||
conn.Close()
|
||||
log.Warnf("connection queue is full, client %s discarded", conn.RemoteAddr())
|
||||
|
||||
Reference in New Issue
Block a user