not working concept

This commit is contained in:
potassium 2024-12-27 00:07:47 +03:00
parent b6a0896de8
commit efd993663d
8 changed files with 241 additions and 28 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
token/token.plaintext

29
decrypt_token.go Normal file
View File

@ -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)
}

30
encrypt_token.go Normal file
View File

@ -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)
}

1
encrypted Normal file
View File

@ -0,0 +1 @@
IxK3LwmQb1j9TvQ8erFnXt+uX4X2u67rHdFFVnDOv6+gTwPRt6UF1DN7vXzD3gqZQPD7aHD0zh21cv1DWDerQzMiX+GHkh2jHGE=

5
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

109
market.go
View File

@ -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)
}
}
}

90
token/token.go Normal file
View File

@ -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
}