register.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package task
  2. import (
  3. "be-vpn/internal/dto"
  4. "bytes"
  5. "encoding/json"
  6. "github.com/tidwall/gjson"
  7. "io"
  8. "log"
  9. "net/http"
  10. )
  11. func Register() {
  12. resp, err := http.Get("https://ipinfo.ipidea.io")
  13. if err != nil {
  14. log.Fatalf("can not get resp, err: %+v", err)
  15. }
  16. bs, err := io.ReadAll(resp.Body)
  17. if err != nil {
  18. log.Printf("can not read resp, err: %+v", err)
  19. return
  20. }
  21. countryCode := gjson.Get(string(bs), "country_code").String()
  22. ip := gjson.Get(string(bs), "ip").String()
  23. city := gjson.Get(string(bs), "city").String()
  24. log.Printf("ipinfo: %s", string(bs))
  25. request := dto.RegisterRequest{Ip: ip, CountryCode: countryCode, CountryName: city, City: city}
  26. body, err := json.Marshal(request)
  27. if err != nil {
  28. log.Printf("err: %+v", err)
  29. return
  30. }
  31. // send request
  32. reader := bytes.NewBuffer(body)
  33. resp, err = http.Post("http://v.starttransfernow.com/register", "application/json", reader)
  34. if err != nil {
  35. log.Printf("err: %+v", err)
  36. return
  37. }
  38. respBody, err := io.ReadAll(resp.Body)
  39. if err != nil {
  40. log.Printf("err: %+v", err)
  41. return
  42. }
  43. log.Printf("resp: %s", string(respBody))
  44. }