2020-11-21 17:53:04 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2020-11-24 18:39:55 +01:00
|
|
|
|
2021-04-08 05:49:29 +02:00
|
|
|
"code.rocketnine.space/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() {
|
2021-01-06 04:00:14 +01:00
|
|
|
var (
|
|
|
|
view bool
|
|
|
|
allowFile bool
|
|
|
|
daemon string
|
|
|
|
hostname string
|
|
|
|
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")
|
2021-01-06 04:00:14 +01:00
|
|
|
flag.StringVar(&hostname, "hostname", "", "server hostname (e.g. rocketnine.space) (defaults to daemon 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()
|
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
defaultConfig := gmitohtml.DefaultConfigPath()
|
2020-11-24 18:29:57 +01:00
|
|
|
if configFile == "" {
|
2020-11-27 05:43:03 +01:00
|
|
|
configFile = defaultConfig
|
2020-11-24 18:29:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if configFile != "" {
|
2020-11-27 05:43:03 +01:00
|
|
|
var configExists bool
|
|
|
|
if _, err := os.Stat(defaultConfig); !os.IsNotExist(err) {
|
|
|
|
configExists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if configExists || configFile != defaultConfig {
|
2021-06-09 01:19:26 +02:00
|
|
|
err := gmitohtml.ReadConfig(configFile)
|
2020-11-27 05:43:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring gmitohtml", configFile, err)
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
for u, label := range gmitohtml.Config.Bookmarks {
|
2020-11-27 05:43:03 +01:00
|
|
|
gmitohtml.AddBookmark(u, label)
|
|
|
|
}
|
2020-11-24 18:29:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
for domain, cc := range gmitohtml.Config.Certs {
|
2020-11-24 18:29:57 +01:00
|
|
|
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-27 05:43:03 +01:00
|
|
|
gmitohtml.SetOnBookmarksChanged(func() {
|
2021-06-09 01:19:26 +02:00
|
|
|
gmitohtml.Config.Bookmarks = gmitohtml.GetBookmarks()
|
2020-11-27 05:43:03 +01:00
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
err := gmitohtml.SaveConfig(configFile)
|
2020-11-27 05:43:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-01-06 04:00:14 +01:00
|
|
|
err := gmitohtml.StartDaemon(daemon, hostname, 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, ""))
|
|
|
|
}
|