diff --git a/README.md b/README.md index dfa06ce..991be1a 100644 --- a/README.md +++ b/README.md @@ -61,41 +61,35 @@ Only one thing user should do is that setting up the database connection, withou }, - "other": { - "plugins": { - - "ssrpanel": { - // Node id on your SSR Panel - "nodeId": 1, - // every N seconds - "checkRate": 60, - // traffic rate - "trafficRate": 1.0, - // gRPC address - "gRPCAddr": "127.0.0.1:10085", - // user config - "user": { - // inbound tag, which inbound you would like add user to - "inboundTag": "proxy", - "level": 0, - "alterId": 16, - "security": "none" - }, - // db connection - "mysql": { - "host": "127.0.0.1", - "port": 3306, - "user": "root", - "password": "ssrpanel", - "dbname": "ssrpanel" - } - } - - + "ssrpanel": { + // Node id on your SSR Panel + "nodeId": 1, + // every N seconds + "checkRate": 60, + // traffic rate + "trafficRate": 1.0, + // gRPC address + "gRPCAddr": "127.0.0.1:10085", + // user config + "user": { + // inbound tag, which inbound you would like add user to + "inboundTag": "proxy", + "level": 0, + "alterId": 16, + "security": "none" + }, + // db connection + "mysql": { + "host": "127.0.0.1", + "port": 3306, + "user": "root", + "password": "ssrpanel", + "dbname": "ssrpanel" } } + } ``` diff --git a/config.go b/config.go index d8b74c5..41983e0 100644 --- a/config.go +++ b/config.go @@ -3,11 +3,11 @@ package v2ray_ssrpanel_plugin import ( "bytes" "encoding/json" - "flag" "io" "os" "path/filepath" "strings" + flag "github.com/spf13/pflag" "v2ray.com/core/common/errors" "v2ray.com/core/common/platform" "v2ray.com/core/common/protocol" @@ -18,12 +18,8 @@ import ( var ( commandLine = flag.NewFlagSet(os.Args[0]+"-plugin-ssrpanel", flag.ContinueOnError) - configFile = commandLine.String("config", "", "Config file for V2Ray.") - _ = commandLine.Bool("version", false, "Show current version of V2Ray.") test = commandLine.Bool("test", false, "Test config file only, without launching V2Ray server.") - _ = commandLine.String("format", "json", "Format of input file.") - _ = commandLine.Bool("plugin", false, "True to load plugins.") ) type UserConfig struct { @@ -53,34 +49,22 @@ func (c *UserConfig) UnmarshalJSON(data []byte) error { return nil } -type myPluginConfig struct { +type Config struct { NodeID uint `json:"nodeId"` CheckRate int `json:"checkRate"` TrafficRate float64 `json:"trafficRate"` MySQL *MySQLConfig `json:"mysql"` UserConfig *UserConfig `json:"user"` GRPCAddr string `json:"gRPCAddr"` + v2rayConfig *conf.Config } -type Config struct { - *conf.Config - Other struct { - Plugins map[string]json.RawMessage `json:"plugins"` - } `json:"other"` - myPluginConfig *myPluginConfig -} - -func testConfig() error { - cfg, err := getConfig() - if err != nil { - return err +func getConfig() (*Config, error) { + type config struct { + *conf.Config + SSRPanel *Config `json:"ssrpanel"` } - logConfig(cfg) - - return nil -} -func getConfig() (*Config, error) { configFile := getConfigFilePath() configInput, err := confloader.LoadConfig(configFile) if err != nil { @@ -88,29 +72,15 @@ func getConfig() (*Config, error) { } defer configInput.Close() - cfg := &Config{} + cfg := &config{} if err = decodeCommentJSON(configInput, cfg); err != nil { return nil, err } - - myCfg := &myPluginConfig{} - if err = json.Unmarshal(cfg.Other.Plugins["ssrpanel"], myCfg); err != nil { - return nil, err - } - cfg.myPluginConfig = myCfg - - if err = checkConfig(cfg); err != nil { - return nil, err + if cfg.SSRPanel != nil { + cfg.SSRPanel.v2rayConfig = cfg.Config } - return cfg, err -} - -func checkConfig(cfg *Config) error { - if cfg.myPluginConfig == nil { - return errors.New("please add SSR Panel config") - } - return nil + return cfg.SSRPanel, err } func getConfigFilePath() string { @@ -145,8 +115,3 @@ func fileExists(file string) bool { info, err := os.Stat(file) return err == nil && !info.IsDir() } - -func logConfig(cfg *Config) { - configContent, _ := json.MarshalIndent(cfg, "", " ") - newError("got config: ", string(configContent)).AtInfo().WriteToLog() -} diff --git a/main/main.go b/main/main.go deleted file mode 100644 index 974c7f9..0000000 --- a/main/main.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "github.com/ColetteContreras/v2ray-ssrpanel-plugin" - "v2ray.com/core" -) - -func GetPluginMetadata() core.PluginMetadata { - v2ray_ssrpanel_plugin.Run() - - return core.PluginMetadata{ - Name: "SSR Panel", - } -} diff --git a/panel.go b/panel.go index cd32bfe..edb37d1 100644 --- a/panel.go +++ b/panel.go @@ -14,20 +14,20 @@ import ( ) type Panel struct { + *Config handlerServiceClient *HandlerServiceClient statsServiceClient *StatsServiceClient db *DB userModels []UserModel - globalConfig *Config startAt time.Time } -func NewPanel(gRPCConn *grpc.ClientConn, db *DB, globalConfig *Config) *Panel { +func NewPanel(gRPCConn *grpc.ClientConn, db *DB, cfg *Config) *Panel { return &Panel{ + Config: cfg, db: db, - handlerServiceClient: NewHandlerServiceClient(gRPCConn, globalConfig.myPluginConfig.UserConfig.InboundTag), + handlerServiceClient: NewHandlerServiceClient(gRPCConn, cfg.UserConfig.InboundTag), statsServiceClient: NewStatsServiceClient(gRPCConn), - globalConfig: globalConfig, startAt: time.Now(), } } @@ -41,7 +41,7 @@ func (p *Panel) Start() { doFunc() c := cron.New() - c.AddFunc(fmt.Sprintf("@every %ds", p.globalConfig.myPluginConfig.CheckRate), doFunc) + c.AddFunc(fmt.Sprintf("@every %ds", p.CheckRate), doFunc) c.Start() c.Run() } @@ -56,7 +56,7 @@ func (p *Panel) do() error { }() p.db.DB.Create(&NodeInfo{ - NodeID: p.globalConfig.myPluginConfig.NodeID, + NodeID: p.NodeID, Uptime: time.Now().Sub(p.startAt) / time.Second, Load: getSystemLoad(), }) @@ -82,7 +82,7 @@ func (p *Panel) do() error { if onlineUsers > 0 { p.db.DB.Create(&NodeOnlineLog{ - NodeID: p.globalConfig.myPluginConfig.NodeID, + NodeID: p.NodeID, OnlineUser: onlineUsers, }) } @@ -119,8 +119,8 @@ func (p *Panel) getTraffic() (userTrafficLogs []UserTrafficLog, err error) { UserID: user.ID, Uplink: uplink, Downlink: downlink, - NodeID: p.globalConfig.myPluginConfig.NodeID, - Rate: p.globalConfig.myPluginConfig.TrafficRate, + NodeID: p.NodeID, + Rate: p.TrafficRate, Traffic: bytefmt.ByteSize(uplink + downlink), }) } @@ -179,7 +179,7 @@ func (p *Panel) syncUser() (addedUserCount, deletedUserCount int, err error) { } func (p *Panel) convertUser(userModel UserModel) *protocol.User { - userCfg := p.globalConfig.myPluginConfig.UserConfig + userCfg := p.UserConfig return &protocol.User{ Level: userCfg.Level, Email: userModel.Email, diff --git a/plugin.go b/plugin.go index 6fc9e49..6bc8758 100644 --- a/plugin.go +++ b/plugin.go @@ -8,31 +8,33 @@ import ( "v2ray.com/core/common/errors" ) -func Run() { - err := run() - if err != nil { - fatal(err) - } +func init() { + go func() { + err := run() + if err != nil { + fatal(err) + } + }() } func run() error { commandLine.Parse(os.Args[1:]) - if *test { - return testConfig() - } - globalCfg, err := getConfig() - if err != nil { + cfg, err := getConfig() + if err != nil || *test || cfg == nil { return err } - db, err := NewMySQLConn(globalCfg.myPluginConfig.MySQL) + // wait v2ray + time.Sleep(time.Second) + + db, err := NewMySQLConn(cfg.MySQL) if err != nil { return err } go func() { - gRPCAddr := globalCfg.myPluginConfig.GRPCAddr + gRPCAddr := cfg.GRPCAddr gRPCConn, err := connectGRPC(gRPCAddr, 10*time.Second) if err != nil { if s, ok := status.FromError(err); ok { @@ -42,7 +44,7 @@ func run() error { } newError(fmt.Sprintf("Connected gRPC server \"%s\" ", gRPCAddr)).AtWarning().WriteToLog() - p := NewPanel(gRPCConn, db, globalCfg) + p := NewPanel(gRPCConn, db, cfg) p.Start() }()