forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (106 loc) · 3.15 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
/*
* Copyright (C) 2017 Jianhui Zhao <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package main
import (
"crypto/md5"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"io"
"os"
"runtime"
"time"
"github.com/kylelemons/go-gypsy/yaml"
"github.com/howeyc/gopass"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
)
type RttysConfig struct {
addr string
sslCert string
sslKey string
username string
password string
token string
}
func init() {
log.AddHook(lfshook.NewHook("/var/log/rttys.log", &log.TextFormatter{}))
}
func main() {
cfg := parseConfig()
if !checkUser() && cfg.username == "" {
log.Error("Operation not permitted. Please start as root or define Username and Password in configuration file")
os.Exit(1)
}
log.Infof("go version: %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
log.Info("rttys version:", rttys_version())
br := newBroker()
go br.run()
httpStart(br, cfg)
}
func genUniqueID(extra string) string {
buf := make([]byte, 20)
binary.BigEndian.PutUint32(buf, uint32(time.Now().Unix()))
io.ReadFull(rand.Reader, buf[4:])
h := md5.New()
h.Write(buf)
h.Write([]byte(extra))
return hex.EncodeToString(h.Sum(nil))
}
func setConfigOpt(yamlCfg *yaml.File, name string, opt *string) {
val, err := yamlCfg.Get(name)
if err != nil {
return
}
*opt = val
}
func parseConfig() *RttysConfig {
cfg := &RttysConfig{}
flag.StringVar(&cfg.addr, "addr", ":5912", "address to listen")
flag.StringVar(&cfg.sslCert, "ssl-cert", "./rttys.crt", "certFile Path")
flag.StringVar(&cfg.sslKey, "ssl-key", "./rttys.key", "keyFile Path")
flag.StringVar(&cfg.token, "token", "", "token to use")
conf := flag.String("conf", "./rttys.conf", "config file to load")
genToken := flag.Bool("gen-token", false, "generate token")
flag.Parse()
if *genToken {
genTokenAndExit()
}
yamlCfg, err := yaml.ReadFile(*conf)
if err == nil {
setConfigOpt(yamlCfg, "addr", &cfg.addr)
setConfigOpt(yamlCfg, "ssl-cert", &cfg.sslCert)
setConfigOpt(yamlCfg, "ssl-key", &cfg.sslKey)
setConfigOpt(yamlCfg, "username", &cfg.username)
setConfigOpt(yamlCfg, "password", &cfg.password)
setConfigOpt(yamlCfg, "token", &cfg.token)
}
return cfg
}
func genTokenAndExit() {
password, err := gopass.GetPasswdPrompt("Please set a password:", true, os.Stdin, os.Stdout)
if err != nil {
log.Fatal(err)
}
token := genUniqueID(string(password))
fmt.Println("Your token is:", token)
os.Exit(0)
}