1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

prototype for plugins

This commit is contained in:
Darien Raymond 2017-11-22 23:00:46 +01:00
parent bfafd06863
commit 6069b77baa
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 73 additions and 0 deletions

View File

@ -53,8 +53,20 @@ func getExecutableDir() string {
return filepath.Dir(exec) return filepath.Dir(exec)
} }
func getExecuableSubDir(dir string) func() string {
return func() string {
return filepath.Join(getExecutableDir(), dir)
}
}
func GetAssetLocation(file string) string { func GetAssetLocation(file string) string {
const name = "v2ray.location.asset" const name = "v2ray.location.asset"
assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
return filepath.Join(assetPath, file) return filepath.Join(assetPath, file)
} }
func GetPluginDirectory() string {
const name = "v2ray.location.plugin"
pluginDir := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecuableSubDir("plugins"))
return pluginDir
}

View File

@ -22,6 +22,7 @@ var (
version = flag.Bool("version", false, "Show current version of V2Ray.") version = flag.Bool("version", false, "Show current version of V2Ray.")
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.") test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
format = flag.String("format", "json", "Format of input file.") format = flag.String("format", "json", "Format of input file.")
plugin = flag.Bool("plugin", false, "True to load plugins.")
) )
func init() { func init() {
@ -82,6 +83,13 @@ func main() {
return return
} }
if *plugin {
if err := core.LoadPlugins(); err != nil {
fmt.Println("Failed to load plugins:", err.Error())
return
}
}
server, err := startV2Ray() server, err := startV2Ray()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())

53
plugin.go Normal file
View File

@ -0,0 +1,53 @@
package core
import (
"os"
"path/filepath"
"plugin"
"strings"
"v2ray.com/core/app/log"
"v2ray.com/core/common/platform"
)
type PluginMetadata struct {
Name string
}
const GetMetadataFuncName = "GetPluginMetadata"
type GetMetadataFunc func() PluginMetadata
func LoadPlugins() error {
pluginPath := platform.GetPluginDirectory()
dir, err := os.Open(pluginPath)
if err != nil {
return err
}
defer dir.Close()
files, err := dir.Readdir(-1)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".so") {
p, err := plugin.Open(filepath.Join(pluginPath, file.Name()))
if err != nil {
return err
}
f, err := p.Lookup(GetMetadataFuncName)
if err != nil {
return err
}
if gmf, ok := f.(GetMetadataFunc); ok {
metadata := gmf()
log.Trace(newError("plugin (", metadata.Name, ") loaded."))
}
}
}
return nil
}