add composer
This commit is contained in:
@@ -90,7 +90,12 @@ func applyHubDefaults(h *HubConfig) {
|
||||
h.CacheTTL = Duration(0)
|
||||
}
|
||||
if trimmed := strings.TrimSpace(h.Module); trimmed == "" {
|
||||
h.Module = hubmodule.DefaultModuleKey()
|
||||
typeKey := strings.ToLower(strings.TrimSpace(h.Type))
|
||||
if meta, ok := hubmodule.Resolve(typeKey); ok {
|
||||
h.Module = meta.Key
|
||||
} else {
|
||||
h.Module = hubmodule.DefaultModuleKey()
|
||||
}
|
||||
} else {
|
||||
h.Module = strings.ToLower(trimmed)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
_ "github.com/any-hub/any-hub/internal/hubmodule/composer"
|
||||
_ "github.com/any-hub/any-hub/internal/hubmodule/docker"
|
||||
_ "github.com/any-hub/any-hub/internal/hubmodule/legacy"
|
||||
_ "github.com/any-hub/any-hub/internal/hubmodule/npm"
|
||||
|
||||
@@ -11,13 +11,14 @@ import (
|
||||
)
|
||||
|
||||
var supportedHubTypes = map[string]struct{}{
|
||||
"docker": {},
|
||||
"npm": {},
|
||||
"go": {},
|
||||
"pypi": {},
|
||||
"docker": {},
|
||||
"npm": {},
|
||||
"go": {},
|
||||
"pypi": {},
|
||||
"composer": {},
|
||||
}
|
||||
|
||||
const supportedHubTypeList = "docker|npm|go|pypi"
|
||||
const supportedHubTypeList = "docker|npm|go|pypi|composer"
|
||||
|
||||
// Validate 针对语义级别做进一步校验,防止非法配置启动服务。
|
||||
func (c *Config) Validate() error {
|
||||
|
||||
28
internal/hubmodule/composer/module.go
Normal file
28
internal/hubmodule/composer/module.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Package composer declares metadata for Composer (PHP) package proxying.
|
||||
package composer
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/any-hub/any-hub/internal/hubmodule"
|
||||
)
|
||||
|
||||
const composerDefaultTTL = 6 * time.Hour
|
||||
|
||||
func init() {
|
||||
hubmodule.MustRegister(hubmodule.ModuleMetadata{
|
||||
Key: "composer",
|
||||
Description: "Composer packages proxy with metadata+dist caching",
|
||||
MigrationState: hubmodule.MigrationStateBeta,
|
||||
SupportedProtocols: []string{
|
||||
"composer",
|
||||
},
|
||||
CacheStrategy: hubmodule.CacheStrategyProfile{
|
||||
TTLHint: composerDefaultTTL,
|
||||
ValidationMode: hubmodule.ValidationModeETag,
|
||||
DiskLayout: "raw_path",
|
||||
RequiresMetadataFile: false,
|
||||
SupportsStreamingWrite: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
183
internal/proxy/composer_rewrite.go
Normal file
183
internal/proxy/composer_rewrite.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/any-hub/any-hub/internal/server"
|
||||
)
|
||||
|
||||
func (h *Handler) rewriteComposerResponse(route *server.HubRoute, resp *http.Response, path string) (*http.Response, error) {
|
||||
if resp == nil || route == nil || route.Config.Type != "composer" {
|
||||
return resp, nil
|
||||
}
|
||||
if !isComposerMetadataPath(path) {
|
||||
return resp, nil
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
rewritten, changed, err := rewriteComposerMetadata(body, route.Config.Domain)
|
||||
if err != nil {
|
||||
resp.Body = io.NopCloser(bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
if !changed {
|
||||
resp.Body = io.NopCloser(bytes.NewReader(body))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
resp.Body = io.NopCloser(bytes.NewReader(rewritten))
|
||||
resp.ContentLength = int64(len(rewritten))
|
||||
resp.Header.Set("Content-Length", strconv.Itoa(len(rewritten)))
|
||||
resp.Header.Set("Content-Type", "application/json")
|
||||
resp.Header.Del("Content-Encoding")
|
||||
resp.Header.Del("Etag")
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func rewriteComposerMetadata(body []byte, domain string) ([]byte, bool, error) {
|
||||
type packagesRoot struct {
|
||||
Packages map[string]json.RawMessage `json:"packages"`
|
||||
}
|
||||
var root packagesRoot
|
||||
if err := json.Unmarshal(body, &root); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if len(root.Packages) == 0 {
|
||||
return body, false, nil
|
||||
}
|
||||
|
||||
changed := false
|
||||
for name, raw := range root.Packages {
|
||||
updated, rewritten, err := rewriteComposerPackagesPayload(raw, domain)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if rewritten {
|
||||
root.Packages[name] = updated
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return body, false, nil
|
||||
}
|
||||
data, err := json.Marshal(root)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return data, true, nil
|
||||
}
|
||||
|
||||
func rewriteComposerPackagesPayload(raw json.RawMessage, domain string) (json.RawMessage, bool, error) {
|
||||
var asArray []map[string]any
|
||||
if err := json.Unmarshal(raw, &asArray); err == nil {
|
||||
rewrote := rewriteComposerVersionSlice(asArray, domain)
|
||||
if !rewrote {
|
||||
return raw, false, nil
|
||||
}
|
||||
data, err := json.Marshal(asArray)
|
||||
return data, true, err
|
||||
}
|
||||
|
||||
var asMap map[string]map[string]any
|
||||
if err := json.Unmarshal(raw, &asMap); err == nil {
|
||||
rewrote := rewriteComposerVersionMap(asMap, domain)
|
||||
if !rewrote {
|
||||
return raw, false, nil
|
||||
}
|
||||
data, err := json.Marshal(asMap)
|
||||
return data, true, err
|
||||
}
|
||||
|
||||
return raw, false, nil
|
||||
}
|
||||
|
||||
func rewriteComposerVersionSlice(items []map[string]any, domain string) bool {
|
||||
changed := false
|
||||
for _, entry := range items {
|
||||
if rewriteComposerVersion(entry, domain) {
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
func rewriteComposerVersionMap(items map[string]map[string]any, domain string) bool {
|
||||
changed := false
|
||||
for _, entry := range items {
|
||||
if rewriteComposerVersion(entry, domain) {
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
func rewriteComposerVersion(entry map[string]any, domain string) bool {
|
||||
if entry == nil {
|
||||
return false
|
||||
}
|
||||
distVal, ok := entry["dist"].(map[string]any)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
urlValue, ok := distVal["url"].(string)
|
||||
if !ok || urlValue == "" {
|
||||
return false
|
||||
}
|
||||
rewritten := rewriteComposerDistURL(domain, urlValue)
|
||||
if rewritten == urlValue {
|
||||
return false
|
||||
}
|
||||
distVal["url"] = rewritten
|
||||
return true
|
||||
}
|
||||
|
||||
func rewriteComposerDistURL(domain, original string) string {
|
||||
parsed, err := url.Parse(original)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
return original
|
||||
}
|
||||
prefix := fmt.Sprintf("/dist/%s/%s", parsed.Scheme, parsed.Host)
|
||||
newURL := url.URL{
|
||||
Scheme: "https",
|
||||
Host: domain,
|
||||
Path: prefix + parsed.Path,
|
||||
RawQuery: parsed.RawQuery,
|
||||
Fragment: parsed.Fragment,
|
||||
}
|
||||
if raw := parsed.RawPath; raw != "" {
|
||||
newURL.RawPath = prefix + raw
|
||||
}
|
||||
return newURL.String()
|
||||
}
|
||||
|
||||
func isComposerMetadataPath(path string) bool {
|
||||
switch {
|
||||
case path == "/packages.json":
|
||||
return true
|
||||
case strings.HasPrefix(path, "/p2/"):
|
||||
return true
|
||||
case strings.HasPrefix(path, "/p/"):
|
||||
return true
|
||||
case strings.HasPrefix(path, "/provider-"):
|
||||
return true
|
||||
case strings.HasPrefix(path, "/providers/"):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isComposerDistPath(path string) bool {
|
||||
return strings.HasPrefix(path, "/dist/")
|
||||
}
|
||||
@@ -200,6 +200,15 @@ func (h *Handler) fetchAndStream(
|
||||
"hub": route.Config.Name,
|
||||
}).Warn("pypi_rewrite_failed")
|
||||
}
|
||||
} else if route.Config.Type == "composer" {
|
||||
if rewritten, rewriteErr := h.rewriteComposerResponse(route, resp, requestPath(c)); rewriteErr == nil {
|
||||
resp = rewritten
|
||||
} else {
|
||||
h.logger.WithError(rewriteErr).WithFields(logrus.Fields{
|
||||
"action": "composer_rewrite",
|
||||
"hub": route.Config.Name,
|
||||
}).Warn("composer_rewrite_failed")
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
@@ -446,6 +455,8 @@ func inferCachedContentType(route *server.HubRoute, locator cache.Locator) strin
|
||||
switch {
|
||||
case strings.HasSuffix(clean, ".zip"):
|
||||
return "application/zip"
|
||||
case strings.HasSuffix(clean, ".json"):
|
||||
return "application/json"
|
||||
case strings.HasSuffix(clean, ".mod"):
|
||||
return "text/plain"
|
||||
case strings.HasSuffix(clean, ".info"):
|
||||
@@ -624,6 +635,11 @@ func resolveUpstreamURL(route *server.HubRoute, base *url.URL, c fiber.Ctx) *url
|
||||
return filesBase.ResolveReference(relative)
|
||||
}
|
||||
}
|
||||
if route != nil && route.Config.Type == "composer" && strings.HasPrefix(clean, "/dist/") {
|
||||
if distTarget, ok := parseComposerDistURL(clean, string(uri.QueryString())); ok {
|
||||
return distTarget
|
||||
}
|
||||
}
|
||||
relative := &url.URL{Path: clean, RawPath: clean}
|
||||
if query := string(uri.QueryString()); query != "" {
|
||||
relative.RawQuery = query
|
||||
@@ -701,6 +717,15 @@ func determineCachePolicy(route *server.HubRoute, locator cache.Locator, method
|
||||
}
|
||||
policy.requireRevalidate = true
|
||||
return policy
|
||||
case "composer":
|
||||
if isComposerDistPath(path) {
|
||||
return policy
|
||||
}
|
||||
if isComposerMetadataPath(path) {
|
||||
policy.requireRevalidate = true
|
||||
return policy
|
||||
}
|
||||
return cachePolicy{}
|
||||
default:
|
||||
return policy
|
||||
}
|
||||
@@ -899,6 +924,38 @@ func applyPyPISimpleFallback(route *server.HubRoute, path string) (string, bool)
|
||||
return "/simple/" + trimmed + "/", true
|
||||
}
|
||||
|
||||
func parseComposerDistURL(path string, rawQuery string) (*url.URL, bool) {
|
||||
if !strings.HasPrefix(path, "/dist/") {
|
||||
return nil, false
|
||||
}
|
||||
trimmed := strings.TrimPrefix(path, "/dist/")
|
||||
parts := strings.SplitN(trimmed, "/", 3)
|
||||
if len(parts) < 3 {
|
||||
return nil, false
|
||||
}
|
||||
scheme := parts[0]
|
||||
host := parts[1]
|
||||
rest := parts[2]
|
||||
if scheme == "" || host == "" {
|
||||
return nil, false
|
||||
}
|
||||
if rest == "" {
|
||||
rest = "/"
|
||||
} else {
|
||||
rest = "/" + rest
|
||||
}
|
||||
target := &url.URL{
|
||||
Scheme: scheme,
|
||||
Host: host,
|
||||
Path: rest,
|
||||
RawPath: rest,
|
||||
}
|
||||
if rawQuery != "" {
|
||||
target.RawQuery = rawQuery
|
||||
}
|
||||
return target, true
|
||||
}
|
||||
|
||||
type bearerChallenge struct {
|
||||
Realm string
|
||||
Service string
|
||||
@@ -1119,6 +1176,8 @@ func ensureProxyHubType(route *server.HubRoute) error {
|
||||
return nil
|
||||
case "pypi":
|
||||
return nil
|
||||
case "composer":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unsupported hub type: %s", route.Config.Type)
|
||||
}
|
||||
|
||||
@@ -150,6 +150,8 @@ func ensureRouterHubType(route *HubRoute) error {
|
||||
return nil
|
||||
case "pypi":
|
||||
return nil
|
||||
case "composer":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unsupported hub type: %s", route.Config.Type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user