mirror of
https://github.com/netfun2000/rttys_zhaojh329.git
synced 2026-02-27 09:53:24 +08:00
@@ -42,6 +42,8 @@ This is the server program of [rtty](https://github.com/zhaojh329/rtty)
|
||||
password for http auth
|
||||
-http-username string
|
||||
username for http auth
|
||||
-log string
|
||||
log file path (default "/var/log/rttys.log")
|
||||
-ssl-cert string
|
||||
certFile Path (default "./rttys.crt")
|
||||
-ssl-key string
|
||||
@@ -49,10 +51,6 @@ This is the server program of [rtty](https://github.com/zhaojh329/rtty)
|
||||
-token string
|
||||
token to use
|
||||
|
||||
## View logs when running in the background
|
||||
|
||||
cat /var/log/rttys.log
|
||||
|
||||
## Authorization
|
||||
|
||||
./rttys -gen-token
|
||||
|
||||
+2
-4
@@ -42,6 +42,8 @@
|
||||
password for http auth
|
||||
-http-username string
|
||||
username for http auth
|
||||
-log string
|
||||
log file path (default "/var/log/rttys.log")
|
||||
-ssl-cert string
|
||||
certFile Path (default "./rttys.crt")
|
||||
-ssl-key string
|
||||
@@ -49,10 +51,6 @@
|
||||
-token string
|
||||
token to use
|
||||
|
||||
## 在后台运行模式下查看日志
|
||||
|
||||
cat /var/log/rttys.log
|
||||
|
||||
## 认证
|
||||
|
||||
./rttys -gen-token
|
||||
|
||||
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
@@ -56,16 +56,16 @@ func (br *Broker) run() {
|
||||
msg := "OK"
|
||||
|
||||
if _, ok := br.devices[dev.id]; ok {
|
||||
log.Error("Device ID conflicting: ", dev.id)
|
||||
log.Error().Msg("Device ID conflicting: " + dev.id)
|
||||
msg = "ID conflicting"
|
||||
err = 1
|
||||
} else if dev.token != br.token {
|
||||
log.Error("Invalid token from terminal device")
|
||||
log.Error().Msg("Invalid token from terminal device")
|
||||
msg = "Invalid token"
|
||||
err = 1
|
||||
} else {
|
||||
br.devices[dev.id] = dev
|
||||
log.Info("New device: ", dev.id)
|
||||
log.Info().Msg("New device: " + dev.id)
|
||||
}
|
||||
|
||||
dev.writeMsg(MsgTypeRegister, append([]byte{err}, msg...))
|
||||
@@ -82,7 +82,7 @@ func (br *Broker) run() {
|
||||
if session.dev == dev {
|
||||
session.user.close()
|
||||
delete(br.sessions, sid)
|
||||
log.Info("Delete session: ", sid)
|
||||
log.Info().Msg("Delete session: " + sid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,11 +90,11 @@ func (br *Broker) run() {
|
||||
if dev, ok := br.devices[user.devid]; ok {
|
||||
if !dev.login(user) {
|
||||
user.loginAck(LoginErrorBusy)
|
||||
log.Errorf("Device '%s' is busy", dev.id)
|
||||
log.Error().Msgf("Device '%s' is busy", dev.id)
|
||||
}
|
||||
} else {
|
||||
user.loginAck(LoginErrorOffline)
|
||||
log.Errorf("Not found the device '%s'", user.devid)
|
||||
log.Error().Msgf("Not found the device '%s'", user.devid)
|
||||
}
|
||||
|
||||
case sid := <-br.logout:
|
||||
@@ -102,7 +102,7 @@ func (br *Broker) run() {
|
||||
delete(br.sessions, sid)
|
||||
session.user.close()
|
||||
session.dev.logout(sid[len(sid)-1] - '0')
|
||||
log.Info("Delete session: ", sid)
|
||||
log.Info().Msg("Delete session: " + sid)
|
||||
}
|
||||
|
||||
case session := <-br.newSession:
|
||||
@@ -110,7 +110,7 @@ func (br *Broker) run() {
|
||||
session.user.sid = sid
|
||||
session.user.loginAck(LoginErrorNone)
|
||||
br.sessions[sid] = session
|
||||
log.Infof("New session: %s", sid)
|
||||
log.Info().Msg("New session: " + sid)
|
||||
|
||||
case msg := <-br.devMessage:
|
||||
sid := msg.devid + string(msg.sid+'0')
|
||||
@@ -147,7 +147,7 @@ func (br *Broker) run() {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Error("Not found sid: ", msg.sid)
|
||||
log.Error().Msg("Not found sid: " + msg.sid)
|
||||
}
|
||||
|
||||
case cmdReq := <-br.cmdReq:
|
||||
|
||||
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"github.com/kylelemons/go-gypsy/yaml"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -61,13 +61,13 @@ func parseConfig() *RttysConfig {
|
||||
if cfg.sslCert != "" && cfg.sslKey != "" {
|
||||
_, err := os.Lstat(cfg.sslCert)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
cfg.sslCert = ""
|
||||
}
|
||||
|
||||
_, err = os.Lstat(cfg.sslKey)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
cfg.sslKey = ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bufio"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -74,7 +74,7 @@ func (dev *Device) handleLogin(code byte, sid byte) {
|
||||
dev.user = nil
|
||||
|
||||
if code == 1 {
|
||||
log.Errorf("login fail, device busy")
|
||||
log.Error().Msg("login fail, device busy")
|
||||
user.loginAck(LoginErrorBusy)
|
||||
return
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func (dev *Device) keepAlive() {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if time.Now().Sub(dev.active) > HeartbeatInterval*3/2 {
|
||||
log.Errorf("Inactive device in long time, now kill it: %s %s", dev.id, time.Now())
|
||||
log.Error().Msgf("Inactive device in long time, now kill it: %s %s", dev.id, time.Now())
|
||||
dev.close()
|
||||
return
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func (dev *Device) close() {
|
||||
dev.br.unregister <- dev
|
||||
close(dev.closeCh)
|
||||
dev.conn.Close()
|
||||
log.Infof("Device '%s' closed", dev.id)
|
||||
log.Info().Msgf("Device '%s' closed", dev.id)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func (dev *Device) readLoop() {
|
||||
b, err := br.Peek(3)
|
||||
if err != nil {
|
||||
if err != io.EOF && !strings.Contains(err.Error(), "use of closed network connection") {
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -160,7 +160,7 @@ func (dev *Device) readLoop() {
|
||||
|
||||
_, err := br.Peek(msgLen + 3)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -206,7 +206,7 @@ func (dev *Device) readLoop() {
|
||||
dev.writeMsg(MsgTypeHeartbeat, []byte{})
|
||||
|
||||
default:
|
||||
log.Error("invalid msg type")
|
||||
log.Error().Msg("invalid msg type")
|
||||
br.Discard(msgLen)
|
||||
}
|
||||
|
||||
@@ -217,14 +217,14 @@ func (dev *Device) readLoop() {
|
||||
func listenDevice(br *Broker, cfg *RttysConfig) {
|
||||
ln, err := net.Listen("tcp", cfg.addrDev)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal().Msg(err.Error())
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
if cfg.sslCert != "" && cfg.sslKey != "" {
|
||||
crt, err := tls.LoadX509KeyPair(cfg.sslCert, cfg.sslKey)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
log.Fatal().Msg(err.Error())
|
||||
}
|
||||
|
||||
tlsConfig := &tls.Config{}
|
||||
@@ -233,15 +233,15 @@ func listenDevice(br *Broker, cfg *RttysConfig) {
|
||||
tlsConfig.Rand = rand.Reader
|
||||
|
||||
ln = tls.NewListener(ln, tlsConfig)
|
||||
log.Info("Listen device on: ", cfg.addrDev, " SSL on")
|
||||
log.Info().Msgf("Listen device on: %s SSL on", cfg.addrDev)
|
||||
} else {
|
||||
log.Info("Listen device on: ", cfg.addrDev, " SSL off")
|
||||
log.Info().Msgf("Listen device on: %s SSL off", cfg.addrDev)
|
||||
}
|
||||
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -3,14 +3,13 @@ module github.com/zhaojh329/rttys
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/GehirnInc/crypt v0.0.0-20190301055215-6c0105aabd46
|
||||
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76
|
||||
github.com/gorilla/websocket v1.4.1
|
||||
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c
|
||||
github.com/json-iterator/go v1.1.7
|
||||
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
|
||||
github.com/json-iterator/go v1.1.9
|
||||
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28
|
||||
github.com/msteinert/pam v0.0.0-20190215180659-f29b9f28d6f9
|
||||
github.com/mattn/go-colorable v0.1.4
|
||||
github.com/rakyll/statik v0.1.6
|
||||
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
|
||||
github.com/sirupsen/logrus v1.4.2
|
||||
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472
|
||||
github.com/rs/zerolog v1.18.0
|
||||
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678
|
||||
)
|
||||
|
||||
@@ -1,41 +1,45 @@
|
||||
github.com/GehirnInc/crypt v0.0.0-20190301055215-6c0105aabd46 h1:rs0kDBt2zF4/CM9rO5/iH+U22jnTygPlqWgX55Ufcxg=
|
||||
github.com/GehirnInc/crypt v0.0.0-20190301055215-6c0105aabd46/go.mod h1:kC29dT1vFpj7py2OvG1khBdQpo3kInWP+6QipLbdngo=
|
||||
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76 h1:eObfFy0e/9OQCd5tHy+855jrW7zTihdgIPD7hf2SOQ0=
|
||||
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76/go.mod h1:WPzFRpaqRmrZAD1vMpqUGZR24FE1EBoSG9lHKQyZOMM=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c h1:kQWxfPIHVLbgLzphqk3QUflDy9QdksZR4ygR807bpy0=
|
||||
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
|
||||
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
|
||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c h1:aY2hhxLhjEAbfXOx2nRJxCXezC6CO2V/yN+OCr1srtk=
|
||||
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
|
||||
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28 h1:mkl3tvPHIuPaWsLtmHTybJeoVEW7cbePK73Ir8VtruA=
|
||||
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28/go.mod h1:T/T7jsxVqf9k/zYOqbgNAsANsjxTd1Yq3htjDhQ1H0c=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/msteinert/pam v0.0.0-20190215180659-f29b9f28d6f9 h1:ZivaaKmjs9q90zi6I4gTLW6tbVGtlBjellr3hMYaly0=
|
||||
github.com/msteinert/pam v0.0.0-20190215180659-f29b9f28d6f9/go.mod h1:np1wUFZ6tyoke22qDJZY40URn9Ae51gX7ljIWXN5TJs=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
|
||||
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
|
||||
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo=
|
||||
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM=
|
||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
|
||||
github.com/rs/zerolog v1.18.0 h1:CbAm3kP2Tptby1i9sYy2MGRg0uxIN9cyDb59Ys7W8z8=
|
||||
github.com/rs/zerolog v1.18.0/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 h1:Gv7RPwsi3eZ2Fgewe3CBsuOebPwO27PoXzRpJPsvSSM=
|
||||
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 h1:wCWoJcFExDgyYx2m2hpHgwz8W3+FPdfldvIgzqDIhyg=
|
||||
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
|
||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/rakyll/statik/fs"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/zhaojh329/rttys/cache"
|
||||
_ "github.com/zhaojh329/rttys/statik"
|
||||
"io/ioutil"
|
||||
@@ -62,7 +62,7 @@ func httpStart(br *Broker, cfg *RttysConfig) {
|
||||
|
||||
statikFS, err := fs.New()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal().Msg(err.Error())
|
||||
}
|
||||
|
||||
staticfs := http.FileServer(statikFS)
|
||||
@@ -94,7 +94,7 @@ func httpStart(br *Broker, cfg *RttysConfig) {
|
||||
} else if r.Method == "POST" {
|
||||
content, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -185,10 +185,10 @@ func httpStart(br *Broker, cfg *RttysConfig) {
|
||||
}
|
||||
|
||||
if cfg.sslCert != "" && cfg.sslKey != "" {
|
||||
log.Info("Listen user on: ", cfg.addrUser, " SSL on")
|
||||
log.Fatal(http.ListenAndServeTLS(cfg.addrUser, cfg.sslCert, cfg.sslKey, nil))
|
||||
log.Info().Msgf("Listen user on: %s SSL on")
|
||||
log.Fatal().Msg(http.ListenAndServeTLS(cfg.addrUser, cfg.sslCert, cfg.sslKey, nil).Error())
|
||||
} else {
|
||||
log.Info("Listen user on: ", cfg.addrUser, " SSL off")
|
||||
log.Fatal(http.ListenAndServe(cfg.addrUser, nil))
|
||||
log.Info().Msgf("Listen user on: %s SSL off", cfg.addrUser)
|
||||
log.Fatal().Msg(http.ListenAndServe(cfg.addrUser, nil).Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,95 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/rifflock/lfshook"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"flag"
|
||||
"github.com/dwdcth/consoleEx"
|
||||
"github.com/mattn/go-colorable"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/zhaojh329/rttys/version"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
||||
type LogFileHook struct {
|
||||
err error
|
||||
path string
|
||||
}
|
||||
|
||||
var logFile = &LogFileHook{}
|
||||
|
||||
func (h *LogFileHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
|
||||
if h.err != nil {
|
||||
return
|
||||
}
|
||||
log.AddHook(lfshook.NewHook("/var/log/rttys.log", &log.TextFormatter{}))
|
||||
|
||||
f, err := os.OpenFile(h.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
h.err = err
|
||||
log.Fatal().Msg(err.Error())
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
f.WriteString(zerolog.TimestampFunc().Format(zerolog.TimeFieldFormat) + " |")
|
||||
f.WriteString(strings.ToUpper(level.String()) + "| ")
|
||||
|
||||
_, file, line, ok := runtime.Caller(3)
|
||||
if ok {
|
||||
f.WriteString(zerolog.CallerMarshalFunc(file, line) + " |")
|
||||
}
|
||||
|
||||
f.WriteString(msg)
|
||||
f.WriteString("\n")
|
||||
}
|
||||
|
||||
func init() {
|
||||
zerolog.CallerMarshalFunc = func(file string, line int) string {
|
||||
return filepath.Base(file) + ":" + strconv.Itoa(line)
|
||||
}
|
||||
|
||||
out := consoleEx.ConsoleWriterEx{Out: colorable.NewColorableStdout()}
|
||||
logger := zerolog.New(out).With().Caller().Timestamp().Logger().Hook(logFile)
|
||||
|
||||
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
|
||||
logger = logger.Hook(logFile)
|
||||
}
|
||||
|
||||
log.Logger = logger
|
||||
}
|
||||
|
||||
func main() {
|
||||
if runtime.GOOS == "windows" {
|
||||
flag.StringVar(&logFile.path, "log", "rttys.log", "log file path")
|
||||
} else {
|
||||
flag.StringVar(&logFile.path, "log", "/var/log/rttys.log", "log file path")
|
||||
}
|
||||
|
||||
cfg := parseConfig()
|
||||
|
||||
if cfg.httpUsername == "" {
|
||||
log.Fatal("You must configure the http username by commandline or config file")
|
||||
log.Fatal().Msg("You must configure the http username by commandline or config file")
|
||||
}
|
||||
|
||||
log.Info("Go Version: ", runtime.Version())
|
||||
log.Info("Go OS/Arch: ", runtime.GOOS, "/", runtime.GOARCH)
|
||||
log.Info().Msg("Go Version: " + runtime.Version())
|
||||
log.Info().Msgf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
|
||||
|
||||
log.Info("Rttys Version: ", version.Version())
|
||||
log.Info().Msg("Rttys Version: " + version.Version())
|
||||
|
||||
gitCommit := version.GitCommit()
|
||||
buildTime := version.BuildTime()
|
||||
|
||||
if gitCommit != "" {
|
||||
log.Info("Git Commit: ", version.GitCommit())
|
||||
log.Info().Msg("Git Commit: " + version.GitCommit())
|
||||
}
|
||||
|
||||
if buildTime != "" {
|
||||
log.Info("Build Time: ", version.BuildTime())
|
||||
log.Info().Msg("Build Time: " + version.BuildTime())
|
||||
}
|
||||
|
||||
br := newBroker(cfg.token)
|
||||
|
||||
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/websocket"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"sync"
|
||||
|
||||
"net/http"
|
||||
@@ -64,7 +64,7 @@ func (u *User) readLoop() {
|
||||
msgType, data, err := u.conn.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func serveUser(br *Broker, w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
http.Error(w, "Upgrade fail", http.StatusBadRequest)
|
||||
log.Error(err)
|
||||
log.Error().Msg(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/howeyc/gopass"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
@@ -74,7 +74,7 @@ func genUniqueID(extra string) string {
|
||||
func genTokenAndExit() {
|
||||
password, err := gopass.GetPasswdPrompt("Please set a password:", true, os.Stdin, os.Stdout)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal().Msg(err.Error())
|
||||
}
|
||||
|
||||
token := genUniqueID(string(password))
|
||||
|
||||
Reference in New Issue
Block a user