123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- package main
- import (
- "bytes"
- "compress/gzip"
- "crypto/sha1"
- "crypto/sha512"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "os"
- "regexp"
- "strings"
- )
- type Site struct {
- Host string `json:host`
- MinimumLength int `json:minimumLength`
- MaximumLength int `json:maximumLength`
- SpecialCharacters string `json:specialCharacters`
- NumberOfSpecialCharacters int `json:numberOfSpecialCharacters`
- NumberOfUpperCase int `json:numberOfUpperCase`
- NumberOfDigits int `json:numberOfDigits`
- Revision int `json:revision`
- }
- func main() {
- http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
- })
- http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- path := strings.TrimPrefix(r.URL.Path, "/")
- if path == "" {
-
- page, err := ioutil.ReadFile("index.html")
- if err != nil {
- http.NotFound(w, r)
- return
- }
- fmt.Fprintf(w, string(page))
- } else {
-
- }
- })
- log.Fatal(http.ListenAndServe("localhost:8080", nil))
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func Save(file string, sites []Site) error {
-
- if _, err := os.Stat(file); os.IsNotExist(err) {
- _, err = os.Create(file)
- if err != nil {
- return err
- }
- }
-
- b, err := json.Marshal(sites)
- if err != nil {
- return err
- }
-
- buffer := bytes.NewBuffer(b)
- gzipWriter := gzip.NewWriter(buffer)
- if err != nil {
- return err
- }
- defer gzipWriter.Close()
-
- fi, err := os.OpenFile(file, os.O_WRONLY, 0666)
- if err != nil {
- return err
- }
- defer fi.Close()
- _, err = fi.Write(buffer.Bytes())
- if err != nil {
- return err
- }
- return nil
- }
- func Read(file string) ([]Site, error) {
-
- if _, err := os.Stat(file); os.IsNotExist(err) {
- return []Site{}, nil
- }
-
- log.Println("Reading compressed file")
- compressed, err := ioutil.ReadFile(file)
- if err != nil {
- return nil, err
- }
-
- log.Println("Decompressing")
- buffer := bytes.NewBuffer(compressed)
- gzipReader, err := gzip.NewReader(buffer)
- if err != nil {
- return nil, err
- }
- defer gzipReader.Close()
-
- log.Println("Unmarshal")
- var sites []Site
- err = json.Unmarshal(buffer.Bytes(), &sites)
- if err != nil {
- return nil, err
- }
- return sites, nil
- }
- func getBookname(profile string) []byte {
- sha := sha1.New()
- sha.Write([]byte(profile))
- return sha.Sum(nil)
- }
- func encrypt(clearText, profile, passphrase string) ([]byte, error) {
- return nil, nil
- }
- func decrypt(encryptedText, profile, passphrase string) ([]byte, error) {
- return nil, nil
- }
- func generatePassphrase(profile, passphrase string, settings Site) ([]byte, error) {
- clearText := fmt.Sprintf(
- "%s-%s-%s-%s",
- strings.ToLower(profile),
- strings.ToLower(passphrase),
- strings.ToLower(settings.Host),
- settings.Revision)
- sha := sha512.New()
- sha.Write([]byte(clearText))
- hash := sha.Sum(nil)
- hash = []byte(fmt.Sprintf("%x", hash))
-
- applySiteSettings(hash, settings)
-
- if settings.MaximumLength > -1 {
- hash = hash[:settings.MaximumLength]
- }
-
- if !validateLength(hash, settings.MinimumLength, settings.MaximumLength) {
- log.Println("Does not meed the length requirements")
- }
- return hash, nil
- }
- func applySiteSettings(source []byte, settings Site) []byte {
- if !containsUppercase(source, settings.NumberOfUpperCase) {
- i := 0
- r := regexp.MustCompile(`[a-z]+`)
- var matches [][]int
- if matches = r.FindAllIndex(source, -1); matches != nil {
- for _, v := range matches {
- if i < settings.NumberOfUpperCase {
- c := strings.ToUpper(string(source[v[0]]))
- source[v[0]] = []byte(c)[0]
- i += 1
- }
- }
- }
- }
- if !containsDigits(source, settings.NumberOfDigits) {
- i := 0
- r := regexp.MustCompile(`[a-z]+`)
- var matches [][]int
- if matches = r.FindAllIndex(source, -1); matches != nil {
- for _, v := range matches {
- if i < settings.NumberOfDigits {
- source[v[0]] = byte(i)
- i += 1
- }
- }
- }
- }
- if !containsSpecialCharacters(source, settings.SpecialCharacters, settings.NumberOfSpecialCharacters) {
- i := 0
- r := regexp.MustCompile(`[a-z]+`)
- var matches [][]int
- if matches = r.FindAllIndex(source, -1); matches != nil {
- for _, v := range matches {
- if i < settings.NumberOfSpecialCharacters {
- i += 1
- source[v[0]] = []byte(settings.SpecialCharacters)[len(settings.SpecialCharacters)-i]
- }
- }
- }
- }
- return source
- }
- func containsDigits(source []byte, minOccurrences int) bool {
- r := regexp.MustCompile(`\d`)
- var matches [][]byte
- if matches = r.FindAll(source, -1); matches == nil {
- return false
- }
- return len(matches) >= minOccurrences
- }
- func containsUppercase(source []byte, minOccurrences int) bool {
- r := regexp.MustCompile(`[A-Z]+`)
- var matches [][]byte
- if matches = r.FindAll(source, -1); matches == nil {
- return false
- }
- return len(matches) >= minOccurrences
- }
- func containsSpecialCharacters(source []byte, specialCharacters string, minOccurrences int) bool {
- s := specialCharacters
- s = strings.Replace(s, "\\", "\\\\", -1)
- s = strings.Replace(s, ".", "\\.", -1)
- s = strings.Replace(s, " ", "\\s", -1)
- s = strings.Replace(s, "-", "\\-", -1)
- s = strings.Replace(s, "[", "\\[", -1)
- s = strings.Replace(s, "]", "\\]", -1)
- r := regexp.MustCompile(`[` + s + `]+`)
- var matches [][]byte
- if matches = r.FindAll(source, -1); matches == nil {
- return false
- }
- return len(matches) >= minOccurrences
- }
- func validateLength(source []byte, minimum, maximum int) bool {
- if minimum > -1 && len(source) < minimum {
- return false
- }
- if maximum > -1 && len(source) > maximum {
- return false
- }
- return true
- }
|