Exemplo n.º 1
0
func TestSetValues(t *testing.T) {
	buf := &bytes.Buffer{}
	handler := weblogs.HandlerWithOptions(
		&handler{Field1: "one", Field2: "two"},
		&weblogs.Options{Writer: buf, Logger: setLogger{}})
	handler.ServeHTTP(
		kNilResponseWriter,
		newRequest("192.168.5.1", "GET", "/foo/bar?query=tall"))
	expected := "field1=one field2=two\n"
	verifyLogs(t, expected, buf.String())
}
Exemplo n.º 2
0
func TestNormalLogs(t *testing.T) {
	buf := &bytes.Buffer{}
	clock := &clock{Time: kTime}
	handler := weblogs.HandlerWithOptions(
		&handler{Status: 321, Clock: clock, ElapsedMillis: 387},
		&weblogs.Options{Writer: buf, Now: clock.Now()})
	handler.ServeHTTP(
		kNilResponseWriter,
		newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall"))
	expected := "03/23/2013 13:14:15 192.168.5.1 GET /foo/bar?query=tall 321 387\n"
	verifyLogs(t, expected, buf.String())
}
Exemplo n.º 3
0
func TestAppendedLogs(t *testing.T) {
	buf := &bytes.Buffer{}
	clock := &clock{Time: kTime}
	handler := weblogs.HandlerWithOptions(
		&handler{Status: 321, LogExtra: "behere"},
		&weblogs.Options{Writer: buf, Now: clock.Now()})
	handler.ServeHTTP(
		kNilResponseWriter,
		newRequest("192.168.5.1", "GET", "/foo/bar?query=tall"))
	expected := "03/23/2013 13:14:15 192.168.5.1 GET /foo/bar?query=tall 321 0 behere\n"
	verifyLogs(t, expected, buf.String())
}
Exemplo n.º 4
0
func TestSupportHijack(t *testing.T) {
	// test that loggers.Capture supports hijacking
	buf := &bytes.Buffer{}
	clock := &clock{Time: kTime}
	handler := weblogs.HandlerWithOptions(
		&handler{Clock: clock, Hijack: true},
		&weblogs.Options{
			Writer: buf,
			Logger: weblogs.ApacheCommonLogger(),
			Now:    clock.Now()})
	handler.ServeHTTP(
		kNilResponseWriter,
		newRequest("192.168.101.103:4444", "GET", "/bar/baz?query=short"))
	expected := "192.168.101.103 - - [23/Mar/2013:13:14:15 +0000] \"GET /bar/baz?query=short HTTP/1.0\" 0 0\n"
	verifyLogs(t, expected, buf.String())
}
Exemplo n.º 5
0
func TestSend500OnNoOutput(t *testing.T) {
	buf := &bytes.Buffer{}
	clock := &clock{Time: kTime}
	handler := weblogs.HandlerWithOptions(
		&handler{LogExtra: "behere", Clock: clock, ElapsedMillis: 23},
		&weblogs.Options{Writer: buf, Now: clock.Now()})
	w := &spyResponseWriter{}
	handler.ServeHTTP(
		w,
		newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall"))
	expected := "03/23/2013 13:14:15 192.168.5.1 GET /foo/bar?query=tall 500 23 behere\n"
	verifyLogs(t, expected, buf.String())
	if w.Status != 500 {
		t.Errorf("Expected 500 error to be sent, but %d was sent.", w.Status)
	}
}
Exemplo n.º 6
0
func TestLogStatus200(t *testing.T) {
	buf := &bytes.Buffer{}
	clock := &clock{Time: kTime}
	handler := weblogs.HandlerWithOptions(
		&handler{Message: "1234567"},
		&weblogs.Options{
			Writer: buf,
			Logger: weblogs.ApacheCommonLogger(),
			Now:    clock.Now()})
	request := newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall")
	request.URL.User = url.User("fred")
	handler.ServeHTTP(
		kNilResponseWriter,
		request)
	expected := "192.168.5.1 - fred [23/Mar/2013:13:14:15 +0000] \"GET /foo/bar?query=tall HTTP/1.0\" 200 7\n"
	verifyLogs(t, expected, buf.String())
}
Exemplo n.º 7
0
func main() {
	flag.Parse()
	if fDb == "" {
		fmt.Println("Need to specify at least -db flag.")
		flag.Usage()
		return
	}
	setupDb(fDb)
	mux := http.NewServeMux()
	http.HandleFunc("/", rootRedirect)
	http.Handle("/static/", http.StripPrefix("/static", static.New()))
	if fIcon != "" {
		err := http_util.AddStaticFromFile(
			http.DefaultServeMux, "/images/favicon.ico", fIcon)
		if err != nil {
			fmt.Printf("Icon file not found - %s\n", fIcon)
		}
	}
	http.Handle(
		"/auth/login",
		&login.Handler{SessionStore: kSessionStore, Store: kStore})
	http.Handle(
		"/auth/poll", pollHandler{})
	http.Handle(
		"/vsafe/", &authHandler{mux})
	mux.Handle("/vsafe/chpasswd", &chpasswd.Handler{Store: kStore, Doer: kDoer})
	mux.Handle("/vsafe/home", &home.Handler{Store: kStore})
	mux.Handle("/vsafe/logout", &logout.Handler{})
	mux.Handle("/vsafe/single", &single.Handler{Store: kStore, Doer: kDoer})
	defaultHandler := context.ClearHandler(
		weblogs.HandlerWithOptions(
			http.DefaultServeMux,
			&weblogs.Options{Logger: logging.ApacheCommonLoggerWithLatency()}))
	if fSSLCrt != "" && fSSLKey != "" {
		if err := http.ListenAndServeTLS(
			fPort, fSSLCrt, fSSLKey, defaultHandler); err != nil {
			fmt.Println(err)
		}
		return
	}
	if err := http.ListenAndServe(fPort, defaultHandler); err != nil {
		fmt.Println(err)
	}
}
Exemplo n.º 8
0
func TestRaces(t *testing.T) {
	buf := &bytes.Buffer{}
	clock := &clock{Time: kTime}
	handler := weblogs.HandlerWithOptions(
		&handler{Status: 200},
		&weblogs.Options{
			Writer: buf, Logger: verySimpleLogger{}, Now: clock.Now()})
	var wg sync.WaitGroup
	wg.Add(20)
	for i := 0; i < 20; i++ {
		go func() {
			handler.ServeHTTP(
				kNilResponseWriter,
				newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall"))
			wg.Done()
		}()
	}
	wg.Wait()
}
Exemplo n.º 9
0
func TestCombinedLogs(t *testing.T) {
	buf := &bytes.Buffer{}
	clock := &clock{Time: kTime}
	handler := weblogs.HandlerWithOptions(
		&handler{Status: 321, Message: "1234567"},
		&weblogs.Options{
			Writer: buf,
			Logger: weblogs.ApacheCombinedLogger(),
			Now:    clock.Now()})
	request := newRequest("192.168.5.1:3333", "GET", "/foo/bar?query=tall")
	request.URL.User = url.User("fred")
	request.Header = make(http.Header)
	request.Header.Set("Referer", "referer")
	request.Header.Set("User-Agent", "useragent")
	handler.ServeHTTP(
		kNilResponseWriter,
		request)
	expected := "192.168.5.1 - fred [23/Mar/2013:13:14:15 +0000] \"GET /foo/bar?query=tall HTTP/1.0\" 321 7 \"referer\" \"useragent\"\n"
	verifyLogs(t, expected, buf.String())
}
Exemplo n.º 10
0
func main() {

	//SSH goroutine

	go func() {
		config := ssh.ServerConfig{
			PublicKeyCallback: keyAuth,
		}
		config.AddHostKey(hostPrivateKeySigner)

		port := strconv.Itoa(hostSSHPort)

		socket, err := net.Listen("tcp", ":"+port)
		if err != nil {
			panic(err)
		}

		for {
			conn, err := socket.Accept()
			if err != nil {
				panic(err)
			}

			// From a standard TCP connection to an encrypted SSH connection
			sshConn, chans, reqs, err := ssh.NewServerConn(conn, &config)
			if err != nil {
				log.Println("Error accepting ssh connection: ", err)
				continue
			}

			log.Println("Connection from", sshConn.RemoteAddr())

			// Print incoming out-of-band Requests
			go handleRequests(reqs)
			// Accept all channels
			go handleChannels(chans)
		}
	}()

	//

	// Garbage collecting goroutine
	// For testing, a stop-the-world gc using mutexes shall be 'nuff
	go func() {

		for {
			authRequestMap.Lock()

			for k, v := range authRequestMap.timestamps {

				killtime := time.Now().Add(-5 * time.Minute)

				if v.Before(killtime) {
					log.Debugf("Garbage collected key %s, %v old", k, time.Now().Sub(v))
					delete(authRequestMap.timestamps, k)
					delete(authRequestMap.matches, k)
				}

			}

			authRequestMap.Unlock()
			time.Sleep(2 * time.Minute)
		}

	}()

	templateStart, err = template.New("index.html").ParseFiles("index.html")
	if err != nil {
		panic(err)
	}

	templateAuth, err = template.New("auth.html").ParseFiles("auth.html")
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/auth", authRequestHandler)
	http.HandleFunc("/", startHandler)

	weblogOptions := &weblogs.Options{

		Writer: nil,
		Logger: customLogger{},
	}

	csrfHandler := nosurf.New(http.DefaultServeMux)

	csrfHandler.SetBaseCookie(http.Cookie{HttpOnly: true, Secure: sslEnabled})

	handler := context.ClearHandler(weblogs.HandlerWithOptions(csrfHandler, weblogOptions))

	if sslEnabled {
		go http.ListenAndServe(":"+strconv.Itoa(hostHTTPPort), handler)
		http.ListenAndServeTLS(":"+strconv.Itoa(hostTLSPort), sslCertPath, sslKeyPath, handler)
	} else {
		http.ListenAndServe(":"+strconv.Itoa(hostHTTPPort), handler)
	}
}