Permalink
Browse files

go 1 / style fixes

  • Loading branch information...
1 parent d15185a commit 9b5bacb45734ea0d4c2bb25c1b137e42faca422e @bradfitz committed Mar 16, 2012
Showing with 18 additions and 23 deletions.
  1. +0 −8 server/Makefile
  2. +18 −15 server/garage-server.go
View
@@ -1,8 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=garage-server
-GOFILES=\
- garage-server.go\
-
-include $(GOROOT)/src/Make.cmd
-
View
@@ -6,42 +6,45 @@ package main
import (
"crypto/hmac"
- "exec"
+ "crypto/sha1"
"flag"
"fmt"
- "http"
+ "net/http"
"os"
+ "os/exec"
"strconv"
"sync"
"time"
)
-var listen *string = flag.String("listen", "0.0.0.0:8081", "host:port to listen on")
-var x10Unit *string = flag.String("x10unit", "f9", "X10 unit to toggle.")
-var heyUPath *string = flag.String(
- "heyupath", "/usr/local/bin/heyu", "Path to heyu binary")
+var (
+ listen = flag.String("listen", "0.0.0.0:8081", "host:port to listen on")
+ x10Unit = flag.String("x10unit", "f9", "X10 unit to toggle.")
+ heyUPath = flag.String(
+ "heyupath", "/usr/local/bin/heyu", "Path to heyu binary")
+)
var sharedSecret string
-var lastOpenTime = int64(0)
+var lastOpenTime time.Time
var lastOpenMutex sync.Mutex
-func GarageOpenError(conn http.ResponseWriter, err os.Error) {
+func GarageOpenError(conn http.ResponseWriter, err error) {
fmt.Println("Error opening garage: ", err)
conn.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(conn, "Error opening garage: %v", err)
}
func HandleGarage(conn http.ResponseWriter, req *http.Request) {
timeString := req.FormValue("t")
- requestTime, err := strconv.Atoi64(timeString)
+ requestTime, err := strconv.ParseInt(timeString, 10, 64)
if len(timeString) == 0 || err != nil {
conn.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(conn, "Missing/bogus 't' query parameter.")
return
}
- if requestTime < time.Seconds()-60 {
+ if time.Unix(requestTime, 0).Before(time.Now().Add(-60 * time.Second)) {
conn.WriteHeader(http.StatusForbidden)
fmt.Fprintf(conn, "Request too old.")
return
@@ -54,9 +57,9 @@ func HandleGarage(conn http.ResponseWriter, req *http.Request) {
return
}
- secretHasher := hmac.NewSHA1([]byte(sharedSecret))
+ secretHasher := hmac.New(sha1.New, []byte(sharedSecret))
fmt.Fprint(secretHasher, timeString)
- expectedHash := fmt.Sprintf("%40x", secretHasher.Sum())
+ expectedHash := fmt.Sprintf("%40x", secretHasher.Sum(nil))
if key != expectedHash {
conn.WriteHeader(http.StatusForbidden)
@@ -66,8 +69,8 @@ func HandleGarage(conn http.ResponseWriter, req *http.Request) {
lastOpenMutex.Lock()
defer lastOpenMutex.Unlock()
- now := time.Seconds()
- if lastOpenTime > now-10 {
+ now := time.Now()
+ if lastOpenTime.After(now.Add(10 * time.Second)) {
conn.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(conn, "Too soon, considering this a dup.")
return
@@ -83,7 +86,7 @@ func HandleGarage(conn http.ResponseWriter, req *http.Request) {
fmt.Fprint(conn, "Opened.")
fmt.Printf("Garage opened at %v from %v\n",
- time.LocalTime(),
+ time.Now(),
req.RemoteAddr)
}

0 comments on commit 9b5bacb

Please sign in to comment.