| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 | package smartimport (	"bufio"	"encoding/base64"	"errors"	"io/ioutil"	"log"	"net/http"	"os"	"os/exec"	"strconv"	"strings"	"time")/*	SYSTEM COMMON FUNCTIONS	This is a system function that put those we usually use function but not belongs to	any module / system.	E.g. fileExists / IsDir etc*//*	Basic Response Functions	Send response with ease*///Send text response with given w and message as stringfunc sendTextResponse(w http.ResponseWriter, msg string) {	w.Write([]byte(msg))}//Send JSON response, with an extra json headerfunc sendJSONResponse(w http.ResponseWriter, json string) {	w.Header().Set("Content-Type", "application/json")	w.Write([]byte(json))}func sendErrorResponse(w http.ResponseWriter, errMsg string) {	w.Header().Set("Content-Type", "application/json")	w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))}func sendOK(w http.ResponseWriter) {	w.Header().Set("Content-Type", "application/json")	w.Write([]byte("\"OK\""))}/*	The paramter move function (mv)	You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in	r (HTTP Request Object)	getParamter (string, aka $_GET['This string])	Will return	Paramter string (if any)	Error (if error)*/func mv(r *http.Request, getParamter string, postMode bool) (string, error) {	if postMode == false {		//Access the paramter via GET		keys, ok := r.URL.Query()[getParamter]		if !ok || len(keys[0]) < 1 {			//log.Println("Url Param " + getParamter +" is missing")			return "", errors.New("GET paramter " + getParamter + " not found or it is empty")		}		// Query()["key"] will return an array of items,		// we only want the single item.		key := keys[0]		return string(key), nil	} else {		//Access the parameter via POST		r.ParseForm()		x := r.Form.Get(getParamter)		if len(x) == 0 || x == "" {			return "", errors.New("POST paramter " + getParamter + " not found or it is empty")		}		return string(x), nil	}}func stringInSlice(a string, list []string) bool {	for _, b := range list {		if b == a {			return true		}	}	return false}func fileExists(filename string) bool {	_, err := os.Stat(filename)	if os.IsNotExist(err) {		return false	}	return true}func IsDir(path string) bool {	if fileExists(path) == false {		return false	}	fi, err := os.Stat(path)	if err != nil {		log.Fatal(err)		return false	}	switch mode := fi.Mode(); {	case mode.IsDir():		return true	case mode.IsRegular():		return false	}	return false}func inArray(arr []string, str string) bool {	for _, a := range arr {		if a == str {			return true		}	}	return false}func timeToString(targetTime time.Time) string {	return targetTime.Format("2006-01-02 15:04:05")}func IntToString(number int) string {	return strconv.Itoa(number)}func StringToInt(number string) (int, error) {	return strconv.Atoi(number)}func StringToInt64(number string) (int64, error) {	i, err := strconv.ParseInt(number, 10, 64)	if err != nil {		return -1, err	}	return i, nil}func Int64ToString(number int64) string {	convedNumber := strconv.FormatInt(number, 10)	return convedNumber}func GetUnixTime() int64 {	return time.Now().Unix()}func LoadImageAsBase64(filepath string) (string, error) {	if !fileExists(filepath) {		return "", errors.New("File not exists")	}	f, _ := os.Open(filepath)	reader := bufio.NewReader(f)	content, _ := ioutil.ReadAll(reader)	encoded := base64.StdEncoding.EncodeToString(content)	return string(encoded), nil}//Get the IP address of the current authentication userfunc ReflectUserIP(w http.ResponseWriter, r *http.Request) {	requestPort, _ := mv(r, "port", false)	showPort := false	if requestPort == "true" {		//Show port as well		showPort = true	}	IPAddress := r.Header.Get("X-Real-Ip")	if IPAddress == "" {		IPAddress = r.Header.Get("X-Forwarded-For")	}	if IPAddress == "" {		IPAddress = r.RemoteAddr	}	if !showPort {		IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")]	}	w.Write([]byte(IPAddress))	return}func execCommand(executable string, args ...string) string {	shell := exec.Command(executable, args...) // Run command	output, err := shell.CombinedOutput()      // Response from cmdline	if err != nil && string(output) == "" {    // If done w/ errors then		log.Println(err)		return ""	}	return string(output)}func wmicGetinfo(wmicName string, itemName string) []string {	//get systeminfo	var InfoStorage []string	cmd := exec.Command("chcp", "65001")	cmd = exec.Command("wmic", wmicName, "list", "full", "/format:list")	if wmicName == "os" {		cmd = exec.Command("wmic", wmicName, "get", "*", "/format:list")	}	if len(wmicName) > 6 {		if wmicName[0:6] == "Win32_" {			cmd = exec.Command("wmic", "path", wmicName, "get", "*", "/format:list")		}	}	out, _ := cmd.CombinedOutput()	strOut := string(out)	strSplitedOut := strings.Split(strOut, "\n")	for _, strConfig := range strSplitedOut {		if strings.Contains(strConfig, "=") {			strSplitedConfig := strings.SplitN(strConfig, "=", 2)			if strSplitedConfig[0] == itemName {				strSplitedConfigReplaced := strings.Replace(strSplitedConfig[1], "\r", "", -1)				InfoStorage = append(InfoStorage, strSplitedConfigReplaced)			}		}	}	if len(InfoStorage) == 0 {		InfoStorage = append(InfoStorage, "Undefined")	}	return InfoStorage}
 |