package main import ( "github.com/BurntSushi/toml" ) type Config struct { HttpPort int HttpsPort int CertPath string KeyPath string DocBase string HomeDocBase string LogPath string AllowRemoteContent bool MaxHtmlDepth int MaxImages int BadMimes []string BadMimesMap map[string]bool BadTags []string BadTagsMap map[string]bool BadDomains []string BadAttrs []string BadAttrsMap map[string]bool } func getConfig(filename string) (Config, error) { var config Config // Defaults config.HttpPort = 80 config.HttpsPort = 443 config.CertPath = "cert.pem" config.KeyPath = "key.pem" config.DocBase = "/var/www/" config.HomeDocBase = "/html/" config.LogPath = "shizaru.log" config.AllowRemoteContent = false config.MaxHtmlDepth = 10 config.MaxImages = 3 config.BadMimes = []string{ "text/javascript", "application/javascript", "application/x-shockwave-flash", } config.BadTags = []string{ "applet", "audio", "base", "blink", "canvas", "embed", "frame", "frameset", "iframe", "marquee", "script", } config.BadAttrs = []string{ "onabort", "onafterprint", "onbeforeprint", "onbeforeunload", "onblur", "oncanplay", "oncanplaythrough", "onchange", "onclick", "oncontextmenu", "oncopy", "oncuechange", "oncut", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "onerror", "onfocus", "onhashchange", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmessage", "onmousedown", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onoffline", "ononline", "onpagehide", "onpageshow", "onpaste", "onpause", "onplay", "onplaying", "onpopstate", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onsearch", "onseeked", "onseeking", "onselect", "onstalled", "onstorage", "onsubmit", "onsuspend", "ontimeupdate", "ontoggle", "onunload", "onvolumechange", "onwaiting", "onwheel", } config.BadDomains = []string{ "facebook.com", "google.com", "instagram.com", "twitter.com", } if filename == "" { return config, nil } _, err := toml.DecodeFile(filename, &config) if err != nil { return config, err } // Build maps from lists config.BadTagsMap = sliceToMap(config.BadTags) config.BadAttrsMap = sliceToMap(config.BadAttrs) config.BadMimesMap = sliceToMap(config.BadMimes) // Done return config, nil } func sliceToMap(slice []string) map[string]bool { themap := make(map[string]bool) for _, x := range slice { themap[x] = true } return themap }