Beispiel #1
0
func main() {
	// Here we have 3 levels of handlers:
	// 1 - session handler
	// 2 - auth handler
	// 3 - file server
	//
	// When a request comes in, first it goes through the session
	// handler, which deals with decrypting and unpacking session
	// data coming in as cookies on incoming requests, and making
	// sure the session is serialized when the response header is
	// written.  After deserializing the incoming session, the
	// request is passed to AuthHandler (defined above).
	// AuthHandler directly serves requests for /login, /logout,
	// and /session.  Requests for any other resource require the
	// session map to have a user key, which is obtained by
	// logging in.  If the user key is present, the request is
	// passed to the FileServer, otherwise the browser is
	// redirected to the login page.
	handler := seshcookie.NewSessionHandler(
		&AuthHandler{http.FileServer(contentDir), userDb},
		"session key, preferably a sequence of data from /dev/urandom",
		nil)

	if err := http.ListenAndServe(":8080", handler); err != nil {
		log.Printf("ListenAndServe:", err)
	}
}
Beispiel #2
0
func CreateRoutes() bool {
	key := make([]byte, 100)
	f, _ := os.Open("/dev/urandom")
	_, _ = f.Read(key)
	keyString := string(key)
	http.Handle("/api/v1/game", seshcookie.NewSessionHandler(
		&RootHandler{},
		keyString,
		nil))
	http.Handle("/api/v1/play", seshcookie.NewSessionHandler(
		&PlayHandler{},
		keyString,
		nil))

	return true

}
func RunTranslator(host string, debug int) {
	hostname = host
	model.Debug = debug

	fmt.Println("Starting web server:", hostname)

	handler := http.NewServeMux()
	handler.HandleFunc("/home", control.DashboardHandler)
	handler.HandleFunc("/sources", control.SourcesHandler)
	handler.HandleFunc("/entries", control.EntriesHandler)
	handler.HandleFunc("/translate", control.TranslationHandler)
	handler.HandleFunc("/import", control.ImportHandler)
	handler.HandleFunc("/import/progress", control.ImportProgressHandler)
	handler.HandleFunc("/import/abort", control.ImportAbortHandler)
	handler.HandleFunc("/export", control.ExportHandler)
	handler.HandleFunc("/live-export", control.LiveExportHandler)
	handler.HandleFunc("/users", control.UsersHandler)
	handler.HandleFunc("/users/add", control.UsersAddHandler)
	handler.HandleFunc("/users/del", control.UsersDelHandler)
	handler.HandleFunc("/users/masq", control.UsersMasqueradeHandler)
	handler.HandleFunc("/account", control.AccountHandler)
	handler.HandleFunc("/account/password", control.SetPasswordHandler)
	handler.HandleFunc("/account/reclaim", control.AccountReclaimHandler)

	handler.HandleFunc("/api/setlead", control.APISetLeadHandler)
	handler.HandleFunc("/api/clearlead", control.APIClearLeadHandler)
	handler.HandleFunc("/api/entries", control.APIEntriesHandler)
	handler.HandleFunc("/api/translate", control.APITranslateHandler)
	handler.HandleFunc("/api/vote", control.APIVoteHandler)

	handler.Handle("/css/", http.FileServer(http.Dir("web")))
	handler.Handle("/bootstrap/", http.FileServer(http.Dir("web")))
	handler.Handle("/images/", http.FileServer(http.Dir("web")))
	handler.Handle("/js/", http.FileServer(http.Dir("web")))

	handler.Handle("/pdf/", http.FileServer(http.Dir(config.Config.PDF.Path)))

	handler.HandleFunc("/", defaultHandler)

	authHandler := AuthHandler{handler}
	sessionHandler := seshcookie.NewSessionHandler(&authHandler, SESSIONKEY, nil)

	if err := http.ListenAndServe(":9091", sessionHandler); err != nil {
		fmt.Printf("Error in ListenAndServe:", err)
	}

	fmt.Println("Done.")
}
Beispiel #4
0
func main() {
	var err error

	// if -memprof or -cpuprof haven't been set on the command
	// line, these are nops
	startProfiling()
	defer stopProfiling()

	rootHandler := seshcookie.NewSessionHandler(
		&AuthHandler{
			&statusHandler{http.FileServer(http.Dir("./static"))},
			&authorizer{"."},
			&decider{},
		},
		cookieKey,
		nil)
	rootHandler.CookieName = cookieName

	// TODO: loop and do stuff
	http.Handle("/", rootHandler)
	http.Handle("/err/", http.FileServer(http.Dir("./err")))

	if devMode {
		err = http.ListenAndServe(
			":8080",
			nil)
	} else {
		go func() {
			mux := http.NewServeMux()
			mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
				http.Redirect(rw, r, "https://boosd.org/", 302)
			})
			http.ListenAndServe(":80", mux)
		}()
		// if we're serving over https, set the secure flag
		// for cookies
		seshcookie.Session.Secure = true
		err = http.ListenAndServeTLS(
			":443",
			"/home/bpowers/.tls/certchain.pem",
			"/home/bpowers/.tls/boosd.org_key.pem",
			nil)
	}
	if err != nil {
		log.Printf("ListenAndServe:", err)
	}

}
func main() {
	fmt.Println("Starting web server")

	handler := http.NewServeMux()
	handler.HandleFunc("/home", control.DashboardHandler)
	handler.HandleFunc("/sources", control.SourcesHandler)
	handler.HandleFunc("/entries", control.EntriesHandler)
	handler.HandleFunc("/translate", control.TranslationHandler)
	handler.HandleFunc("/import", control.ImportHandler)
	handler.HandleFunc("/import/done", control.ImportDoneHandler)
	handler.HandleFunc("/export", control.ExportHandler)
	handler.HandleFunc("/users", control.UsersHandler)
	handler.HandleFunc("/users/add", control.UsersAddHandler)
	handler.HandleFunc("/users/del", control.UsersDelHandler)
	handler.HandleFunc("/account", control.AccountHandler)
	handler.HandleFunc("/account/password", control.SetPasswordHandler)
	handler.HandleFunc("/account/reclaim", control.AccountReclaimHandler)

	handler.HandleFunc("/api/setlead", control.APISetLeadHandler)
	handler.HandleFunc("/api/clearlead", control.APIClearLeadHandler)
	handler.HandleFunc("/api/entries", control.APIEntriesHandler)
	handler.HandleFunc("/api/translate", control.APITranslateHandler)
	handler.HandleFunc("/api/vote", control.APIVoteHandler)

	handler.Handle("/css/", http.FileServer(http.Dir("../web")))
	handler.Handle("/bootstrap/", http.FileServer(http.Dir("../web")))
	handler.Handle("/images/", http.FileServer(http.Dir("../web")))
	handler.Handle("/js/", http.FileServer(http.Dir("../web")))

	handler.Handle("/pdf/", http.FileServer(http.Dir("../../Composer 2.1.3/public")))

	handler.HandleFunc("/", defaultHandler)

	authHandler := AuthHandler{handler}
	sessionHandler := seshcookie.NewSessionHandler(&authHandler, SESSIONKEY, nil)

	if err := http.ListenAndServe(":9091", sessionHandler); err != nil {
		fmt.Printf("Error in ListenAndServe:", err)
	}

	fmt.Println("Done.")
}
Beispiel #6
0
func main() {
	// give us as much parallelism as possible
	runtime.GOMAXPROCS(runtime.NumCPU())

	devMode := flag.Bool("dev", false, "run on port 8080, rather than 8443")
	flag.Parse()

	conf, err := conf.ReadConfigFile("config")
	if err != nil {
		log.Printf("reading config failed: %s\n", err)
		return
	}
	key, err := conf.GetString("", "key")
	if err != nil {
		log.Printf("reading config.key failed: %s", err)
		return
	}
	name, err := conf.GetString("", "cookie-name")
	if err != nil {
		log.Printf("reading config.cookie-name failed: %s", err)
		return
	}
	musicDir, err := conf.GetString("", "music-dir")
	if err != nil {
		log.Printf("reading config.models-dir failed: %s", err)
		return
	}
	if musicDir[:2] == "~/" {
		musicDir = path.Join(os.Getenv("HOME"), musicDir[2:])
	}

	log.Printf("using music in %s\n", musicDir)

	rootHandler := seshcookie.NewSessionHandler(
		&AuthHandler{
			http.FileServer(http.Dir("./static")),
			&authorizer{musicDir},
			&decider{},
		},
		key,
		nil)
	rootHandler.CookieName = name

	http.Handle("/", rootHandler)
	http.Handle("/err/", http.FileServer(http.Dir("./err")))
	http.Handle("/music/", http.StripPrefix("/music",
		http.FileServer(http.Dir(musicDir))))
	http.HandleFunc("/api/", proxyToCnote)
	http.HandleFunc("/pkg/", proxyToGodoc)
	http.HandleFunc("/doc/", proxyToGodoc)

	watch, err := NewDirwatch(musicDir)
	if err != nil {
		log.Printf("NewDirwatch: %r\n", err)
		return
	}
	_ = watch

	if *devMode {
		err = http.ListenAndServe(
			"127.0.0.1:8080",
			nil)
	} else {
		go func() {
			mux := http.NewServeMux()
			mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
				http.Redirect(rw, r, "https://lightswitchrave.net/", 302)
			})
			http.ListenAndServe(":8080", mux)
		}()
		// if we're serving over https, set the secure flag
		// for cookies
		seshcookie.Session.Secure = true
		err = http.ListenAndServeTLS(
			":8443",
			"/home/bpowers/.tls/certchain.pem",
			"/home/bpowers/.tls/boosd.org_key.pem",
			nil)
	}
	if err != nil {
		log.Printf("ListenAndServe:", err)
	}
}
Beispiel #7
0
func WithSession(handler http.HandlerFunc) http.HandlerFunc {
	return seshcookie.NewSessionHandler(http.HandlerFunc(handler), sessionKey, nil).ServeHTTP
}