mirror of
https://github.com/mudler/LocalAI.git
synced 2026-09-14 07:07:33 -04:00
Introduce versioned resource manifests, a checksum-verified CAS preparer, offline test wrappers, and a guarded network transport. Replace live Hugging Face, GitHub, and OCI cases with deterministic fixtures and inject fixture metadata into importer discovery. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com>
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
// SPDX-License-Identifier: MIT
|
|
|
|
// Package testnetwork provides an explicit outbound-network guard for tests.
|
|
package testnetwork
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"strings"
|
|
)
|
|
|
|
type Guard struct {
|
|
Dialer net.Dialer
|
|
Dial func(context.Context, string, string) (net.Conn, error)
|
|
Allowed []netip.Prefix
|
|
}
|
|
|
|
func LocalGuard() *Guard {
|
|
prefixes := []string{"127.0.0.0/8", "::1/128", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"}
|
|
guard := &Guard{}
|
|
for _, prefix := range prefixes {
|
|
guard.Allowed = append(guard.Allowed, netip.MustParsePrefix(prefix))
|
|
}
|
|
return guard
|
|
}
|
|
|
|
func (g *Guard) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
originalAddress := address
|
|
host, _, err := net.SplitHostPort(address)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("test network guard: invalid address %q: %w", address, err)
|
|
}
|
|
addresses, err := net.DefaultResolver.LookupNetIP(ctx, "ip", strings.Trim(host, "[]"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("test network guard: resolve %q: %w", host, err)
|
|
}
|
|
for _, resolved := range addresses {
|
|
if !g.allowed(resolved.Unmap()) {
|
|
return nil, fmt.Errorf("test network guard: public dial blocked: %s (%s)", host, resolved)
|
|
}
|
|
}
|
|
if g.Dial != nil {
|
|
return g.Dial(ctx, network, originalAddress)
|
|
}
|
|
return g.Dialer.DialContext(ctx, network, originalAddress)
|
|
}
|
|
|
|
func (g *Guard) allowed(address netip.Addr) bool {
|
|
for _, prefix := range g.Allowed {
|
|
if prefix.Contains(address) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|