Skip to content
Snippets Groups Projects
main.go 3.64 KiB
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/docopt/docopt-go"
)

var (
	appVersion    string
	buildTime     string
	webDir        string
	WarningLogger *log.Logger
	InfoLogger    *log.Logger
	ErrorLogger   *log.Logger
	verboseBool   bool
)

func init() {
	InfoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime)
	WarningLogger = log.New(os.Stdout, "WARNING: ", log.Ldate|log.Ltime)
	ErrorLogger = log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime)
}

// serve directory ipv6
func renderPage(w http.ResponseWriter, req *http.Request) {
	provider := strings.Split(req.URL.Path, "/")
	serveFile := filepath.Join(webDir, req.URL.Path)
	cmd := exec.Command("/usr/bin/cert2json.sh", provider[1])
	err := cmd.Run()
	if err != nil {
		WarningLogger.Println(err)
		w.WriteHeader(http.StatusServiceUnavailable)
	} else {
		if verboseBool {
			InfoLogger.Printf("HTTP Status %v", http.StatusOK)
		}
		w.WriteHeader(http.StatusOK)
	}
	fmt.Printf("test %s\n", serveFile)
	time.Sleep(1 * time.Second)
	http.ServeFile(w, req, serveFile)
}

// function redirect
func redirect(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, "/index.html", http.StatusMovedPermanently)
}

func main() {

	progName := filepath.Base(os.Args[0])

	usage := fmt.Sprintf(`ACME Web:
  - serve ACME HTML pages

Usage:
  %v [--listen-address=LISTENADDRESS] [--listen-port=LISTENPORT] [--verbose]
  %v -h | --help
  %v -b | --build
  %v -v | --version

Options:
  -h --help                         Show this screen
  -b --build                        Print version and build information and exit
  -v --version                      Print version information and exit
  --listen-address=LISTENADDRESS    Web server address. Check Go net/http documentation [default: any]
  --listen-port=LISTENPORT          Web server port [default: 8000]
  --verbose                         Log also successful connections
`, progName, progName, progName, progName)

	arguments, _ := docopt.ParseArgs(usage, nil, appVersion)

	if arguments["--build"] == true {
		fmt.Printf("%v version: %v, built on: %v\n", progName, appVersion, buildTime)
		os.Exit(0)
	}

	webDir = "/var/www/acme_web"
	verboseBool = arguments["--verbose"].(bool)
	listenAddress := arguments["--listen-address"].(string)
	listenPort := arguments["--listen-port"].(string)

	baseURLs := [6]string{"/sectigo_ev", "/sectigo_ov", "/letsencrypt", "/sectigo_ev/", "/sectigo_ov/", "/letsencrypt/"}
	otherURLs := [12]string{"/letsencrypt/cert_mame.html", "/letsencrypt/expiry_date.html",
		"/letsencrypt/letsencrypt.json", "/letsencrypt/letsencrypt_expired.json",
		"/sectigo_ov/cert_mame.html", "/sectigo_ov/expiry_date.html",
		"/sectigo_ov/sectigo_ov.json", "/sectigo_ov/sectigo_ov_expired.json",
		"/sectigo_ev/incert_mamedex.html", "/sectigo_ev/expiry_date.html",
		"/sectigo_ev/sectigo_ev.json", "/sectigo_ev/sectigo_ev_expired.json"}

	fs := http.FileServer(http.Dir("/var/www/acme_web/static"))
	http.Handle("/static/", http.StripPrefix("/static/", fs))

	//http.HandleFunc("index.html", func(res http.ResponseWriter, req *http.Request) {
	//	http.ServeFile(res, req, filepath.Join(webDir, "index.html"))
	//})
	http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		http.ServeFile(res, req, filepath.Join(webDir, "index.html"))
	})

	for _, element := range baseURLs {
		http.HandleFunc(element, redirect)
	}

	for _, otherElement := range otherURLs {
		http.HandleFunc(otherElement, renderPage)
	}

	if listenAddress == "any" {
		log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", listenPort), nil))
	} else {
		log.Fatal(http.ListenAndServe(fmt.Sprintf("%v:%v", listenAddress, listenPort), nil))
	}

}