diff options
Diffstat (limited to 'post.go')
| -rw-r--r-- | post.go | 60 |
1 files changed, 60 insertions, 0 deletions
@@ -2,8 +2,10 @@ package main import ( "bytes" + "fmt" "io/ioutil" "net/http" + "net/url" "os" "os/user" @@ -36,3 +38,61 @@ func httpPost(url string, data []byte) ([]byte, error) { } return body, nil } + +func parseURL() (string, string) { + parsedURL, err := url.Parse(argv.Server) + if err != nil { + fmt.Println("Error parsing URL:", err) + return "", "" + } + + // Extract Host (includes hostname/IP and port if present) + host := parsedURL.Host + fmt.Println("Host:", host) + + // Extract Hostname (without port) + hostname := parsedURL.Hostname() + fmt.Println("Hostname:", hostname) + + // Extract Port + port := parsedURL.Port() + fmt.Println("Port:", port) + + return parsedURL.Hostname(), parsedURL.Port() +} + +func gusPost(port string, dest string) ([]byte, error) { + var err error + var req *http.Request + + gus, _ := parseURL() + url := fmt.Sprintf("http://%s:%d/%s?port=%s&dest=%s", gus, 2522, "enable", port, dest) + + data := []byte("hello world") + + req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) + + usr, _ := user.Current() + req.Header.Set("author", usr.Username) + hostname, _ := os.Hostname() + req.Header.Set("hostname", hostname) + req.Header.Set("port", port) + req.Header.Set("dest", dest) + + log.Printf("gusPust url(%s) port(%s) dest(%s) hostname(%s)\n", url, port, dest, hostname) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Error(err) + return nil, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Error(err) + return body, err + } + return body, nil +} |
