2023-10-18 19:56:55 +08:00
|
|
|
// package main
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// import (
|
|
|
|
// "bytes"
|
|
|
|
// "encoding/json"
|
|
|
|
// "fmt"
|
|
|
|
// "io/ioutil"
|
|
|
|
// "net/http"
|
|
|
|
// "os"
|
|
|
|
// "time"
|
|
|
|
// )
|
2023-10-18 17:51:36 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// const (
|
|
|
|
// AdminUser = "admin"
|
2023-10-18 21:44:24 +08:00
|
|
|
// EndpointURL = "http://localhost:8888/api/endpoints"
|
|
|
|
// AuthURL = "http://localhost:8888/api/auth"
|
|
|
|
// CredentialLoc = "/root/credential"
|
2023-10-18 19:56:55 +08:00
|
|
|
// )
|
2023-10-18 17:51:36 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// type Endpoint struct {
|
|
|
|
// Name string `json:"Name"`
|
|
|
|
// URL string `json:"URL"`
|
|
|
|
// }
|
2023-10-18 17:51:36 +08:00
|
|
|
|
2023-10-18 21:44:24 +08:00
|
|
|
// type AuthResponse struct {
|
|
|
|
// Jwt string `json:"jwt"`
|
|
|
|
// }
|
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// func main() {
|
|
|
|
// var password string
|
|
|
|
|
|
|
|
// for {
|
|
|
|
// if _, err := os.Stat(CredentialLoc); os.IsNotExist(err) {
|
|
|
|
// fmt.Printf("%s does not exist, waiting for 3 seconds...\n", CredentialLoc)
|
|
|
|
// time.Sleep(3 * time.Second)
|
|
|
|
// } else {
|
|
|
|
// fmt.Printf("%s exists, proceeding...\n", CredentialLoc)
|
|
|
|
// data, err := ioutil.ReadFile(CredentialLoc)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to read file:", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// password = string(data)
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
2023-10-18 17:51:36 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// client := &http.Client{}
|
2023-10-18 21:44:24 +08:00
|
|
|
// req, err := http.NewRequest("GET", AuthURL, nil)
|
2023-10-18 19:56:55 +08:00
|
|
|
// req.SetBasicAuth(AdminUser, password)
|
|
|
|
// resp, err := client.Do(req)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to make request:", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// defer resp.Body.Close()
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to read response:", err)
|
|
|
|
// return
|
|
|
|
// }
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 21:44:24 +08:00
|
|
|
// fmt.Printf("Received body: %s\n", string(body))
|
|
|
|
|
|
|
|
// var authResponse AuthResponse
|
|
|
|
// err = json.Unmarshal(body, &authResponse)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to parse JSON:", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fmt.Printf("Received JWT: %s\n", authResponse.Jwt)
|
|
|
|
|
|
|
|
// req, err = http.NewRequest("GET", EndpointURL, nil)
|
|
|
|
// req.Header.Set("Authorization", "Bearer "+authResponse.Jwt)
|
|
|
|
// resp, err = client.Do(req)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to make request:", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// defer resp.Body.Close()
|
|
|
|
|
|
|
|
// body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to read response:", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// var endpoints []Endpoint
|
|
|
|
// err = json.Unmarshal(body, &endpoints)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to parse JSON:", err)
|
|
|
|
// return
|
|
|
|
// }
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// for _, endpoint := range endpoints {
|
|
|
|
// if endpoint.Name == "local" && endpoint.URL == "/var/run/docker.sock" {
|
|
|
|
// fmt.Println("Endpoint exists, exiting...")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// }
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// fmt.Println("Endpoint does not exist, creating...")
|
|
|
|
// endpoint := Endpoint{
|
|
|
|
// Name: "local",
|
|
|
|
// URL: "/var/run/docker.sock",
|
|
|
|
// }
|
|
|
|
// data, err := json.Marshal(endpoint)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to encode JSON:", err)
|
|
|
|
// return
|
|
|
|
// }
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// req, err = http.NewRequest("POST", EndpointURL, bytes.NewBuffer(data))
|
2023-10-18 21:44:24 +08:00
|
|
|
// req.Header.Set("Authorization", "Bearer "+authResponse.Jwt)
|
2023-10-18 19:56:55 +08:00
|
|
|
// resp, err = client.Do(req)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println("Failed to make request:", err)
|
|
|
|
// return
|
|
|
|
// }
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
// if resp.StatusCode != http.StatusCreated {
|
|
|
|
// fmt.Println("Failed to create endpoint:", resp.Status)
|
|
|
|
// } else {
|
|
|
|
// fmt.Println("Endpoint created successfully")
|
|
|
|
// }
|
|
|
|
// }
|
2023-10-18 17:21:53 +08:00
|
|
|
|
2023-10-18 19:56:55 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("Hello World")
|
2023-10-18 17:51:36 +08:00
|
|
|
}
|