1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "net/url"
9 "os"
10 "os/exec"
11 "runtime"
12
13 "code.rocketnine.space/tslocum/ez"
14 "code.rocketnine.space/tslocum/gmitohtml/pkg/gmitohtml"
15 )
16
17 func openBrowser(url string) {
18 var err error
19 switch runtime.GOOS {
20 case "linux":
21 err = exec.Command("xdg-open", url).Start()
22 case "windows":
23 err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
24 case "darwin":
25 err = exec.Command("open", url).Start()
26 default:
27 err = fmt.Errorf("unsupported platform")
28 }
29 if err != nil {
30 log.Fatal(err)
31 }
32 }
33
34 func main() {
35 var (
36 view bool
37 allowFile bool
38 daemon string
39 hostname string
40 configFile string
41 )
42 flag.BoolVar(&view, "view", false, "open web browser")
43 flag.BoolVar(&allowFile, "allow-file", false, "allow local file access via file://")
44 flag.StringVar(&daemon, "daemon", "", "start daemon on specified address")
45 flag.StringVar(&hostname, "hostname", "", "server hostname (e.g. rocketnine.space) (defaults to daemon address)")
46 flag.StringVar(&configFile, "config", "", "path to configuration file")
47
48 flag.Parse()
49
50 defaultConfig, err := ez.DefaultConfigPath("gmitohtml")
51 if err != nil {
52 log.Fatal(err)
53 } else if configFile == "" {
54 configFile = defaultConfig
55 }
56
57 if configFile != "" {
58 var configExists bool
59 if _, err := os.Stat(defaultConfig); !os.IsNotExist(err) {
60 configExists = true
61 }
62
63 if configExists || configFile != defaultConfig {
64 err := ez.Deserialize(gmitohtml.Config, configFile)
65 if err != nil {
66 log.Fatalf("failed to read configuration file at %s: %v\nSee CONFIGURATION.md for information on configuring gmitohtml", configFile, err)
67 }
68
69 for u, label := range gmitohtml.Config.Bookmarks {
70 gmitohtml.AddBookmark(u, label)
71 }
72 }
73 }
74
75 for domain, cc := range gmitohtml.Config.Certs {
76 certData, err := ioutil.ReadFile(cc.Cert)
77 if err != nil {
78 log.Fatalf("failed to load client certificate for domain %s: %s", domain, err)
79 }
80
81 keyData, err := ioutil.ReadFile(cc.Key)
82 if err != nil {
83 log.Fatalf("failed to load client certificate for domain %s: %s", domain, err)
84 }
85
86 err = gmitohtml.SetClientCertificate(domain, certData, keyData)
87 if err != nil {
88 log.Fatalf("failed to load client certificate for domain %s", domain)
89 }
90 }
91
92 if daemon != "" {
93 gmitohtml.SetOnBookmarksChanged(func() {
94 gmitohtml.Config.Bookmarks = gmitohtml.GetBookmarks()
95
96 err := ez.Serialize(gmitohtml.Config, configFile)
97 if err != nil {
98 log.Fatal(err)
99 }
100 })
101
102 err := gmitohtml.StartDaemon(daemon, hostname, allowFile)
103 if err != nil {
104 log.Fatal(err)
105 }
106
107 if view {
108 openBrowser("http://" + daemon)
109 }
110
111 select {}
112 }
113
114 data, err := ioutil.ReadAll(os.Stdin)
115 if err != nil {
116 log.Fatal(err)
117 }
118
119 data = gmitohtml.Convert(data, "")
120
121 if view {
122 openBrowser(string(append([]byte("data:text/html,"), []byte(url.PathEscape(string(data)))...)))
123 return
124 }
125 fmt.Print(gmitohtml.Convert(data, ""))
126 }
127
View as plain text