From efd993663d062f811e5992f32b22109a10d3fb14 Mon Sep 17 00:00:00 2001 From: potassium Date: Fri, 27 Dec 2024 00:07:47 +0300 Subject: [PATCH] not working concept --- .gitignore | 1 + decrypt_token.go | 29 +++++++++++++ encrypt_token.go | 30 +++++++++++++ encrypted | 1 + go.mod | 5 ++- go.sum | 4 ++ market.go | 109 +++++++++++++++++++++++++++++++++++------------ token/token.go | 90 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 241 insertions(+), 28 deletions(-) create mode 100644 .gitignore create mode 100644 decrypt_token.go create mode 100644 encrypt_token.go create mode 100644 encrypted create mode 100644 token/token.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98dc036 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +token/token.plaintext \ No newline at end of file diff --git a/decrypt_token.go b/decrypt_token.go new file mode 100644 index 0000000..9246abd --- /dev/null +++ b/decrypt_token.go @@ -0,0 +1,29 @@ +// go:build token +package main + +import ( + "crypto/sha256" + "fmt" + "git.niplace.ru/XoxJlopeZi4BB/paxpamir/token" + "io" + "os" +) + +func Must[T any](v T, err error) T { + if err != nil { + panic(err) + } + return v +} + +func main() { + hash := sha256.Sum256(token.Password()) + s, err := token.Decrypt( + string(Must(io.ReadAll(Must(os.Open("encrypted"))))), + hash[:], + ) + if err != nil { + panic(err) + } + fmt.Println(s) +} diff --git a/encrypt_token.go b/encrypt_token.go new file mode 100644 index 0000000..1cced93 --- /dev/null +++ b/encrypt_token.go @@ -0,0 +1,30 @@ +// go:build token +package main + +import ( + "crypto/sha256" + "fmt" + "io" + "os" + + "git.niplace.ru/XoxJlopeZi4BB/paxpamir/token" +) + +func Must[T any](v T, err error) T { + if err != nil { + panic(err) + } + return v +} + +func main() { + hash := sha256.Sum256(token.Password()) + s, err := token.Encrypt( + string(Must(io.ReadAll(Must(os.Open("token/token.plaintext"))))), + hash[:], + ) + if err != nil { + panic(err) + } + fmt.Println(s) +} diff --git a/encrypted b/encrypted new file mode 100644 index 0000000..d5d1da8 --- /dev/null +++ b/encrypted @@ -0,0 +1 @@ +IxK3LwmQb1j9TvQ8erFnXt+uX4X2u67rHdFFVnDOv6+gTwPRt6UF1DN7vXzD3gqZQPD7aHD0zh21cv1DWDerQzMiX+GHkh2jHGE= diff --git a/go.mod b/go.mod index 188fb68..34f3b1f 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,11 @@ -module paxpamir +module git.niplace.ru/XoxJlopeZi4BB/paxpamir go 1.23.1 require ( git.niplace.ru/XoxJlopeZi4BB/imageutils v0.0.0-20241224134015-2709034fc322 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + golang.org/x/term v0.27.0 ) + +require golang.org/x/sys v0.28.0 // indirect diff --git a/go.sum b/go.sum index ccbdedf..5050131 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,7 @@ git.niplace.ru/XoxJlopeZi4BB/imageutils v0.0.0-20241224134015-2709034fc322 h1:je git.niplace.ru/XoxJlopeZi4BB/imageutils v0.0.0-20241224134015-2709034fc322/go.mod h1:QieEgSGk9ZYISDv5XyMMktQXi/2qlTmdHuxTlt1Xfq8= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q= +golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= diff --git a/market.go b/market.go index d302938..f58f7f7 100644 --- a/market.go +++ b/market.go @@ -1,19 +1,28 @@ -package market +package main import ( "bytes" "fmt" "image" - "io" + _ "image/jpeg" + "image/png" "log" "math/rand" "net/http" "time" "git.niplace.ru/XoxJlopeZi4BB/imageutils" + "git.niplace.ru/XoxJlopeZi4BB/paxpamir/token" tg "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) +const ( + CardNum = 116 + DominanceCheck = 4 + EventCards = 12 + SpecialCards = DominanceCheck + EventCards +) + func shuffle[S ~[]E, E any](a S) { rand.Seed(time.Now().UnixNano()) for i := len(a) - 1; i > 0; i-- { @@ -35,7 +44,7 @@ var ShuffledCardPool []int = func(n int) []int { } shuffle(arr) return arr -}(116) +}(CardNum) var RupeesOnCards []int = func() (ns []int) { rand.Seed(time.Now().UnixNano()) @@ -45,8 +54,8 @@ var RupeesOnCards []int = func() (ns []int) { return }() -var LinkFormat string = "https://rally-the-troops.com/" + - "pax-pamir/cards/card_%d.jpg" +var LinkFormat string = "https://rally-the-troops.com" + + "/pax-pamir/cards/card_%d.jpg" type Button struct { Text string `json:"text"` @@ -75,47 +84,78 @@ func Buttons(b []Button) tg.InlineKeyboardMarkup { ) } +func Must[T any](v T, err error) T { + if err != nil { + log.Println("ERROR:", err) + } + return v +} + +func GetImage(link string) (image.Image, error) { + resp, err := http.Get(link) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + img, _, err := image.Decode(resp.Body) + if img == nil { + log.Fatal("img is nil") + } + if err != nil { + return nil, err + } + return img, nil +} + func HandleCommand(update tg.Update, bot *tg.BotAPI) { maxCost := 5 + var err error for i := 0; i <= maxCost; i++ { - var ( - msg tg.PhotoConfig - buf *bytes.Buffer - img image.Image + var buf *bytes.Buffer + var img = imageutils.Concat( + Must(GetImage(fmt.Sprintf(LinkFormat, + takeOne(ShuffledCardPool)))), + Must(GetImage(fmt.Sprintf(LinkFormat, + takeOne(ShuffledCardPool)))), + imageutils.Right, ) - - for j := 0; j < 2; j++ { - cardNum := ShuffledCardPool[0] - ShuffledCardPool = ShuffledCardPool[1:] - resp, err := http.Get(fmt.Sprintf(LinkFormat, - cardNum)) - if err != nil { - log.Println("http GET error:", err) - } - defer resp.Body.Close() - - io.Copy(buf, resp.Body) + if img == nil { + log.Fatal("pizdec") } - msg = tg.NewPhoto( - update.Message.Chat.ID, - tg.FileBytes(buf.Bytes()), + + err = png.Encode( + buf, + img, ) + if err != nil { + log.Println("error on decode:", err) + } + buttons := []Button{} for j := 0; j < 2; j++ { text := func() (s string) { s = fmt.Sprintf("buy for %d", i) - if RupeesOnCard[j] >= i { + if RupeesOnCards[j] >= i { s += fmt.Sprintf(", get 1") } return }() - buttons := append(buttons, Button{Text: text}) + buttons = append(buttons, Button{Text: text}) } - _, err := bot.Send(msg) // get response message + + msg := tg.NewPhoto( + update.Message.Chat.ID, + tg.FileBytes{Name: "cards", Bytes: buf.Bytes()}, + ) + msg.ReplyMarkup = Buttons(buttons) + + responseMsg, err := bot.Send(msg) // get response message if err != nil { log.Println("failed to send message on command:", err) } + log.Print("message sent. id:", responseMsg.MessageID) } } @@ -150,3 +190,18 @@ func ConsumeCallback(update tg.Update, bot *tg.BotAPI) { } log.Print("request sent. response:", req.Description) } + +func main() { + bot := Must(tg.NewBotAPI(token.Token())) + { + log.Printf("authorized on account %s", bot.Self.UserName) + u := tg.NewUpdate(0) + u.Timeout = 60 + + updates := bot.GetUpdatesChan(u) + + for update := range updates { + HandleUpdate(update, bot) + } + } +} diff --git a/token/token.go b/token/token.go new file mode 100644 index 0000000..b379ff3 --- /dev/null +++ b/token/token.go @@ -0,0 +1,90 @@ +package token + +import ( + "crypto/aes" + "crypto/cipher" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "fmt" + "io" + "syscall" + "os" + + "golang.org/x/term" +) + +func Must[T any](v T, err error) T { + if err != nil { + panic(err) + } + return v +} + +func Token() string { + hash := sha256.Sum256(Password()) + v, err := Decrypt(string(Must(io.ReadAll(Must(os.Open("encrypted"))))), hash[:]) + if err != nil { + panic(err) + } + return v +} + +func Password() []byte { + print("Password: ") + p, err := term.ReadPassword(int(syscall.Stdin)) + if err != nil { + panic(err) + } + return p +} + +func Encrypt(plainText string, key []byte) (string, error) { + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + gcm, err := cipher.NewGCM(block) + if err != nil { + return "", err + } + + nonce := make([]byte, gcm.NonceSize()) + if _, err := io.ReadFull(rand.Reader, nonce); err != nil { + return "", err + } + + ciphertext := gcm.Seal(nonce, nonce, []byte(plainText), nil) + return base64.StdEncoding.EncodeToString(ciphertext), nil +} + +func Decrypt(cipherText string, key []byte) (string, error) { + ciphertext, err := base64.StdEncoding.DecodeString(cipherText) + if err != nil { + return "", err + } + + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + gcm, err := cipher.NewGCM(block) + if err != nil { + return "", err + } + + nonceSize := gcm.NonceSize() + if len(ciphertext) < nonceSize { + return "", fmt.Errorf("ciphertext too short") + } + + nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] + plainText, err := gcm.Open(nil, nonce, ciphertext, nil) + if err != nil { + return "", err + } + + return string(plainText), nil +}