package main

import (
	"be-vpn/internal/dto"
	"bytes"
	"encoding/json"
	"github.com/tidwall/gjson"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	file, err := os.Open("/home/ubuntu/client.ovpn")
	if err != nil {
		log.Fatalf("can not open file, err: %+v", err)
	}
	buf := bytes.Buffer{}
	if _, err := io.Copy(&buf, file); err != nil {
		log.Fatalf("can not read file, err: %+v", err)
	}

	resp, err := http.Get("https://ipinfo.ipidea.io")
	if err != nil {
		log.Fatalf("can not get resp, err: %+v", err)
	}
	bs, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("can not read resp, err: %+v", err)
	}
	countryCode := gjson.Get(string(bs), "country_code").String()
	ip := gjson.Get(string(bs), "ip").String()
	city := gjson.Get(string(bs), "city").String()
	log.Printf("ipinfo: %s", string(bs))

	request := dto.RegisterRequest{Ip: ip, CountryCode: countryCode, CountryName: city, Secret: buf.String()}
	body, err := json.Marshal(request)
	if err != nil {
		log.Printf("err: %+v", err)
	}

	for true {
		// send request
		reader := bytes.NewBuffer(body)
		resp, err := http.Post("http://v.starttransfernow.com/register", "application/json", reader)
		if err != nil {
			log.Printf("err: %+v", err)
			continue
		}
		respBody, err := io.ReadAll(resp.Body)
		if err != nil {
			log.Printf("err: %+v", err)
		}
		log.Printf("resp: %s", string(respBody))
		time.Sleep(1 * time.Second)
	}
}
