Skip to content

Commit

Permalink
feat: 修复panic
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Sep 4, 2023
1 parent 98c6e7a commit 7e6965a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions core/WsManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func process(client *WsClient) {
}
go client.WriteMsg()
go client.ReadMsg(func(c *WsClient, message Message) {
defer func() {
recover()
}()
services := client.wsManager.services
if service, ok := services[message.Type]; ok {
service(c, message)
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import (
)

var (
upgrader = websocket.Upgrader{}
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
loggerConfig = middleware.LoggerWithConfig(middleware.LoggerConfig{
Skipper: func(c echo.Context) bool {
if c.Path() == "/ping" {
Expand Down
45 changes: 39 additions & 6 deletions util/analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package analytics

import (
"fmt"
fmt "fmt"
"github.com/ystyle/google-analytics"
"math/rand"
"os"
"runtime"
"time"
)

var (
Expand All @@ -14,15 +14,14 @@ var (
version string
)

func Analytics(client, format string) {
func Analytics(clientID, format string) {
if secret == "" || measurement == "" {
return
}
t := time.Now().Unix()
analytics.SetKeys(secret, measurement) // // required
payload := analytics.Payload{
ClientID: fmt.Sprintf("%d.%d", rand.Int31(), t), // required
UserID: client,
ClientID: clientID, // required
UserID: getClientID(),
Events: []analytics.Event{
{
Name: "kas", // required
Expand All @@ -37,3 +36,37 @@ func Analytics(client, format string) {
}
analytics.Send(payload)
}

func getClientID() string {
clientID := fmt.Sprintf("%d", rand.Uint32())
config, err := os.UserConfigDir()
if err != nil {
return clientID
}
filepath := fmt.Sprintf("%s/kas/config", config)
if exist, _ := isExists(filepath); exist {
bs, err := os.ReadFile(filepath)
if err != nil {
return clientID
}
clientID = string(bs)
} else {
err := os.MkdirAll(fmt.Sprintf("%s/kas", config), 0700)
if err != nil {
return clientID
}
_ = os.WriteFile(filepath, []byte(clientID), 0700)
}
return clientID
}

func isExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

0 comments on commit 7e6965a

Please sign in to comment.