# Go

package main

import (
   "crypto/aes"
   "crypto/cipher"
   "crypto/rand"
   "encoding/base64"
   "encoding/json"
   "fmt"
   "io"
   "io/ioutil"
   mrand "math/rand"
   "net/http"
   "strings"
   "time"
)

const (
   url    = "https://ipdata.yazx.com/api/ip/v2/check/"
   snuser = "XXX"
   snkey  = "XXX"
)

func AESDecrypt(ciphertxt string, keystr string) string {
   // prepare cipher text
   cipherbyte, _ := base64.StdEncoding.DecodeString(ciphertxt)
   // prepare the key
   block, _ := aes.NewCipher([]byte(keystr))

   if len(cipherbyte) < aes.BlockSize {
      return ""
   }

   // split iv and ciphertext
   iv := cipherbyte[:aes.BlockSize]
   cipherbyte = cipherbyte[aes.BlockSize:]
   stream := cipher.NewCFBDecrypter(block, iv)

   // XORKeyStream can work in-place if the two arguments are the same.
   stream.XORKeyStream(cipherbyte, cipherbyte)
   return string(cipherbyte)
}

func AESEncrypt(plaintxt string, keystr string) string {
   block, _ := aes.NewCipher([]byte(keystr))
   ciphertext := make([]byte, aes.BlockSize+len(plaintxt))
   iv := ciphertext[:aes.BlockSize]
   io.ReadFull(rand.Reader, iv)

   randobj := mrand.New(mrand.NewSource(time.Now().UnixNano()))
   randobj.Read(iv)

   stream := cipher.NewCFBEncrypter(block, iv)
   stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(plaintxt))
   return base64.StdEncoding.EncodeToString([]byte(ciphertext))
}

func TestGetStatus(Data string) {
   encryptData := AESEncrypt(Data, snkey)

   payload := strings.NewReader(
      "{\"snuser\": \"" + snuser + "\", \"data\": \"" + encryptData + "\"}")
   req, _ := http.NewRequest("POST", url, payload)

   req.Header.Add("content-type", "application/json")
   req.Header.Add("cache-control", "no-cache")

   res, _ := http.DefaultClient.Do(req)

   defer res.Body.Close()
   body, _ := ioutil.ReadAll(res.Body)

   var response map[string]interface{}
   if err := json.Unmarshal(body, &response); err != nil {
      panic(err)
   }
   
   if response["status"].(float64) != 200 {
      fmt.Println("get status failed")
   } else {
      fmt.Println("get status success =>>>")
      fmt.Println(AESDecrypt(response["data"].(string), snkey))
   }
}

func main() {
   ipData := "{\"ip\": \"122.241.179.74\", \"attack_time\": 1663320929}"
   TestGetStatus(ipData)
}
Last Updated: 1/23/2024, 4:55:00 PM