style: using is struct to handle all judgements global envs
This commit is contained in:
Vendored
+2
-1
@@ -4,12 +4,13 @@
|
|||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
|
||||||
{
|
{
|
||||||
"name": "Launch Package",
|
"name": "Launch Package",
|
||||||
"type": "go",
|
"type": "go",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"program": "${fileDirname}"
|
"program": "${fileDirname}/../"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
+1
-1
@@ -54,7 +54,7 @@ func Auth(w http.ResponseWriter, r *http.Request, targetMethod string, allowedUs
|
|||||||
return 0, fmt.Errorf("Method not allowed: %s", targetMethod)
|
return 0, fmt.Errorf("Method not allowed: %s", targetMethod)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isDebug && !ValidateTimeStamp(r.Header) {
|
if !is.debug && !ValidateTimeStamp(r.Header) {
|
||||||
return 0, fmt.Errorf("Invalid or missing X-Timestamp in header")
|
return 0, fmt.Errorf("Invalid or missing X-Timestamp in header")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,23 +22,31 @@ type SoftwareInfo struct {
|
|||||||
BuildType string
|
BuildType string
|
||||||
}
|
}
|
||||||
|
|
||||||
var softwareInfo SoftwareInfo
|
var softwareInfo SoftwareInfo = SoftwareInfo{
|
||||||
|
|
||||||
type StatusInfo struct {
|
|
||||||
Status string
|
|
||||||
}
|
|
||||||
|
|
||||||
var isDebug bool
|
|
||||||
var isOnline bool
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
softwareInfo = SoftwareInfo{
|
|
||||||
Name: "Super-frpc",
|
Name: "Super-frpc",
|
||||||
Version: "0.0.1",
|
Version: "0.0.1",
|
||||||
Developer: "Madobi Nanami",
|
Developer: "Madobi Nanami",
|
||||||
BuildVer: 1,
|
BuildVer: 1,
|
||||||
BuildType: "debug",
|
BuildType: "debug",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StatusInfo struct {
|
||||||
|
Status string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Is struct {
|
||||||
|
debug bool
|
||||||
|
online bool
|
||||||
|
watchdogConnected bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var is Is = Is {
|
||||||
|
debug: false,
|
||||||
|
online: false,
|
||||||
|
watchdogConnected: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer))
|
postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer))
|
||||||
configPath := flag.String("config", "./config.json", "path to config file")
|
configPath := flag.String("config", "./config.json", "path to config file")
|
||||||
dbPath_data := flag.String("db", "./database.db", "path to database file")
|
dbPath_data := flag.String("db", "./database.db", "path to database file")
|
||||||
@@ -63,7 +71,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
postLog.SetDebugMode(config.Debug)
|
postLog.SetDebugMode(config.Debug)
|
||||||
isDebug = config.Debug
|
is.debug = config.Debug
|
||||||
|
|
||||||
if err := postLog.InitLogsDatabase(*dbPath_log); err != nil {
|
if err := postLog.InitLogsDatabase(*dbPath_log); err != nil {
|
||||||
postLog.Fatal(fmt.Sprintf("Failed to initialize logs database: %v", err))
|
postLog.Fatal(fmt.Sprintf("Failed to initialize logs database: %v", err))
|
||||||
@@ -112,7 +120,7 @@ func main() {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
postLog.Info(fmt.Sprintf("Server starting on %s", addr))
|
postLog.Info(fmt.Sprintf("Server starting on %s", addr))
|
||||||
isOnline = true
|
is.online = true
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
postLog.Fatal(fmt.Sprintf("Failed to start server: %v", err))
|
postLog.Fatal(fmt.Sprintf("Failed to start server: %v", err))
|
||||||
}
|
}
|
||||||
@@ -152,7 +160,7 @@ func GetStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
statusInfo := StatusInfo{
|
statusInfo := StatusInfo{
|
||||||
Status: "Online",
|
Status: "Online",
|
||||||
}
|
}
|
||||||
if !isOnline {
|
if !is.online {
|
||||||
statusInfo.Status = "Offline"
|
statusInfo.Status = "Offline"
|
||||||
}
|
}
|
||||||
SendSuccessResponse(w, "getStatus", statusInfo)
|
SendSuccessResponse(w, "getStatus", statusInfo)
|
||||||
|
|||||||
+1
-1
@@ -221,7 +221,7 @@ func isValidPassword(password string) bool { // Validate password complexity and
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ValidateTimeStamp(header http.Header) bool {
|
func ValidateTimeStamp(header http.Header) bool {
|
||||||
if isDebug {
|
if is.debug {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+23
-11
@@ -15,12 +15,9 @@ var (
|
|||||||
isConnected bool
|
isConnected bool
|
||||||
recvChan chan string
|
recvChan chan string
|
||||||
stopRecvChan chan struct{}
|
stopRecvChan chan struct{}
|
||||||
|
recvWg sync.WaitGroup
|
||||||
)
|
)
|
||||||
|
|
||||||
func notifyMessage(message string) {
|
|
||||||
postLog.Info(fmt.Sprintf("%s", message))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Init() error {
|
func Init() error {
|
||||||
tcpConnMutex.Lock()
|
tcpConnMutex.Lock()
|
||||||
defer tcpConnMutex.Unlock()
|
defer tcpConnMutex.Unlock()
|
||||||
@@ -53,6 +50,7 @@ func tcpConnect(ipaddr string, port int) error {
|
|||||||
tcpConn = conn
|
tcpConn = conn
|
||||||
isConnected = true
|
isConnected = true
|
||||||
|
|
||||||
|
recvWg.Add(1)
|
||||||
go recvMsg()
|
go recvMsg()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -66,7 +64,7 @@ func sendMsg(message string, target int) (string, error) {
|
|||||||
return "", fmt.Errorf("not connected")
|
return "", fmt.Errorf("not connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := tcpConn.Write([]byte(message + "\n"))
|
_, err := tcpConn.Write([]byte(message))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tcpConnMutex.Unlock()
|
tcpConnMutex.Unlock()
|
||||||
return "", fmt.Errorf("failed to send message: %v", err)
|
return "", fmt.Errorf("failed to send message: %v", err)
|
||||||
@@ -83,13 +81,23 @@ func sendMsg(message string, target int) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func recvMsg() {
|
func recvMsg() {
|
||||||
reader := bufio.NewReader(tcpConn)
|
defer recvWg.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
tcpConnMutex.Lock()
|
||||||
|
if tcpConn == nil {
|
||||||
|
tcpConnMutex.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn := tcpConn
|
||||||
|
tcpConnMutex.Unlock()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-stopRecvChan:
|
case <-stopRecvChan:
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
tcpConn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
|
reader := bufio.NewReader(conn)
|
||||||
line, err := reader.ReadString('\n')
|
line, err := reader.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||||
@@ -118,7 +126,7 @@ func recvMsg() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(line) > 0 && line != "success" && !isResponseMessage(line) {
|
if len(line) > 0 && line != "success" && !isResponseMessage(line) {
|
||||||
notifyMessage(line)
|
postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,17 +137,21 @@ func isResponseMessage(msg string) bool {
|
|||||||
return msg == "success" || msg == "failed"
|
return msg == "success" || msg == "failed"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func Destroy() error {
|
func Destroy() error {
|
||||||
tcpConnMutex.Lock()
|
tcpConnMutex.Lock()
|
||||||
defer tcpConnMutex.Unlock()
|
|
||||||
|
|
||||||
if stopRecvChan != nil {
|
if stopRecvChan != nil {
|
||||||
close(stopRecvChan)
|
close(stopRecvChan)
|
||||||
stopRecvChan = nil
|
stopRecvChan = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tcpConnMutex.Unlock()
|
||||||
|
|
||||||
|
recvWg.Wait()
|
||||||
|
|
||||||
|
tcpConnMutex.Lock()
|
||||||
|
defer tcpConnMutex.Unlock()
|
||||||
|
|
||||||
if tcpConn != nil {
|
if tcpConn != nil {
|
||||||
err := tcpConn.Close()
|
err := tcpConn.Close()
|
||||||
tcpConn = nil
|
tcpConn = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user