99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/user"
|
|
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
func httpPost(url string, data []byte) ([]byte, error) {
|
|
var err error
|
|
var req *http.Request
|
|
|
|
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)
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|