🔨 Changes go net websocket to gorilla websocket
This commit is contained in:
parent
0264c51970
commit
a86eed2f72
@ -3,12 +3,13 @@ module svitan.dev/keys/server
|
|||||||
go 1.24.1
|
go 1.24.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/gorilla/websocket v1.5.3
|
||||||
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/labstack/echo/v4 v4.13.3
|
github.com/labstack/echo/v4 v4.13.3
|
||||||
github.com/labstack/gommon v0.4.2
|
github.com/labstack/gommon v0.4.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/joho/godotenv v1.5.1 // indirect
|
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
|
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
|
||||||
|
115
server/main.go
115
server/main.go
@ -8,11 +8,11 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
"golang.org/x/net/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const TimeFormat = "2006-01-02 15:04:05"
|
const TimeFormat = "2006-01-02 15:04:05"
|
||||||
@ -27,6 +27,7 @@ var id = 0
|
|||||||
var clients = []*Client{}
|
var clients = []*Client{}
|
||||||
var keyDir = "."
|
var keyDir = "."
|
||||||
var token = ""
|
var token = ""
|
||||||
|
var upgrader = websocket.Upgrader{}
|
||||||
|
|
||||||
type ReqData struct {
|
type ReqData struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
@ -90,7 +91,7 @@ func main() {
|
|||||||
app.GET("/", func(c echo.Context) error {
|
app.GET("/", func(c echo.Context) error {
|
||||||
return c.JSON(http.StatusOK, "Hello World!")
|
return c.JSON(http.StatusOK, "Hello World!")
|
||||||
})
|
})
|
||||||
app.GET("/key", key)
|
app.GET("/keys", keys)
|
||||||
|
|
||||||
app.GET("/clients", func(c echo.Context) error {
|
app.GET("/clients", func(c echo.Context) error {
|
||||||
if c.Request().Header.Get("Authorization") != token {
|
if c.Request().Header.Get("Authorization") != token {
|
||||||
@ -130,63 +131,67 @@ func main() {
|
|||||||
log.Fatal(app.Start(":8080"))
|
log.Fatal(app.Start(":8080"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func key(c echo.Context) error {
|
func keys(c echo.Context) error {
|
||||||
websocket.Handler(func(ws *websocket.Conn) {
|
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
|
||||||
client := Client{
|
if err != nil {
|
||||||
ID: id,
|
return err
|
||||||
conn: ws,
|
|
||||||
Exit: false,
|
|
||||||
}
|
|
||||||
clients = append(clients, &client)
|
|
||||||
id += 1
|
|
||||||
fmt.Printf("%s client %-3d connected", time.Now().Format(TimeFormat), client.ID)
|
|
||||||
|
|
||||||
defer ws.Close()
|
}
|
||||||
err := websocket.Message.Send(ws, "welcome")
|
defer ws.Close()
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.OpenFile(fmt.Sprintf("%s/%s_%d.txt", keyDir, time.Now().Format("2006-01-02_15-04"), client.ID), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
|
client := Client{
|
||||||
if err != nil {
|
ID: id,
|
||||||
log.Error(err)
|
conn: ws,
|
||||||
fmt.Printf("%s client %-3d crashed (couldn't open file)", time.Now().Format(TimeFormat), client.ID)
|
Exit: false,
|
||||||
client.Exit = true
|
}
|
||||||
return
|
clients = append(clients, &client)
|
||||||
}
|
id += 1
|
||||||
|
fmt.Printf("%s client %-3d connected", time.Now().Format(TimeFormat), client.ID)
|
||||||
|
|
||||||
for {
|
err = ws.WriteMessage(websocket.TextMessage, []byte("welcome"))
|
||||||
if client.Exit {
|
if err != nil {
|
||||||
_ = websocket.Message.Send(ws, "exit")
|
log.Error(err)
|
||||||
_ = ws.Close()
|
}
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := ""
|
f, err := os.OpenFile(fmt.Sprintf("%s/%s_%d.txt", keyDir, time.Now().Format("2006-01-02_15-04"), client.ID), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
|
||||||
err = websocket.Message.Receive(ws, &msg)
|
if err != nil {
|
||||||
if err != nil {
|
log.Error(err)
|
||||||
log.Error(err)
|
fmt.Printf("%s client %-3d crashed (couldn't open file)", time.Now().Format(TimeFormat), client.ID)
|
||||||
_ = websocket.Message.Send(ws, "exit")
|
|
||||||
_ = ws.Close()
|
|
||||||
fmt.Printf("%s client %-3d crashed (websocket error)", time.Now().Format(TimeFormat), client.ID)
|
|
||||||
client.Exit = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = f.WriteString(msg)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
_ = websocket.Message.Send(ws, "exit")
|
|
||||||
_ = ws.Close()
|
|
||||||
fmt.Printf("%s client %-3d crashed (file write error)", time.Now().Format(TimeFormat), client.ID)
|
|
||||||
client.Exit = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("%s client %-3d disconnected", time.Now().Format(TimeFormat), client.ID)
|
|
||||||
client.Exit = true
|
client.Exit = true
|
||||||
}).ServeHTTP(c.Response(), c.Request())
|
return c.NoContent(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
return nil
|
for {
|
||||||
|
if client.Exit {
|
||||||
|
_ = ws.WriteMessage(websocket.TextMessage, []byte("exit"))
|
||||||
|
_ = ws.Close()
|
||||||
|
fmt.Printf("%s client %-3d disconnected", time.Now().Format(TimeFormat), client.ID)
|
||||||
|
client.Exit = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
_, msg, err := ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
_ = ws.WriteMessage(websocket.TextMessage, []byte("exit"))
|
||||||
|
_ = ws.Close()
|
||||||
|
fmt.Printf("%s client %-3d crashed (websocket error)", time.Now().Format(TimeFormat), client.ID)
|
||||||
|
client.Exit = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = f.Write(msg)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
_ = ws.WriteMessage(websocket.TextMessage, []byte("exit"))
|
||||||
|
_ = ws.Close()
|
||||||
|
fmt.Printf("%s client %-3d crashed (file write error)", time.Now().Format(TimeFormat), client.ID)
|
||||||
|
client.Exit = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.NoContent(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user