Files
x/internal/net/http/http.go
T
ginuerzh ef58909fb7 fix(dialer): correct inverted setMark on FreeBSD/OpenBSD
Fix setMark early-return conditions that were inverted relative to
Linux: on FreeBSD (SO_USER_COOKIE) and OpenBSD (SO_RTABLE), a
non-zero mark was short-circuited instead of applied, disabling
socket marking entirely.

Also fix GetClientIP to trim leading whitespace from X-Forwarded-For
entries (per RFC 7239), and fix Body.Read to subtract len(b) instead
of n from recordSize when a single read exceeds the remaining quota.

Add unit tests for internal/net/ (addr, dialer, http, ip, net, pipe,
transport, resolve, proxyproto, udp).
2026-05-21 20:58:48 +08:00

73 lines
1.1 KiB
Go

package http
import (
"bytes"
"io"
"net"
"net/http"
"strings"
)
func GetClientIP(req *http.Request) net.IP {
if req == nil {
return nil
}
// cloudflare CDN
sip := req.Header.Get("CF-Connecting-IP")
if sip == "" {
ss := strings.Split(req.Header.Get("X-Forwarded-For"), ",")
if len(ss) > 0 && ss[0] != "" {
sip = strings.TrimSpace(ss[0])
}
}
if sip == "" {
sip = req.Header.Get("X-Real-Ip")
}
return net.ParseIP(sip)
}
type Body struct {
r io.ReadCloser
buf bytes.Buffer
length int64
recordSize int
}
func NewBody(r io.ReadCloser, maxRecordSize int) *Body {
p := &Body{
r: r,
recordSize: maxRecordSize,
}
return p
}
func (p *Body) Read(b []byte) (n int, err error) {
n, err = p.r.Read(b)
p.length += int64(n)
if p.recordSize > 0 {
b = b[:n]
if n > p.recordSize {
b = b[:p.recordSize]
}
p.buf.Write(b)
p.recordSize -= len(b)
}
return
}
func (p *Body) Close() error {
return p.r.Close()
}
func (p *Body) Content() []byte {
return p.buf.Bytes()
}
func (p *Body) Length() int64 {
return p.length
}