mirror of
https://github.com/fumiama/tienyik.git
synced 2026-06-28 06:40:30 +08:00
feat: add more apis
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
package hcli
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
base14 "github.com/fumiama/go-base16384"
|
||||
"github.com/fumiama/tienyik/internal/log"
|
||||
)
|
||||
|
||||
var eps = base14.DecodeString("栝啇俌蠯姙呗宬籣欞敖蚹煮岎冃勀紀㴆")
|
||||
|
||||
func ep(p string) string {
|
||||
sb := &strings.Builder{}
|
||||
sb.WriteString(eps)
|
||||
sb.WriteString(p)
|
||||
log.Debugln("ep wraps:", sb)
|
||||
return sb.String()
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package hcli
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
base14 "github.com/fumiama/go-base16384"
|
||||
)
|
||||
|
||||
func setCommonHeaders(req *http.Request) {
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Pragma", "no-cache")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Origin", base14.DecodeString("栝啇俌蠯姜吲融艹歛烦宸㴅"))
|
||||
req.Header.Set("Referer", base14.DecodeString("栝啇俌蠯姜吲融艹歛烦宸紀㴆"))
|
||||
req.Header.Set(
|
||||
"User-Agent",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
|
||||
)
|
||||
}
|
||||
|
||||
func Get(path string) (resp *http.Response, err error) {
|
||||
req, err := http.NewRequest(http.MethodGet, ep(path), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setCommonHeaders(req)
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func Post(path string, body io.Reader) (resp *http.Response, err error) {
|
||||
req, err := http.NewRequest(http.MethodPost, ep(path), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setCommonHeaders(req)
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func Put(path string, body io.Reader) (resp *http.Response, err error) {
|
||||
req, err := http.NewRequest(http.MethodPut, ep(path), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setCommonHeaders(req)
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func Delete(path string) (resp *http.Response, err error) {
|
||||
req, err := http.NewRequest(http.MethodDelete, ep(path), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setCommonHeaders(req)
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func Patch(path string, body io.Reader) (resp *http.Response, err error) {
|
||||
req, err := http.NewRequest(http.MethodPatch, ep(path), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setCommonHeaders(req)
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
35
internal/horm/req.go
Normal file
35
internal/horm/req.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package horm
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"reflect"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
"github.com/fumiama/tienyik/internal/textio"
|
||||
)
|
||||
|
||||
func Marshal(tya *tienyik.AES, x any) []byte {
|
||||
q := url.Values{}
|
||||
v := reflect.ValueOf(x).Elem()
|
||||
t := v.Type()
|
||||
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
fieldType := t.Field(i)
|
||||
|
||||
formTag := fieldType.Tag.Get("form")
|
||||
if formTag == "" || formTag == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
if field.Kind() == reflect.String {
|
||||
q.Set(formTag, field.String())
|
||||
}
|
||||
}
|
||||
|
||||
s := q.Encode()
|
||||
if tya != nil {
|
||||
return tya.Encrypt(textio.StringToBytes(s))
|
||||
}
|
||||
return textio.StringToBytes(s)
|
||||
}
|
||||
@@ -2,12 +2,18 @@ package hson
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
"github.com/fumiama/tienyik/internal/log"
|
||||
"github.com/fumiama/tienyik/internal/op"
|
||||
)
|
||||
|
||||
type reqbody struct {
|
||||
EData string `json:"edata"`
|
||||
}
|
||||
|
||||
func Marshal(tya *tienyik.AES, v any) []byte {
|
||||
w := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||
err := json.NewEncoder(w).Encode(v)
|
||||
@@ -16,7 +22,9 @@ func Marshal(tya *tienyik.AES, v any) []byte {
|
||||
}
|
||||
log.Debugln("Marshal JSON:", w.String())
|
||||
if tya != nil {
|
||||
return tya.Encrypt(w.Bytes())
|
||||
return tya.Encrypt(op.Must(json.Marshal(&reqbody{
|
||||
EData: base64.StdEncoding.EncodeToString(w.Bytes()),
|
||||
})))
|
||||
}
|
||||
return w.Bytes()
|
||||
}
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
package textio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
)
|
||||
|
||||
func API() string {
|
||||
@@ -42,29 +38,3 @@ func API() string {
|
||||
sb.WriteString(p[1:])
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func EUrlParams(tya *tienyik.AES, params url.Values) string {
|
||||
return url.Values{
|
||||
FuncName(1, true): {BytesToString(tya.Encrypt(
|
||||
StringToBytes(params.Encode()),
|
||||
))},
|
||||
}.Encode()
|
||||
}
|
||||
|
||||
func ParseQuery(tya *tienyik.AES, eparams string) (url.Values, error) {
|
||||
q, err := url.ParseQuery(eparams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(q) != 1 {
|
||||
return nil, errors.New("len(q) must be 1")
|
||||
}
|
||||
for _, v := range q {
|
||||
dec, err := tya.Decrypt(StringToBytes(v[0]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return url.ParseQuery(BytesToString(dec))
|
||||
}
|
||||
panic("unexpected")
|
||||
}
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
package textio
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
)
|
||||
|
||||
func TestEUrlParams(t *testing.T) {
|
||||
const aesplain = "moduleCode=DESKTOP_MSGCENTER"
|
||||
var (
|
||||
rawkey = []uint32{
|
||||
2004378729, 1936745065, 1933079672, 1970627951,
|
||||
842425958, 1932686949, 1903374648, 1936290669,
|
||||
}
|
||||
key [32]byte
|
||||
)
|
||||
for i, k := range rawkey {
|
||||
binary.BigEndian.PutUint32(key[i*4:(i+1)*4], k)
|
||||
}
|
||||
t.Log(string(key[:])) // wxdispbis8txuueo26ffs2veqs18sism
|
||||
tya := tienyik.NewAES(key[:])
|
||||
params := EUrlParams(&tya, url.Values{
|
||||
"moduleCode": {"DESKTOP_MSGCENTER"},
|
||||
})
|
||||
q, err := ParseQuery(&tya, params)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for k, v := range q {
|
||||
plainValue := k + "=" + v[0]
|
||||
if plainValue != aesplain {
|
||||
t.Fatal("expect", aesplain, "got", plainValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEUrlParamsMultiple(t *testing.T) {
|
||||
var (
|
||||
rawkey = []uint32{
|
||||
2004378729, 1936745065, 1933079672, 1970627951,
|
||||
842425958, 1932686949, 1903374648, 1936290669,
|
||||
}
|
||||
key [32]byte
|
||||
)
|
||||
for i, k := range rawkey {
|
||||
binary.BigEndian.PutUint32(key[i*4:(i+1)*4], k)
|
||||
}
|
||||
tya := tienyik.NewAES(key[:])
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params url.Values
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
name: "single parameter",
|
||||
params: url.Values{
|
||||
"userId": {"12345"},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"userId": "12345",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple parameters",
|
||||
params: url.Values{
|
||||
"userId": {"12345"},
|
||||
"userName": {"testUser"},
|
||||
"status": {"active"},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"userId": "12345",
|
||||
"userName": "testUser",
|
||||
"status": "active",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "special characters",
|
||||
params: url.Values{
|
||||
"email": {"test@example.com"},
|
||||
"message": {"Hello World!"},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"email": "test@example.com",
|
||||
"message": "Hello World!",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "chinese characters",
|
||||
params: url.Values{
|
||||
"name": {"张三"},
|
||||
"city": {"北京"},
|
||||
},
|
||||
expected: map[string]string{
|
||||
"name": "张三",
|
||||
"city": "北京",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
params := EUrlParams(&tya, tc.params)
|
||||
q, err := ParseQuery(&tya, params)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for key, expectedValue := range tc.expected {
|
||||
if vals, ok := q[key]; ok && len(vals) > 0 {
|
||||
if vals[0] != expectedValue {
|
||||
t.Fatalf("key %s: expect %s, got %s", key, expectedValue, vals[0])
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("key %s not found in query", key)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
6
internal/textio/http.go
Normal file
6
internal/textio/http.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package textio
|
||||
|
||||
const (
|
||||
ContenTypeForm = "application/x-www-form-urlencoded"
|
||||
ContenTypeJSON = "application/json"
|
||||
)
|
||||
Reference in New Issue
Block a user