Files
amazon-token/main.go
2019-03-20 22:51:35 -06:00

129 lines
3.2 KiB
Go

package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"sort"
"strconv"
)
var (
AmazonClient = &http.Client{}
)
type HeadersText struct {
Token string `json:"x-amz-access-token"`
Instance string `json:"x-flex-instance-id"`
UserAgent string `json:"user-agent"`
Host string `json:"Host"`
}
func main() {
accts, next := checkActs()
if len(accts) == 0 {
newLogin(path.Join("accts", "1"))
} else {
fmt.Println("Choose account number to switch to, or other option")
for key, item := range accts {
fmt.Printf("%s - %s\n", key, item)
}
fmt.Println("a - add account")
fmt.Println("r - new login to existing account")
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("Choice: ")
scanner.Scan()
choice := scanner.Text()
if choice == "A" || choice == "a" {
newLogin(path.Join("accts", fmt.Sprintf("%d", next+1)))
break
} else if choice == "R" || choice == "r" {
fmt.Println("Choose account: ")
scanner.Scan()
choice2 := scanner.Text()
newLogin(path.Join("accts", choice2))
break
} else if _, ok := accts[choice]; ok {
selectActive(path.Join("accts", choice))
break
}
}
}
}
func checkActs() (map[string]string, int) {
accts := make(map[string]string)
nums := []int{0}
os.MkdirAll("accts", os.ModePerm)
list, _ := ioutil.ReadDir("accts")
for _, item := range list {
if item.IsDir() {
num, err := strconv.Atoi(item.Name())
if err != nil {
continue
}
nums = append(nums, num)
api.Load(path.Join("accts", item.Name()))
accts[item.Name()] = api.Payload.AuthData.User.UserId
}
}
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return accts, nums[0]
}
func selectActive(acctpath string) {
os.Remove("headers.txt")
os.Remove("amazon-api.json")
os.Remove("amazon-bearer.json")
err := os.Symlink(path.Join(acctpath, "headers.txt"), "headers.txt")
os.Symlink(path.Join(acctpath, "amazon-api.json"), "amazon-api.json")
os.Symlink(path.Join(acctpath, "amazon-bearer.json"), "amazon-bearer.json")
if err != nil {
fmt.Println(err)
}
}
func newLogin(acctpath string) {
os.MkdirAll(acctpath, os.ModePerm)
api.Load(acctpath)
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("This data will not be saved. It will only be used to login")
if api.Payload.AuthData.User.UserId != "email" {
fmt.Printf("Logging into: %s", api.Payload.AuthData.User.UserId)
} else {
fmt.Print("Email: ")
scanner.Scan()
api.Payload.AuthData.User.UserId = scanner.Text()
}
resp, err := http.Get("https://token.blocksuperscript.com/" + api.Payload.AuthData.User.UserId)
if err != nil {
fmt.Println("Problems running script")
os.Exit(0)
}
if resp.StatusCode != 204 {
fmt.Println("You are not authorized to run this")
os.Exit(0)
}
api.Save(acctpath)
fmt.Print("Password: ")
scanner.Scan()
api.Payload.AuthData.User.Password = scanner.Text()
login()
output := HeadersText{
Token: bearer.Tokens.Bearer.AccessToken,
Instance: api.Headers.Instance,
UserAgent: api.Headers.Main,
Host: "flex-capacity-na.amazon.com",
}
head_file, _ := os.Create(path.Join(acctpath, "headers.txt"))
json.NewEncoder(head_file).Encode(output)
head_file.Close()
bearer.Save(TokenExpire, acctpath)
selectActive(acctpath)
}