summaryrefslogtreecommitdiff
path: root/post.go
blob: f4eb18a91bee0fa4ba8116a87b43b511bcd7e901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
}