chore: code cleanup
This commit is contained in:
@ -100,7 +100,7 @@ func HandleConn(c net.Conn, tunnel C.Tunnel, cache *cache.LruCache[string, bool]
|
||||
|
||||
func authenticate(request *http.Request, cache *cache.LruCache[string, bool]) *http.Response {
|
||||
authenticator := authStore.Authenticator()
|
||||
if inbound.SkipAuthRemoteAddr(N.NewCustomAddr("", request.RemoteAddr, nil)) {
|
||||
if inbound.SkipAuthRemoteAddress(request.RemoteAddr) {
|
||||
authenticator = nil
|
||||
}
|
||||
if authenticator != nil {
|
||||
|
@ -17,35 +17,15 @@ func WithAdditions(ctx context.Context, additions ...inbound.Addition) context.C
|
||||
return context.WithValue(ctx, ctxKeyAdditions, additions)
|
||||
}
|
||||
|
||||
func getAdditions(ctx context.Context) []inbound.Addition {
|
||||
func getAdditions(ctx context.Context) (additions []inbound.Addition) {
|
||||
if v := ctx.Value(ctxKeyAdditions); v != nil {
|
||||
if a, ok := v.([]inbound.Addition); ok {
|
||||
return a
|
||||
additions = a
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func combineAdditions(ctx context.Context, additions []inbound.Addition, extraAdditions ...inbound.Addition) []inbound.Addition {
|
||||
additionsCloned := false
|
||||
if ctxAdditions := getAdditions(ctx); len(ctxAdditions) > 0 {
|
||||
additions = slices.Clone(additions)
|
||||
additionsCloned = true
|
||||
additions = append(additions, ctxAdditions...)
|
||||
}
|
||||
if user, ok := auth.UserFromContext[string](ctx); ok {
|
||||
if !additionsCloned {
|
||||
additions = slices.Clone(additions)
|
||||
additionsCloned = true
|
||||
}
|
||||
additions = slices.Clone(additions)
|
||||
additions = append(additions, inbound.WithInUser(user))
|
||||
}
|
||||
if len(extraAdditions) > 0 {
|
||||
if !additionsCloned {
|
||||
additions = slices.Clone(additions)
|
||||
additionsCloned = true
|
||||
}
|
||||
additions = append(additions, extraAdditions...)
|
||||
}
|
||||
return additions
|
||||
return
|
||||
}
|
||||
|
@ -92,12 +92,10 @@ func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, meta
|
||||
cMetadata := &C.Metadata{
|
||||
NetWork: C.TCP,
|
||||
Type: h.Type,
|
||||
Host: metadata.Destination.Fqdn,
|
||||
}
|
||||
additions := combineAdditions(ctx, h.Additions, inbound.WithDstAddr(metadata.Destination), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
|
||||
for _, addition := range additions {
|
||||
addition.Apply(cMetadata)
|
||||
}
|
||||
inbound.ApplyAdditions(cMetadata, inbound.WithDstAddr(metadata.Destination), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
|
||||
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
|
||||
inbound.ApplyAdditions(cMetadata, h.Additions...)
|
||||
|
||||
h.Tunnel.HandleTCPConn(conn, cMetadata) // this goroutine must exit after conn unused
|
||||
return nil
|
||||
@ -155,12 +153,10 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
||||
cMetadata := &C.Metadata{
|
||||
NetWork: C.UDP,
|
||||
Type: h.Type,
|
||||
Host: dest.Fqdn,
|
||||
}
|
||||
additions := combineAdditions(ctx, h.Additions, inbound.WithDstAddr(dest), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
|
||||
for _, addition := range additions {
|
||||
addition.Apply(cMetadata)
|
||||
}
|
||||
inbound.ApplyAdditions(cMetadata, inbound.WithDstAddr(dest), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
|
||||
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
|
||||
inbound.ApplyAdditions(cMetadata, h.Additions...)
|
||||
|
||||
h.Tunnel.HandleUDPPacket(cPacket, cMetadata)
|
||||
}
|
||||
|
@ -36,9 +36,7 @@ func (l *Listener) Close() error {
|
||||
|
||||
func (l *Listener) handleTCP(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) {
|
||||
N.TCPKeepAlive(conn)
|
||||
conn, metadata := inbound.NewSocket(l.target, conn, C.TUNNEL, additions...)
|
||||
metadata.SpecialProxy = l.proxy
|
||||
tunnel.HandleTCPConn(conn, metadata)
|
||||
tunnel.HandleTCPConn(inbound.NewSocket(l.target, conn, C.TUNNEL, additions...))
|
||||
}
|
||||
|
||||
func New(addr, target, proxy string, tunnel C.Tunnel, additions ...inbound.Addition) (*Listener, error) {
|
||||
@ -59,6 +57,10 @@ func New(addr, target, proxy string, tunnel C.Tunnel, additions ...inbound.Addit
|
||||
addr: addr,
|
||||
}
|
||||
|
||||
if proxy != "" {
|
||||
additions = append([]inbound.Addition{inbound.WithSpecialProxy(proxy)}, additions...)
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
|
@ -51,6 +51,11 @@ func NewUDP(addr, target, proxy string, tunnel C.Tunnel, additions ...inbound.Ad
|
||||
proxy: proxy,
|
||||
addr: addr,
|
||||
}
|
||||
|
||||
if proxy != "" {
|
||||
additions = append([]inbound.Addition{inbound.WithSpecialProxy(proxy)}, additions...)
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
buf := pool.Get(pool.UDPBufferSize)
|
||||
@ -76,7 +81,5 @@ func (l *PacketConn) handleUDP(pc net.PacketConn, tunnel C.Tunnel, buf []byte, a
|
||||
payload: buf,
|
||||
}
|
||||
|
||||
packet, metadata := inbound.NewPacket(l.target, cPacket, C.TUNNEL, additions...)
|
||||
metadata.SpecialProxy = l.proxy
|
||||
tunnel.HandleUDPPacket(packet, metadata)
|
||||
tunnel.HandleUDPPacket(inbound.NewPacket(l.target, cPacket, C.TUNNEL, additions...))
|
||||
}
|
||||
|
Reference in New Issue
Block a user