forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (163 loc) · 3.91 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"database/sql"
"fmt"
"os"
"runtime"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/zhaojh329/rttys/config"
rlog "github.com/zhaojh329/rttys/log"
"github.com/zhaojh329/rttys/utils"
"github.com/zhaojh329/rttys/version"
_ "github.com/go-sql-driver/mysql"
)
func initDb(cfg *config.Config) error {
db, err := sql.Open("mysql", cfg.DB)
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS config(name VARCHAR(512) PRIMARY KEY NOT NULL, value TEXT NOT NULL)")
if err != nil {
return err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS account(username VARCHAR(512) PRIMARY KEY NOT NULL, password TEXT NOT NULL, admin INT NOT NULL)")
if err != nil {
return err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS device(id VARCHAR(512) PRIMARY KEY NOT NULL, description TEXT NOT NULL, online DATETIME NOT NULL, username TEXT NOT NULL)")
return err
}
func runRttys(c *cli.Context) {
rlog.SetPath(c.String("log"))
cfg := config.Parse(c)
log.Info().Msg("Go Version: " + runtime.Version())
log.Info().Msgf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
log.Info().Msg("Rttys Version: " + version.Version())
gitCommit := version.GitCommit()
buildTime := version.BuildTime()
if gitCommit != "" {
log.Info().Msg("Git Commit: " + version.GitCommit())
}
if buildTime != "" {
log.Info().Msg("Build Time: " + version.BuildTime())
}
err := initDb(cfg)
if err != nil {
log.Error().Msg("Init database fail:" + err.Error())
return
}
br := newBroker(cfg)
go br.run()
listenDevice(br)
listenDeviceWeb(br)
httpStart(br)
select {}
}
func main() {
defaultLogPath := "/var/log/rttys.log"
if runtime.GOOS == "windows" {
defaultLogPath = "rttys.log"
}
app := &cli.App{
Name: "rttys",
Usage: "The server side for rtty",
Version: version.Version(),
Commands: []*cli.Command{
{
Name: "run",
Usage: "Run rttys",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log",
Value: defaultLogPath,
Usage: "log file path",
},
&cli.StringFlag{
Name: "conf",
Aliases: []string{"c"},
Value: "./rttys.conf",
Usage: "config file to load",
},
&cli.StringFlag{
Name: "addr-dev",
Value: ":5912",
Usage: "address to listen device",
},
&cli.StringFlag{
Name: "addr-user",
Value: ":5913",
Usage: "address to listen user",
},
&cli.StringFlag{
Name: "addr-web",
Value: "",
Usage: "address to listen for web proxy (default auto)",
},
&cli.StringFlag{
Name: "web-redir-url",
Value: "",
Usage: "url to redirect for access device's web",
},
&cli.StringFlag{
Name: "ssl-cert",
Value: "",
Usage: "ssl cert file Path",
},
&cli.StringFlag{
Name: "ssl-key",
Value: "",
Usage: "ssl key file Path",
},
&cli.StringFlag{
Name: "ssl-cacert",
Value: "",
Usage: "mtls CA storage in PEM file Path",
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Value: "",
Usage: "token to use",
},
&cli.StringFlag{
Name: "white-list",
Value: "",
Usage: "white list(device IDs separated by spaces or *)",
},
&cli.StringFlag{
Name: "db",
Value: "rttys:rttys@tcp(localhost)/rttys",
Usage: "mysql database source",
},
&cli.BoolFlag{
Name: "local-auth",
Usage: "need auth for local",
},
},
Action: func(c *cli.Context) error {
runRttys(c)
return nil
},
},
{
Name: "token",
Usage: "Generate a token",
Action: func(c *cli.Context) error {
utils.GenToken()
return nil
},
},
},
Action: func(c *cli.Context) error {
c.App.Command("run").Run(c)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}