2020-11-21 17:53:04 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-11-24 18:39:55 +01:00
|
|
|
"path"
|
2020-11-21 17:53:04 +01:00
|
|
|
"runtime"
|
2020-11-24 18:39:55 +01:00
|
|
|
|
|
|
|
"gitlab.com/tslocum/gmitohtml/pkg/gmitohtml"
|
2020-11-21 17:53:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func openBrowser(url string) {
|
|
|
|
var err error
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
err = exec.Command("xdg-open", url).Start()
|
|
|
|
case "windows":
|
|
|
|
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
|
|
|
|
case "darwin":
|
|
|
|
err = exec.Command("open", url).Start()
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported platform")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var view bool
|
2020-11-25 03:18:16 +01:00
|
|
|
var allowFile bool
|
2020-11-21 17:53:04 +01:00
|
|
|
var daemon string
|
2020-11-24 18:29:57 +01:00
|
|
|
var configFile string
|
2020-11-21 17:53:04 +01:00
|
|
|
flag.BoolVar(&view, "view", false, "open web browser")
|
2020-11-25 03:18:16 +01:00
|
|
|
flag.BoolVar(&allowFile, "allow-file", false, "allow local file access via file://")
|
2020-11-21 17:53:04 +01:00
|
|
|
flag.StringVar(&daemon, "daemon", "", "start daemon on specified address")
|
2020-11-24 18:29:57 +01:00
|
|
|
flag.StringVar(&configFile, "config", "", "path to configuration file")
|
2020-11-21 17:53:04 +01:00
|
|
|
// TODO option to include response header in page
|
|
|
|
flag.Parse()
|
|
|
|
|
2020-11-24 18:29:57 +01:00
|
|
|
if configFile == "" {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err == nil && homedir != "" {
|
|
|
|
defaultConfig := path.Join(homedir, ".config", "gmitohtml", "config.yaml")
|
|
|
|
if _, err := os.Stat(defaultConfig); !os.IsNotExist(err) {
|
|
|
|
configFile = defaultConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if configFile != "" {
|
|
|
|
err := readconfig(configFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring gmitohtml", configFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for domain, cc := range config.Certs {
|
|
|
|
certData, err := ioutil.ReadFile(cc.Cert)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to load client certificate for domain %s: %s", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyData, err := ioutil.ReadFile(cc.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to load client certificate for domain %s: %s", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gmitohtml.SetClientCertificate(domain, certData, keyData)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to load client certificate for domain %s", domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 17:53:04 +01:00
|
|
|
if daemon != "" {
|
2020-11-25 03:18:16 +01:00
|
|
|
err := gmitohtml.StartDaemon(daemon, allowFile)
|
2020-11-21 17:53:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if view {
|
|
|
|
openBrowser("http://" + daemon)
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:29:57 +01:00
|
|
|
select {}
|
2020-11-21 17:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = gmitohtml.Convert(data, "")
|
|
|
|
|
|
|
|
if view {
|
|
|
|
openBrowser(string(append([]byte("data:text/html,"), []byte(url.PathEscape(string(data)))...)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Print(gmitohtml.Convert(data, ""))
|
|
|
|
}
|