1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

fix: unwrap adapter modules to get underlying modules

This commit is contained in:
WeidiDeng 2023-07-10 11:20:29 +08:00
parent 119e8794bc
commit 3cbbd1b62e
No known key found for this signature in database
GPG key ID: 25F87CE1741EC7CD
2 changed files with 22 additions and 3 deletions

View file

@ -135,4 +135,11 @@ func (am adapterModule) CaddyModule() caddy.ModuleInfo {
}
}
// UnwrapAdapter return the original Adapter, this method must be exported
// to be type-assertable 🤷
// hack, https://github.com/caddyserver/caddy/issues/5621
func (am adapterModule) UnwrapAdapter() any {
return am.Adapter
}
var configAdapters = make(map[string]Adapter)

View file

@ -174,6 +174,13 @@ func upgradeBuild(pluginPkgs map[string]struct{}, fl Flags) (int, error) {
return caddy.ExitCodeSuccess, nil
}
func getModPkgPath(iface any) string {
if rv := reflect.ValueOf(iface); rv.Kind() == reflect.Ptr {
iface = reflect.New(reflect.TypeOf(iface).Elem()).Elem().Interface()
}
return reflect.TypeOf(iface).PkgPath()
}
func getModules() (standard, nonstandard, unknown []moduleInfo, err error) {
bi, ok := debug.ReadBuildInfo()
if !ok {
@ -195,10 +202,15 @@ func getModules() (standard, nonstandard, unknown []moduleInfo, err error) {
// not sure why), and since New() should return a pointer
// value, we need to dereference it first
iface := any(modInfo.New())
if rv := reflect.ValueOf(iface); rv.Kind() == reflect.Ptr {
iface = reflect.New(reflect.TypeOf(iface).Elem()).Elem().Interface()
modPkgPath := getModPkgPath(iface)
// Unwrap config adapters to get the underlying adapter modules, as config adapter modules are hacks anyway. https://github.com/caddyserver/caddy/issues/5621
// this method will only be called if it's from the built-in module to prevent abuse
if strings.HasPrefix(modPkgPath, caddy.ImportPath) {
if unwrapper, ok := iface.(interface{ UnwrapAdapter() any }); ok {
modPkgPath = getModPkgPath(unwrapper.UnwrapAdapter())
}
}
modPkgPath := reflect.TypeOf(iface).PkgPath()
// now we find the Go module that the Caddy module's package
// belongs to; we assume the Caddy module package path will