<commit_before>// Copyright 2016 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"html/template"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/gorilla/mux"
	"zombiezen.com/go/sandpass/pkg/keepass"
	"zombiezen.com/go/sandpass/pkg/uuids"
)

var (
	listen       = flag.String("listen", "[::]:8080", "address to listen on")
	dbPath       = flag.String("db", "", "path to database")
	templatesDir = flag.String("templates_dir", "templates", "path to template directory")
	sessionGC    = flag.Duration("session_gc", 1*time.Minute, "frequency at which sessions are to be cleared from memory after expiring")
)

// Read-only globals
var (
	tmpl   *template.Template
	router *mux.Router
)

// Global state protected by mu.
var (
	mu        sync.Mutex
	sessions  sessionStorage
	dbStorage *storage
)

func main() {
	flag.Parse()
	if *dbPath == "" {
		log.Println("must specify -db")
		os.Exit(1)
	}

	if err := initTemplates(); err != nil {
		log.Println("failed to parse templates:", err)
		os.Exit(1)
	}
	if err := initDatabase(); err != nil {
		// TODO(light): present recovery options to user instead
		log.Println("open database:", err)
		os.Exit(1)
	}
	initHandlers()
	go gcSessions()
	if err := http.ListenAndServe(*listen, nil); err != nil {
		log.Println("listen:", err)
		os.Exit(1)
	}
}

func initTemplates() error {
	var err error
	tmpl, err = template.New("").Funcs(template.FuncMap{
		"sortEntries": sortEntries,
		"emspace":     emspace,
	}).ParseGlob(filepath.Join(*templatesDir, "*.html"))
	return err
}

func initDatabase() error {
	var err error
	dbStorage, err = newStorage(*dbPath)
	return err
}

func initHandlers() {
	r := mux.NewRouter()

	// App handlers
	r.Handle("/", checkPerm("read", appHandler(index)))
	r.Handle("/search", checkPerm("read", appHandler(handleSearch)))
	r.Handle("/groups", checkPerm("read", appHandler(groupList))).Name("listGroups")
	r.Handle("/newgroup", checkPerm("write", appHandler(postGroupForm))).Methods("GET")
	r.Handle("/newgroup", checkPerm("write", appHandler(postGroup))).Methods("POST")
	r.Handle("/groups/{gid}", checkPerm("read", appHandler(viewGroup))).Name("viewGroup").Methods("GET")
	r.Handle("/groups/{gid}", checkPerm("write", appHandler(deleteGroup))).Methods("DELETE")
	r.Handle("/groups/{gid}/edit", checkPerm("write", appHandler(postGroupForm))).Methods("GET")
	r.Handle("/groups/{gid}/edit", checkPerm("write", appHandler(postGroup))).Methods("POST")
	r.Handle("/groups/{gid}/delete", checkPerm("write", appHandler(confirmDeleteGroup))).Methods("GET")
	r.Handle("/groups/{gid}/delete", checkPerm("write", appHandler(deleteGroup))).Methods("POST")
	r.Handle("/groups/{gid}/newentry", checkPerm("write", appHandler(postEntryForm))).Methods("GET")
	r.Handle("/groups/{gid}/newentry", checkPerm("write", appHandler(postEntry))).Methods("POST")
	r.Handle("/groups/{gid}/entry/{uuid}", checkPerm("read", appHandler(viewEntry))).Name("viewEntry").Methods("GET")
	r.Handle("/groups/{gid}/entry/{uuid}", checkPerm("write", appHandler(deleteEntry))).Methods("DELETE")
	r.Handle("/groups/{gid}/entry/{uuid}/edit", checkPerm("write", appHandler(postEntryForm))).Methods("GET")
	r.Handle("/groups/{gid}/entry/{uuid}/edit", checkPerm("write", appHandler(postEntry))).Methods("POST")
	r.Handle("/groups/{gid}/entry/{uuid}/delete", checkPerm("write", appHandler(confirmDeleteEntry))).Methods("GET")
	r.Handle("/groups/{gid}/entry/{uuid}/delete", checkPerm("write", appHandler(deleteEntry))).Methods("POST")
	r.Handle("/nuke", checkPerm("write", appHandler(confirmNuke))).Methods("GET")
	r.Handle("/nuke", checkPerm("write", appHandler(nuke))).Methods("POST")
	r.Handle("/_/newdb", checkPerm("write", appHandler(newDB))).Methods("POST")
	r.Handle("/_/start", checkPerm("write", appHandler(startSession))).Methods("POST")
	r.Handle("/_/pwgen", appHandler(pwgen)).Methods("GET")

	// Static files
	r.Handle("/style.css", serveStaticFile("style.css"))
	r.Handle("/fonts/Roboto-Regular.woff", serveStaticFile("third_party/roboto/Roboto-Regular.woff"))
	r.Handle("/fonts/Roboto-Bold.woff", serveStaticFile("third_party/roboto/Roboto-Bold.woff"))
	r.Handle("/js/clipboard.js", serveStaticFile("third_party/clipboard.js/dist/clipboard.min.js"))
	r.Handle("/js/editentry.js", serveStaticFile("js/editentry.js"))
	r.Handle("/js/entry.js", serveStaticFile("js/entry.js"))
	r.Handle("/js/init.js", serveStaticFile("js/init.js"))
<commit_msg>Require only read permission for /_/start<commit_after>// Copyright 2016 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"html/template"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/gorilla/mux"
	"zombiezen.com/go/sandpass/pkg/keepass"
	"zombiezen.com/go/sandpass/pkg/uuids"
)

