forked from zhanggf2010/v2ray-ssrpanel-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
74 lines (61 loc) · 1.51 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package ssrpanel
import (
"fmt"
"google.golang.org/grpc/status"
"os"
"time"
"v2ray.com/core/common/errors"
)
func init() {
go func() {
err := run()
if err != nil {
fatal(err)
}
newError("v0.4.0 started").AtWarning().WriteToLog()
}()
}
func run() error {
commandLine.Parse(os.Args[1:])
cfg, err := getConfig()
if err != nil || *test || cfg == nil {
return err
}
// wait v2ray
time.Sleep(time.Second)
db, err := NewMySQLConn(cfg.MySQL)
if err != nil {
return err
}
go func() {
apiInbound := getInboundConfigByTag(cfg.v2rayConfig.Api.Tag, cfg.v2rayConfig.InboundConfigs)
gRPCAddr := fmt.Sprintf("%s:%d", apiInbound.ListenOn.String(), apiInbound.PortRange.From)
gRPCConn, err := connectGRPC(gRPCAddr, 10*time.Second)
if err != nil {
if s, ok := status.FromError(err); ok {
err = errors.New(s.Message())
}
fatal(fmt.Sprintf("connect to gRPC server \"%s\" err: ", gRPCAddr), err)
}
newErrorf("Connected gRPC server \"%s\" ", gRPCAddr).AtWarning().WriteToLog()
p, err := NewPanel(gRPCConn, db, cfg)
if err != nil {
fatal("new panel error", err)
}
p.Start()
}()
return nil
}
func newErrorf(format string, a ...interface{}) *errors.Error {
return newError(fmt.Sprintf(format, a...))
}
func newError(values ...interface{}) *errors.Error {
values = append([]interface{}{"SSRPanelPlugin: "}, values...)
return errors.New(values...)
}
func fatal(values ...interface{}) {
newError(values...).AtError().WriteToLog()
// Wait log
time.Sleep(1 * time.Second)
os.Exit(-2)
}