forked from BrassHornCommunications/go-packetflagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (110 loc) · 3.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
//"html/template"
"encoding/json"
"flag"
"fmt"
"github.com/boltdb/bolt"
"io/ioutil"
"log"
"net/http"
"runtime"
"strconv"
)
type CoreConf struct {
DbPath string `json:"dbpath"`
ListenPort int64 `json:"listenport"`
Debug bool `json:"debug"`
TLS bool `json:"tls_enabled"`
TLSKey string `json:"tls_private_key"`
TLSCert string `json:"tls_certificate"`
}
type TemplateConf struct {
FQDN string
ListenPort int64
Success bool
/*URLs []string
PACName string
PACDesc string*/
PAC PAC
}
type PAC struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Password string `json:"password"`
URLs []string `json:"urls"`
}
func main() {
log.Println("---------------------------------------------")
//Grab all our command line config
configuration := flag.String("conf", "", "path to configuration file")
flag.Parse()
conf := readConfig(*configuration)
//Populate our template conf - normally passed by value to other functions
tmplConf := TemplateConf{
FQDN: "localhost",
ListenPort: conf.ListenPort,
}
//We have our main web thread and the watcher threads
runtime.GOMAXPROCS(3)
//Tell people what we're up to
log.Println("Using config: " + *configuration)
log.Println("DB Path is: " + conf.DbPath)
//We need a DB for holding PACs
db, err := bolt.Open(conf.DbPath, 0600, nil)
if err != nil {
log.Fatal(err)
} else {
log.Println("DB opened, all is OK")
}
defer db.Close()
//Make sure our buckets exist
db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("pacs"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
//Set webpage elements
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { PFWebIndex(w, r, tmplConf) })
http.HandleFunc("/about/", func(w http.ResponseWriter, r *http.Request) { PFWebAbout(w, r, tmplConf) })
http.HandleFunc("/create/", func(w http.ResponseWriter, r *http.Request) { PFWebCreate(w, r, db, tmplConf) })
http.HandleFunc("/view/", func(w http.ResponseWriter, r *http.Request) { PFWebView(w, r, db, tmplConf) })
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("assets/css/"))))
http.Handle("/font/", http.StripPrefix("/font/", http.FileServer(http.Dir("assets/font/"))))
//Actually serve the PAC file
http.HandleFunc("/pac/", func(w http.ResponseWriter, r *http.Request) { PFWebServ(w, r, db) })
ListenPort := strconv.FormatInt(conf.ListenPort, 10)
if conf.TLS {
http.ListenAndServeTLS(":"+ListenPort, conf.TLSCert, conf.TLSKey, nil)
} else {
http.ListenAndServe(":"+ListenPort, nil)
}
//And we're done
}
// Reads our JSON formatted config file
// and returns a struct
func readConfig(filename string) CoreConf {
var conf CoreConf
if filename == "" {
conf.DbPath = "./urls.bolt"
conf.ListenPort = 8080
} else {
b, err := ioutil.ReadFile(filename)
if err != nil {
/*fmt.Println("Cannot read configuration file ", filename)
os.Exit(1)*/
log.Fatal("Cannot read configuration file ", filename)
}
var conf CoreConf
err = json.Unmarshal(b, &conf)
if err != nil {
/*fmt.Println("Cannot parse configuration file: ", err)
os.Exit(1)*/
log.Fatal("Cannot read configuration file ", filename)
}
}
return conf
}