var (
	listen       = flag.String("listen", "[::]:8080", "address to listen on")
	dbPath       = flag.String("db", "", "path to database")
	templatesDir = flag.String("templates_dir", "templates", "path to template directory")
	sessionGC    = flag.Duration("session_gc", 1*time.Minute, "frequency at which sessions are to be cleared from memory after expiring")
)

// Read-only globals
var (
	tmpl   *template.Template
	router *mux.Router
)

// Global state protected by mu.
var (
	mu        sync.Mutex
	sessions  sessionStorage
	dbStorage *storage
)

func main() {
	flag.Parse()
	if *dbPath == "" {
		log.Println("must specify -db")
		os.Exit(1)
	}

	if err := initTemplates(); err != nil {
		log.Println("failed to parse templates:", err)
		os.Exit(1)
	}
	if err := initDatabase(); err != nil {
		// TODO(light): present recovery options to user instead
		log.Println("open database:", err)
		os.Exit(1)
	}
	initHandlers()
	go gcSessions()
	if err := http.ListenAndServe(*listen, nil); err != nil {
		log.Println("listen:", err)
		os.Exit(1)
	}
}

func initTemplates() error {
	var err error
	tmpl, err = template.New("").Funcs(template.FuncMap{
		"sortEntries": sortEntries,
		"emspace":     emspace,
	}).ParseGlob(filepath.Join(*templatesDir, "*.html"))
	return err
}

func initDatabase() error {
	var err error
	dbStorage, err = newStorage(*dbPath)
	return err
}

func initHandlers() {
	r := mux.NewRouter()

	// App handlers
	r.Handle("/", checkPerm("read", appHandler(index)))
	r.Handle("/search", checkPerm("read", appHandler(handleSearch)))
	r.Handle("/groups", checkPerm("read", appHandler(groupList))).Name("listGroups")
	r.Handle("/newgroup", checkPerm("write", appHandler(postGroupForm))).Methods("GET")
	r.Handle("/newgroup", checkPerm("write", appHandler(postGroup))).Methods("POST")
	r.Handle("/groups/{gid}", checkPerm("read", appHandler(viewGroup))).Name("viewGroup").Methods("GET")
	r.Handle("/groups/{gid}", checkPerm("write", appHandler(deleteGroup))).Methods("DELETE")
	r.Handle("/groups/{gid}/edit", checkPerm("write", appHandler(postGroupForm))).Methods("GET")
	r.Handle("/groups/{gid}/edit", checkPerm("write", appHandler(postGroup))).Methods("POST")
	r.Handle("/groups/{gid}/delete", checkPerm("write", appHandler(confirmDeleteGroup))).Methods("GET")
	r.Handle("/groups/{gid}/delete", checkPerm("write", appHandler(deleteGroup))).Methods("POST")
	r.Handle("/groups/{gid}/newentry", checkPerm("write", appHandler(postEntryForm))).Methods("GET")
	r.Handle("/groups/{gid}/newentry", checkPerm("write", appHandler(postEntry))).Methods("POST")
	r.Handle("/groups/{gid}/entry/{uuid}", checkPerm("read", appHandler(viewEntry))).Name("viewEntry").Methods("GET")
	r.Handle("/groups/{gid}/entry/{uuid}", checkPerm("write", appHandler(deleteEntry))).Methods("DELETE")
	r.Handle("/groups/{gid}/entry/{uuid}/edit", checkPerm("write", appHandler(postEntryForm))).Methods("GET")
	r.Handle("/groups/{gid}/entry/{uuid}/edit", checkPerm("write", appHandler(postEntry))).Methods("POST")
	r.Handle("/groups/{gid}/entry/{uuid}/delete", checkPerm("write", appHandler(confirmDeleteEntry))).Methods("GET")
	r.Handle("/groups/{gid}/entry/{uuid}/delete", checkPerm("write", appHandler(deleteEntry))).Methods("POST")
	r.Handle("/nuke", checkPerm("write", appHandler(confirmNuke))).Methods("GET")
	r.Handle("/nuke", checkPerm("write", appHandler(nuke))).Methods("POST")
	r.Handle("/_/newdb", checkPerm("write", appHandler(newDB))).Methods("POST")
	r.Handle("/_/start", checkPerm("read", appHandler(startSession))).Methods("POST")
	r.Handle("/_/pwgen", appHandler(pwgen)).Methods("GET")

	// Static files
	r.Handle("/style.css", serveStaticFile("style.css"))
	r.Handle("/fonts/Roboto-Regular.woff", serveStaticFile("third_party/roboto/Roboto-Regular.woff"))
	r.Handle("/fonts/Roboto-Bold.woff", serveStaticFile("third_party/roboto/Roboto-Bold.woff"))
	r.Handle("/js/clipboard.js", serveStaticFile("third_party/clipboard.js/dist/clipboard.min.js"))
	r.Handle("/js/editentry.js", serveStaticFile("js/editentry.js"))
	r.Handle("/js/entry.js", serveStaticFile("js/entry.js"))
	r.Handle("/js/init.js", serveStaticFile("js/init.js"))

{'fraction_non_alphanumeric': 0.19396338446313707, 'fraction_numerical': 0.003364670954972786, 'mean_word_length': 5.380050505050505, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6381803', 'n_tokens_mistral': 3413, 'n_tokens_neox': 3278, 'n_words': 661}