Skip to content

Commit

Permalink
Update config, remove main package
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-i-pc committed Dec 7, 2018
1 parent 5164f83 commit 89c9c10
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 114 deletions.
56 changes: 25 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}



}
```

Expand Down
57 changes: 11 additions & 46 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -53,64 +49,38 @@ 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 {
return nil, errors.New("failed to load config: ", configFile).Base(err)
}
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 {
Expand Down Expand Up @@ -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()
}
14 changes: 0 additions & 14 deletions main/main.go

This file was deleted.

20 changes: 10 additions & 10 deletions panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand All @@ -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()
}
Expand All @@ -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(),
})
Expand All @@ -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,
})
}
Expand Down Expand Up @@ -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),
})
}
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 15 additions & 13 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}()

Expand Down

0 comments on commit 89c9c10

Please sign in to comment.