2024-01-12 04:14:09 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-31 08:45:58 -06:00
|
|
|
"embed"
|
2024-01-12 04:14:09 -06:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-01-31 08:45:58 -06:00
|
|
|
"os"
|
2024-01-12 15:17:55 -06:00
|
|
|
"strings"
|
2024-01-31 08:45:58 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
2024-01-12 04:14:09 -06:00
|
|
|
)
|
|
|
|
|
2024-11-06 14:50:43 -06:00
|
|
|
// are sent via -ldflags at buildtime
|
|
|
|
var VERSION string
|
2024-11-16 00:07:41 -06:00
|
|
|
var BUILDTIME string
|
2024-10-08 11:36:46 -05:00
|
|
|
|
2024-02-23 14:23:41 -06:00
|
|
|
//go:embed resources/*
|
2024-02-26 21:48:51 -06:00
|
|
|
var resources embed.FS
|
2024-01-12 04:14:09 -06:00
|
|
|
|
2024-10-08 10:53:57 -05:00
|
|
|
// var accessf, clientf *os.File
|
2024-01-14 11:39:22 -06:00
|
|
|
|
2024-01-31 08:45:58 -06:00
|
|
|
var repoMap map[string]string
|
|
|
|
var versionMap map[string]string
|
|
|
|
var configfile []string
|
|
|
|
var keysSorted []string
|
|
|
|
|
2024-10-08 11:36:46 -05:00
|
|
|
var HOSTNAME string = "go.wit.com"
|
|
|
|
|
2024-01-12 16:04:08 -06:00
|
|
|
// remove '?' part and trailing '/'
|
|
|
|
func cleanURL(url string) string {
|
|
|
|
url = "/" + strings.Trim(url, "/")
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2024-01-12 04:14:09 -06:00
|
|
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-08 10:53:57 -05:00
|
|
|
// dumpClient(accessf, clientf, r)
|
2024-01-12 04:14:09 -06:00
|
|
|
var tmp string
|
2024-01-12 16:04:08 -06:00
|
|
|
// tmp = r.URL.String()
|
|
|
|
tmp = cleanURL(r.URL.Path)
|
2024-01-12 15:17:55 -06:00
|
|
|
parts := strings.Split(tmp, "?")
|
2024-01-12 16:04:08 -06:00
|
|
|
log.Info("client sent url =", tmp)
|
|
|
|
log.Info("parts are:", parts)
|
2024-01-12 15:17:55 -06:00
|
|
|
requrl := parts[0]
|
|
|
|
|
|
|
|
url, repourl := findkey(requrl)
|
2024-10-08 11:36:46 -05:00
|
|
|
log.Warn("gowebd URL =", url, "REPO URL =", repourl, "REQUEST URL =", requrl)
|
2024-01-12 11:39:36 -06:00
|
|
|
if repourl != "" {
|
2024-01-12 15:17:55 -06:00
|
|
|
repoHTML(w, url, repourl)
|
2024-01-12 11:39:36 -06:00
|
|
|
return
|
|
|
|
}
|
2024-01-12 12:26:05 -06:00
|
|
|
if tmp == "/" {
|
|
|
|
indexHeader(w)
|
|
|
|
indexBodyStart(w)
|
2024-01-12 13:52:55 -06:00
|
|
|
indexBodyScanConfig(w)
|
2024-01-15 16:28:03 -06:00
|
|
|
indexDivEnd(w)
|
2024-11-06 17:10:00 -06:00
|
|
|
pfile, err := os.ReadFile("/etc/gowebd/footer.html")
|
|
|
|
if err == nil {
|
|
|
|
fmt.Fprint(w, string(pfile))
|
|
|
|
} else {
|
|
|
|
log.Warn(err, "no footer filee found in /etc/gowebd/footer.html")
|
|
|
|
log.Warn("falling back to the resources/footer.html")
|
|
|
|
writeFile(w, "footer.html")
|
|
|
|
}
|
2024-01-12 12:26:05 -06:00
|
|
|
indexBodyEnd(w)
|
|
|
|
return
|
|
|
|
}
|
2024-02-11 19:26:44 -06:00
|
|
|
if tmp == "/install.sh" {
|
2024-02-26 21:48:51 -06:00
|
|
|
writeFile(w, "install.sh")
|
2024-02-11 19:26:44 -06:00
|
|
|
return
|
|
|
|
}
|
2024-01-17 16:05:28 -06:00
|
|
|
if tmp == "/me" {
|
|
|
|
j, err := dumpJsonClient(r)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "BAD ZOOT")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, j)
|
|
|
|
return
|
|
|
|
}
|
2024-02-13 16:14:22 -06:00
|
|
|
if tmp == "/register" {
|
|
|
|
regfile, _ := os.OpenFile("/home/jcarr/regfile.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if registerClient(regfile, r) {
|
|
|
|
fmt.Fprintln(w, "OK")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, "FAILED")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-02-11 19:26:44 -06:00
|
|
|
if tmp == "/goReference.svg" {
|
2024-02-26 21:48:51 -06:00
|
|
|
writeFile(w, "goReference.svg")
|
2024-02-11 19:26:44 -06:00
|
|
|
return
|
|
|
|
}
|
2024-01-12 11:55:45 -06:00
|
|
|
if tmp == "/skeleton.v2.css" {
|
2024-02-26 21:48:51 -06:00
|
|
|
writeFile(w, "skeleton.v2.css")
|
2024-01-12 04:14:09 -06:00
|
|
|
return
|
|
|
|
}
|
2024-10-08 11:36:46 -05:00
|
|
|
if tmp == "/favicon.ico" {
|
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
2024-09-09 10:25:03 -05:00
|
|
|
// used for uptime monitor checking
|
|
|
|
if tmp == "/uptime" {
|
2024-10-06 16:34:16 -05:00
|
|
|
writeFile(w, "uptime.html")
|
2024-09-09 10:25:03 -05:00
|
|
|
return
|
|
|
|
}
|
2024-01-12 15:17:55 -06:00
|
|
|
log.Warn("BAD URL =", url, "REPO URL =", repourl)
|
2024-01-12 17:38:05 -06:00
|
|
|
badurl(w, r.URL.String())
|
|
|
|
// fmt.Fprintln(w, "BAD", tmp)
|
2024-01-12 04:14:09 -06:00
|
|
|
}
|
|
|
|
|
2024-02-26 21:48:51 -06:00
|
|
|
func writeFile(w http.ResponseWriter, filename string) {
|
2024-01-12 11:55:45 -06:00
|
|
|
// fmt.Fprintln(w, "GOT TEST?")
|
2024-02-26 21:48:51 -06:00
|
|
|
fullname := "resources/" + filename
|
|
|
|
pfile, err := resources.ReadFile(fullname)
|
2024-01-31 08:45:58 -06:00
|
|
|
if err != nil {
|
2024-01-12 11:55:45 -06:00
|
|
|
log.Println("ERROR:", err)
|
2024-01-12 04:14:09 -06:00
|
|
|
// w.Write(pfile)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var repohtml string
|
|
|
|
repohtml = string(pfile)
|
2024-02-26 21:48:51 -06:00
|
|
|
if filename == "goReference.svg" {
|
2024-02-11 19:26:44 -06:00
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
}
|
2024-01-12 11:55:45 -06:00
|
|
|
fmt.Fprintln(w, repohtml)
|
2024-02-26 21:48:51 -06:00
|
|
|
log.Println("writeFile() found internal file:", filename)
|
2024-01-12 04:14:09 -06:00
|
|
|
// w.Close()
|
|
|
|
/*
|
2024-01-31 08:45:58 -06:00
|
|
|
filename = "/tmp/" + name + ".so"
|
|
|
|
log.Error(err, "write out file here", name, filename, len(pfile))
|
|
|
|
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
f.Close()
|
2024-01-12 04:14:09 -06:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-10-08 10:53:57 -05:00
|
|
|
// accessf, _ = os.OpenFile("/home/jcarr/accessclient.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
// clientf, _ = os.OpenFile("/home/jcarr/httpclient.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
2024-01-12 04:44:36 -06:00
|
|
|
readconfigfile()
|
2024-01-31 08:45:58 -06:00
|
|
|
readVersionFile()
|
|
|
|
|
2024-02-11 19:26:44 -06:00
|
|
|
// make the instructions to install be:
|
|
|
|
// curl -sSL https://go.wit.com/install.sh | bash
|
|
|
|
|
|
|
|
// build instruction:
|
|
|
|
// go install go.wit.com/apps/getgui@latest
|
|
|
|
|
2024-02-10 07:34:58 -06:00
|
|
|
// for i, s := range versionMap {
|
|
|
|
// log.Println("found i =", i, "with", "s =", s)
|
|
|
|
// }
|
2024-01-31 08:45:58 -06:00
|
|
|
log.Println("found log =", versionMap["go.wit.com/log"])
|
2024-01-12 04:14:09 -06:00
|
|
|
http.HandleFunc("/", okHandler)
|
2024-10-06 16:34:16 -05:00
|
|
|
// go https()
|
2024-10-08 11:36:46 -05:00
|
|
|
p := fmt.Sprintf(":%d", argv.Port)
|
|
|
|
log.Println("HOSTNAME set to:", HOSTNAME)
|
|
|
|
log.Println("Running on port", p)
|
|
|
|
err := http.ListenAndServe(p, nil)
|
2024-01-12 04:14:09 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error starting server:", err)
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 17:38:05 -06:00
|
|
|
|
|
|
|
func badurl(w http.ResponseWriter, badurl string) {
|
|
|
|
fmt.Fprintln(w, "<!DOCTYPE html>")
|
|
|
|
fmt.Fprintln(w, "<html>")
|
|
|
|
fmt.Fprintln(w, " <head>")
|
2024-10-08 11:36:46 -05:00
|
|
|
fmt.Fprintln(w, " <meta http-equiv=\"refresh\" content=\"3; url=https://" + HOSTNAME + "/\">")
|
2024-01-12 17:38:05 -06:00
|
|
|
fmt.Fprintln(w, " </head>")
|
|
|
|
fmt.Fprintln(w, " <body>")
|
|
|
|
fmt.Fprintln(w, " IPv4 IS NOT SUPPORTED<br>")
|
|
|
|
fmt.Fprintln(w, " MANY OF THESE REPOS REQUIRE IPv6.<br>")
|
|
|
|
fmt.Fprintln(w, " <br>")
|
2024-11-17 06:26:01 -06:00
|
|
|
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://go.wit.com/\">redirecting</a>")
|
2024-01-12 17:38:05 -06:00
|
|
|
fmt.Fprintln(w, " </body>")
|
|
|
|
fmt.Fprintln(w, "</html>")
|
|
|
|
}
|
2024-01-31 08:45:58 -06:00
|
|
|
|
|
|
|
func formatDuration(d time.Duration) string {
|
|
|
|
seconds := int(d.Seconds()) % 60
|
|
|
|
minutes := int(d.Minutes()) % 60
|
|
|
|
hours := int(d.Hours()) % 24
|
|
|
|
days := int(d.Hours()) / 24
|
|
|
|
|
|
|
|
result := ""
|
|
|
|
if days > 0 {
|
|
|
|
result += fmt.Sprintf("%dd ", days)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
if hours > 0 {
|
|
|
|
result += fmt.Sprintf("%dh ", hours)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
if minutes > 0 {
|
|
|
|
result += fmt.Sprintf("%dm ", minutes)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
if seconds > 0 {
|
|
|
|
result += fmt.Sprintf("%ds", seconds)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|