Compare commits

...

2 Commits

Author SHA1 Message Date
daru
7185544e03 Prime hotfix 2022-07-26 20:48:17 +02:00
daru
7fe4181c72 prime fun + user stash 2022-07-26 20:33:11 +02:00
8 changed files with 120 additions and 27 deletions

View File

@@ -295,3 +295,8 @@ _[]Appointment_ (POST/DELETE)
}
]
```
### User data
| Method | Route | Description | Response | Parameter | Header | POST-Body |
| - | - | - | - | - | - | - |
| GET | /api/userdata/{user}/{key} | User data holen | | {user} = MAL username; {key} = Key der data | X-HUSO-AUTH | Plaintext(binary) data |
| PUT | /api/userdata/{user}/{key} | User data setzen | Plaintext(binary) data | {user} = MAL username; {key} = Key der data | X-HUSO-AUTH | |

10
go.mod
View File

@@ -5,26 +5,26 @@ go 1.18
require (
github.com/allegro/bigcache/v3 v3.0.2
github.com/bwmarrin/discordgo v0.25.0
github.com/fasthttp/router v1.4.10
github.com/fasthttp/router v1.4.11
github.com/go-sql-driver/mysql v1.6.0
github.com/gookit/color v1.5.1
github.com/klauspost/compress v1.15.9
github.com/valyala/fasthttp v1.38.0
github.com/valyala/quicktemplate v1.7.0
github.com/xujiajun/nutsdb v0.9.0
golang.org/x/time v0.0.0-20220609170525-579cf78fd858
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9
)
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/klauspost/compress v1.15.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/xujiajun/mmap-go v1.0.1 // indirect
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
)

21
go.sum
View File

@@ -11,8 +11,8 @@ github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fasthttp/router v1.4.10 h1:C8z6K1pTqhLjSv97/qCY9tZiiPT8JuFwDoO9E2HJFWQ=
github.com/fasthttp/router v1.4.10/go.mod h1:FGSUOg9SQ/tU864SfD23kG/HwfD0akXqOqhTQ27gTFQ=
github.com/fasthttp/router v1.4.11 h1:99BvgVxeS2oOZBHnKr/okpdPq1jkn8WvYA2trh/71LY=
github.com/fasthttp/router v1.4.11/go.mod h1:luEEYkGBSAmYyPaMeIUGNgqY+FdHHYDOK9Kivaw7aNo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
@@ -25,8 +25,8 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.8 h1:JahtItbkWjf2jzm/T+qgMxkP9EMHsqEUA6vCMGmXvhA=
github.com/klauspost/compress v1.15.8/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -40,7 +40,6 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.30.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
github.com/valyala/fasthttp v1.37.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/fasthttp v1.38.0 h1:yTjSSNjuDi2PPvXY2836bIwLmiTS2T4T9p1coQshpco=
github.com/valyala/fasthttp v1.38.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/quicktemplate v1.7.0 h1:LUPTJmlVcb46OOUY3IeD9DojFpAVbsG+5WFTcjMJzCM=
@@ -58,8 +57,8 @@ github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b/go.mod h1:AZd87GYJl
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
@@ -73,15 +72,15 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 h1:Dpdu/EMxGMFgq0CeYMh4fazTD2vtlZRYE7wyynxJb9U=
golang.org/x/time v0.0.0-20220609170525-579cf78fd858/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 h1:ftMN5LMiBFjbzleLqtoBZk7KdJwhuybIU+FckUHgoyQ=
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -20,7 +20,7 @@ import (
)
const (
husoVersion = "1.4"
husoVersion = "1.5"
registerSecret = "綾波レイ"
seasonStrJikan = "seasons/"
userApiJikan = "users/"
@@ -37,6 +37,7 @@ const (
bucketMedia = "media"
bucketAppoint = "appoint"
bucketChat = "chat"
bucketStash = "stash"
AppointSplit = "§"
chatLength = 10101
)

View File

@@ -67,13 +67,6 @@ type WatchUser struct {
Updated time.Time `json:"updated"`
}
type UserData struct {
Username string `json:"username"`
MalID int64 `json:"malId"`
DiscordID int64 `json:"discordId"`
Secret string `json:"secret"`
}
type User struct {
MalID int64 `json:"id"`
Username string `json:"username"`
@@ -86,6 +79,13 @@ type User struct {
Joined time.Time `json:"joined"`
}
type UserData struct {
Username string `json:"username"`
MalID int64 `json:"malId"`
DiscordID int64 `json:"discordId"`
Secret string `json:"secret"`
}
type RegisterData struct {
Username string `json:"username"`
MalID int64 `json:"malId"`

55
ober.go
View File

@@ -28,6 +28,7 @@ func RunWebserv() {
r.GET("/api/chat/{id}", Headers(ChatGet))
r.GET("/api/log", Headers(LogGet))
r.GET("/api/user/{user?}", Headers(UserGet))
r.GET("/api/userdata/{user}/{data}", Headers(UserStashGet))
r.GET("/api/watch/{user?}", Headers(WatchGet))
r.GET("/api/watchext/{user?}", Headers(WatchExtendedGet))
r.POST("/api/appointment/{user}", Headers(AppointmentPost))
@@ -35,6 +36,7 @@ func RunWebserv() {
r.POST("/api/register", Headers(Register))
r.POST("/api/watch/{user}", Headers(WatchPost))
r.PATCH("/api/register", Headers(RegisterUpdate))
r.PUT("/api/userdata/{user}/{data}", Headers(UserStashPut))
r.DELETE("/api/appointment/{user}", Headers(AppointmentDelete))
r.DELETE("/api/register", Headers(UnRegister))
r.DELETE("/api/watch/{user}", Headers(WatchDelete))
@@ -327,6 +329,10 @@ func UserGet(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
}
func UserStashGet(ctx *fasthttp.RequestCtx) {
processUserStashReq(ctx, false)
}
func WatchGet(ctx *fasthttp.RequestCtx) {
animeUsers, err := watchGetLogic(ctx)
if err != nil {
@@ -585,6 +591,10 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
processUpdateReq(ctx, true)
}
func UserStashPut(ctx *fasthttp.RequestCtx) {
processUserStashReq(ctx, true)
}
func AppointmentDelete(ctx *fasthttp.RequestCtx) {
processUpdateAppointmentReq(ctx, false)
}
@@ -875,6 +885,51 @@ func processUpdateAppointmentReq(ctx *fasthttp.RequestCtx, update bool) {
ctx.SetContentType("application/json; charset=utf-8")
}
func processUserStashReq(ctx *fasthttp.RequestCtx, update bool) {
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
dataPath := fmt.Sprintf("%s", ctx.UserValue("data"))
if auth == nil || string(auth) == "" || dataPath == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
username := fmt.Sprintf("%s", ctx.UserValue("user"))
legit, _ := GheddoAuth(username, string(auth))
if !legit {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
return
}
if update {
compressed, err := CompressZstd(ctx.PostBody())
if err != nil {
addErrorToCtx(ctx, err)
return
}
err = DbSave(bucketStash, fmt.Sprintf("%s.%s", username, dataPath), compressed)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetStatusCode(fasthttp.StatusAccepted)
} else {
compressed, err := DbRead(bucketStash, []byte(fmt.Sprintf("%s.%s", username, dataPath)))
if err != nil {
addErrorToCtx(ctx, err)
return
}
decompressed, err := DecompressZstd(compressed)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetBody(decompressed)
ctx.SetStatusCode(fasthttp.StatusOK)
}
}
func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error {
_, err := ctx.Write(bytes)
if err != nil {

View File

@@ -12,7 +12,7 @@ import (
)
func Arbeiten() {
for range time.Tick(time.Hour) {
for range time.Tick(29 * time.Minute) {
Arbeit()
}
}
@@ -28,7 +28,7 @@ func Arbeit() {
} else {
cleared := 0
for _, a := range appoints {
if a.Time.Add(time.Hour * 22).Before(time.Now()) {
if a.Time.Add(22 * time.Hour).Before(time.Now()) {
// appointment expired
keyBytes := Int64AndDateToBytes(a.Anime, a.Time)
err = DbDelete(bucketAppoint, string(keyBytes))
@@ -38,7 +38,7 @@ func Arbeit() {
} else {
cleared++
}
} else if time.Now().Before(a.Time) && time.Now().Add(time.Hour).After(a.Time) {
} else if time.Now().Add(4*time.Minute).Before(a.Time) && time.Now().Add(33*time.Minute).After(a.Time) {
// This has not happened and is happening soon
AnnounceAppointmentSoon(&a)
}

View File

@@ -1,12 +1,16 @@
package main
import (
"bytes"
"crypto/sha512"
"encoding/binary"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/klauspost/compress/zstd"
)
func Sauce(malid int64, username string) string {
@@ -43,3 +47,32 @@ func BytesToInt64AndDate(bytes []byte) (int64, time.Time, error) {
func Int64AndDateToBytes(num int64, appoint time.Time) []byte {
return []byte(fmt.Sprintf("%d%s%s", num, AppointSplit, appoint.Format(time.RFC3339)))
}
func CompressZstd(src []byte) ([]byte, error) {
var buf bytes.Buffer
encoder, err := zstd.NewWriter(&buf)
if err != nil {
return nil, err
}
reader := bytes.NewReader(src)
_, err = io.Copy(encoder, reader)
if err != nil {
encoder.Close()
return nil, err
}
err = encoder.Close()
return buf.Bytes(), err
}
func DecompressZstd(src []byte) ([]byte, error) {
reader := bytes.NewReader(src)
decoder, err := zstd.NewReader(reader)
if err != nil {
return nil, err
}
defer decoder.Close()
var buf bytes.Buffer
_, err = io.Copy(&buf, decoder)
return buf.Bytes(), err
}