add encryption and get page

This commit is contained in:
Aleksandr Trushkin
2024-01-29 00:09:30 +03:00
parent c814f27c54
commit 9ef5f2bbf8
8 changed files with 436 additions and 35 deletions

View File

@ -1,6 +1,8 @@
package config
type Eway struct {
Login string `toml:"login"`
Password string `toml:"password"`
SessionID string `toml:"session_id"`
SessionUser string `toml:"session_user"`
OwnerID string `toml:"owner_id"`

32
internal/crypto/cipher.go Normal file
View File

@ -0,0 +1,32 @@
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"fmt"
)
var (
someDumbKey = []byte("9530e001b619e8e98a889055f06821bb")
)
func Encrypt(plaintext string) (hexed string, err error) {
aes, err := aes.NewCipher(someDumbKey)
if err != nil {
return "", fmt.Errorf("making new cipher: %w", err)
}
gcm, err := cipher.NewGCM(aes)
if err != nil {
return "", fmt.Errorf("making new gcm: %w", err)
}
nonce := make([]byte, gcm.NonceSize())
_, err = rand.Read(nonce)
if err != nil {
return "", fmt.Errorf("generating nonce: %w", err)
}
outvalue := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
return hex.EncodeToString(outvalue), nil
}

View File

@ -0,0 +1,10 @@
//go:build !encon
// +build !encon
package crypto
import "git.loyso.art/frx/eway/internal/entity"
func Decrypt(hexedcipher string) (plaintext string, err error) {
return "", entity.SimpleError("this feature turned off")
}

View File

@ -0,0 +1,35 @@
//go:build encon
// +build encon
package crypto
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
func Decrypt(hexedcipher string) (plaintext string, err error) {
aes, err := aes.NewCipher(someDumbKey)
if err != nil {
return "", fmt.Errorf("making new cipher: %w", err)
}
gcm, err := cipher.NewGCM(aes)
if err != nil {
return "", fmt.Errorf("making new gcm: %w", err)
}
nonceSize := gcm.NonceSize()
valueDecoded, err := hex.DecodeString(hexedcipher)
if err != nil {
return "", fmt.Errorf("decoding hexed value: %w", err)
}
nonce, cipherText := valueDecoded[:nonceSize], valueDecoded[nonceSize:]
plaintextRaw, err := gcm.Open(nil, []byte(nonce), []byte(cipherText), nil)
if err != nil {
return "", fmt.Errorf("opening: %w", err)
}
return string(plaintextRaw), nil
}

View File

@ -13,6 +13,7 @@ import (
"strings"
"git.loyso.art/frx/eway/internal/config"
"git.loyso.art/frx/eway/internal/crypto"
"git.loyso.art/frx/eway/internal/entity"
"github.com/go-resty/resty/v2"
@ -36,38 +37,54 @@ type client struct {
type Config config.Eway
func New(cfg Config, log zerolog.Logger) (client, error) {
if cfg.SessionID == "" {
return client{}, entity.SimpleError("no session id provided")
}
if cfg.SessionUser == "" {
return client{}, entity.SimpleError("no session user provided")
}
cookies := []*http.Cookie{
{
Name: "session_id",
Value: cfg.SessionID,
Domain: "eway.elevel.ru",
HttpOnly: true,
},
{
Name: "session_user",
Value: cfg.SessionUser,
Domain: "eway.elevel.ru",
HttpOnly: true,
},
}
func New(ctx context.Context, cfg Config, log zerolog.Logger) (client, error) {
httpclient := resty.New().
SetDebug(cfg.Debug).
SetCookies(cookies).
// SetCookies(cookies).
SetBaseURL("https://eway.elevel.ru/api")
return client{
c := client{
http: httpclient,
log: log.With().Str("client", "eway").Logger(),
}, nil
}
if cfg.SessionID == "" || cfg.SessionUser == "" {
if cfg.Login == "" || cfg.Password == "" {
return client{}, entity.SimpleError("no auth method provided")
}
decryptedPassword, err := crypto.Decrypt(cfg.Password)
if err != nil {
return client{}, fmt.Errorf("decrypting password: %w", err)
}
err = c.login(ctx, cfg.Login, decryptedPassword)
if err != nil {
return client{}, err
}
log.Info().Msg("login successful")
} else if cfg.SessionID != "" && cfg.SessionUser != "" {
cookies := []*http.Cookie{
{
Name: "session_id",
Value: cfg.SessionID,
Domain: "eway.elevel.ru",
HttpOnly: true,
},
{
Name: "session_user",
Value: cfg.SessionUser,
Domain: "eway.elevel.ru",
HttpOnly: true,
},
}
c.http.SetCookies(cookies)
} else {
return client{}, entity.SimpleError("bad configuration: either session_id and session_user should be set or login and password")
}
return c, nil
}
type GetGoodsNewParams struct {
@ -181,8 +198,6 @@ func (c client) GetGoodsRemnants(
return nil, fmt.Errorf("reading raw body: %w", err)
}
c.log.Debug().RawJSON("response", data).Msg("body prepared")
out = make(entity.MappedGoodsRemnants, len(productIDs))
err = json.NewDecoder(bytes.NewReader(data)).Decode(&out)
if err != nil {
@ -234,3 +249,23 @@ func (c client) GetGoodsNew(
return mapResponseByOrder(response), response.RecordsTotal, nil
}
func (c client) login(ctx context.Context, user, pass string) error {
resp, err := c.http.R().
SetDoNotParseResponse(true).
SetFormData(map[string]string{
"username": user,
"password": pass,
}).Post("https://eway.elevel.ru/")
if err != nil {
return fmt.Errorf("sending request: %w", err)
}
if resp.IsError() {
zerolog.Ctx(ctx).Warn().Int("code", resp.StatusCode()).Msg("bad response")
return entity.SimpleError("request was not successful")
}
return nil
}