Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Eclipse+Goclipse support. Cleaner folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcposch committed Nov 18, 2013
1 parent c8e3a4b commit cabec80
Show file tree
Hide file tree
Showing 24 changed files with 168 additions and 101 deletions.
5 changes: 4 additions & 1 deletion .gitignore
@@ -1,5 +1,8 @@
.*.swp
static/doc/*.html
scramble
scramble.sql
build
bin/
pkg/
.settings/
src/*.*/
17 changes: 17 additions & 0 deletions .project
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>scramble</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.googlecode.goclipse.goBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>goclipse.goNature</nature>
</natures>
</projectDescription>
19 changes: 11 additions & 8 deletions Makefile
@@ -1,16 +1,19 @@
GOPATH := $(shell pwd)

run: build
$(GOPATH)/bin/scramble
./static/bin/scramble

build: doc $(shell find . -name '*.go')
go get .
go install .
cp $(GOPATH)/bin/scramble static/bin/scramble
build: doc $(shell find . -name '*.go') $(shell find . -name '*.js')
go get scramble
mkdir -p bin
go build -o bin/scramble src/cmd/scramble/*.go
cp bin/scramble static/bin/scramble

test:
go test
test: $(shell find . -name '*.go') $(shell find . -name '*.js')
go test scramble
cat static/js/stubs.js static/js/sugar.min.js static/js/openpgp.js static/js/scrypt.js static/js/app.js static/js/test.js | node

lint:
lint:
go get github.com/golang/lint/golint
$(GOPATH)/bin/golint *.go

Expand Down
14 changes: 14 additions & 0 deletions src/cmd/scramble/main.go
@@ -0,0 +1,14 @@
package main

import (
"scramble"
)

func main() {
// HTTP Static Files + REST API
scramble.StartHTTPServer()

// SMTP Incoming Messages
scramble.StartSMTPServer()
scramble.StartSMTPSaver()
}
2 changes: 1 addition & 1 deletion auth.go → src/scramble/auth.go
@@ -1,4 +1,4 @@
package main
package scramble

import (
"errors"
Expand Down
44 changes: 32 additions & 12 deletions config.go → src/scramble/config.go
@@ -1,14 +1,14 @@
package main
package scramble

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"errors"
)

// All configuration for a Scramble server+notary.
Expand All @@ -35,16 +35,36 @@ type Config struct {
}

func validateConfig(cfg *Config) error {
if cfg.DbServer == "" { return errors.New("DbServer must be set") }
if cfg.DbUser == "" { return errors.New("DbUser must be set") }
if cfg.DbCatalog == "" { return errors.New("DbCatalog must be set") }
if cfg.SMTPMxHost == "" { return errors.New("SMTPMxHost must be set") }
if cfg.SMTPPort == 0 { return errors.New("SMTPPort must be set") }
if cfg.MaxEmailSize == 0 { return errors.New("MaxEmailSize must be set") }
if cfg.HTTPPort == 0 { return errors.New("HTTPPort must be set") }
if len(cfg.Notaries) == 0 { return errors.New("Notaries must be set") }
if len(cfg.ReservedNames) == 0 { return errors.New("ReservedNames must be set") }
if cfg.AncestorIDsMaxBytes == 0 { return errors.New("AncestorIDsMaxBytes must be set") }
if cfg.DbServer == "" {
return errors.New("DbServer must be set")
}
if cfg.DbUser == "" {
return errors.New("DbUser must be set")
}
if cfg.DbCatalog == "" {
return errors.New("DbCatalog must be set")
}
if cfg.SMTPMxHost == "" {
return errors.New("SMTPMxHost must be set")
}
if cfg.SMTPPort == 0 {
return errors.New("SMTPPort must be set")
}
if cfg.MaxEmailSize == 0 {
return errors.New("MaxEmailSize must be set")
}
if cfg.HTTPPort == 0 {
return errors.New("HTTPPort must be set")
}
if len(cfg.Notaries) == 0 {
return errors.New("Notaries must be set")
}
if len(cfg.ReservedNames) == 0 {
return errors.New("ReservedNames must be set")
}
if cfg.AncestorIDsMaxBytes == 0 {
return errors.New("AncestorIDsMaxBytes must be set")
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion crypto.go → src/scramble/crypto.go
@@ -1,4 +1,4 @@
package main
package scramble

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion crypto_test.go → src/scramble/crypto_test.go
@@ -1,4 +1,4 @@
package main
package scramble

import "testing"

Expand Down
8 changes: 4 additions & 4 deletions email.go → src/scramble/email.go
@@ -1,10 +1,10 @@
package main
package scramble

import (
"log"
"strings"
"crypto/rand"
"encoding/hex"
"log"
"strings"
)

type EmailAddress struct {
Expand Down Expand Up @@ -77,7 +77,7 @@ func ParseAngledEmailAddressesSmart(addrList string) EmailAddresses {
}
addrs := EmailAddresses{}
for _, addr := range found {
addrs = append(addrs, &EmailAddress{addr[1],addr[2]})
addrs = append(addrs, &EmailAddress{addr[1], addr[2]})
}
return addrs
}
Expand Down
47 changes: 5 additions & 42 deletions server.go → src/scramble/handlers.go
@@ -1,15 +1,14 @@
package main
package scramble

import (
"fmt"
"log"
"net/http"
"runtime/debug"
"strings"
"time"
)

func main() {
func StartHTTPServer() {
// Rest API
http.HandleFunc("/user/", userHandler) // create users, look up hash->pubkey
http.HandleFunc("/publickeys/notary", notaryHandler) // this notary & default client notaries
Expand All @@ -27,10 +26,6 @@ func main() {
// Resources
http.HandleFunc("/", staticHandler) // html, js, css

// SMTP Incoming Messages
StartSMTPServer()
StartSMTPSaver()

// Serve HTTP on localhost only. Let Nginx terminate HTTPS for us.
address := fmt.Sprintf("127.0.0.1:%d", GetConfig().HTTPPort)
log.Printf("Listening on http://%s\n", address)
Expand Down Expand Up @@ -60,7 +55,7 @@ func auth(handler func(http.ResponseWriter, *http.Request, *UserID)) http.Handle
func recoverAndLogHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Wrap the ResponseWriter to remember the status
rww := &ResponseWriterWrapper{-1, w}
rww := &responseWriterWrapper{200, w}
begin := time.Now()

defer func() {
Expand All @@ -75,9 +70,6 @@ func recoverAndLogHandler(handler http.Handler) http.Handler {

// Finally, log.
durationMS := time.Since(begin).Nanoseconds() / 1000000
if rww.Status == -1 {
rww.Status = 200
}
log.Printf("%s %s %v %v %s", r.RemoteAddr, r.Method, rww.Status, durationMS, r.URL)
}()

Expand All @@ -86,41 +78,12 @@ func recoverAndLogHandler(handler http.Handler) http.Handler {
}

// Remember the status for logging
type ResponseWriterWrapper struct {
type responseWriterWrapper struct {
Status int
http.ResponseWriter
}

func (w *ResponseWriterWrapper) WriteHeader(status int) {
func (w *responseWriterWrapper) WriteHeader(status int) {
w.Status = status
w.ResponseWriter.WriteHeader(status)
}

// Stick it as a deferred statement in gouroutines to prevent the program from crashing.
// Catches panics and emails a report to the configured AdminEmails
func Recover() {
if e := recover(); e != nil {
stack := string(debug.Stack())
errorString := fmt.Sprintf("%s:\n%s", e, stack)
log.Println("<!> " + errorString)
messageID := GenerateMessageID().String()
if len(GetConfig().AdminEmails) > 0 {
go smtpSendSafe(&OutgoingEmail{
Email: Email{
EmailHeader: EmailHeader{
MessageID: messageID,
ThreadID: messageID,
UnixTime: time.Now().Unix(),
From: "daemon@" + GetConfig().SMTPMxHost,
To: strings.Join(GetConfig().AdminEmails, ","),
},
},
IsPlaintext: true,
PlaintextSubject: "Panic from Scramble server " + GetConfig().SMTPMxHost,
PlaintextBody: errorString,
})
} else {
log.Printf("Set AdminEmails: ['your_email@host',...] in config to receive alerts")
}
}
}
16 changes: 1 addition & 15 deletions handlers.go → src/scramble/handlers_rest.go
@@ -1,4 +1,4 @@
package main
package scramble

import (
"encoding/json"
Expand All @@ -15,20 +15,6 @@ import (
//"github.com/jaekwon/go-prelude/colors"
)

//
// SERVE HTML, CSS, JS
//

func staticHandler(w http.ResponseWriter, r *http.Request) {
var path string
if strings.HasSuffix(r.URL.Path, "/") {
path = r.URL.Path + "index.html"
} else {
path = r.URL.Path
}
http.ServeFile(w, r, "static/"+path)
}

//
// USER ROUTE
//
Expand Down
20 changes: 20 additions & 0 deletions src/scramble/handlers_static.go
@@ -0,0 +1,20 @@
package scramble

import (
"net/http"
"strings"
)

//
// SERVE HTML, CSS, JS
//

func staticHandler(w http.ResponseWriter, r *http.Request) {
var path string
if strings.HasSuffix(r.URL.Path, "/") {
path = r.URL.Path + "index.html"
} else {
path = r.URL.Path
}
http.ServeFile(w, r, "static/"+path)
}
4 changes: 2 additions & 2 deletions handlers_test.go → src/scramble/handlers_test.go
@@ -1,4 +1,4 @@
package main
package scramble

import (
"encoding/json"
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestPublicKeysHandler(t *testing.T) {
"POST", "publickeys/query",
url.Values{
"needResolution": {tUser.EmailAddress},
"needPubKey": {tUser.EmailAddress+",doesnotexist@"+GetConfig().SMTPMxHost},
"needPubKey": {tUser.EmailAddress + ",doesnotexist@" + GetConfig().SMTPMxHost},
"notaries": {GetConfig().SMTPMxHost},
},
)
Expand Down
2 changes: 1 addition & 1 deletion migrations.go → src/scramble/migrations.go
@@ -1,4 +1,4 @@
package main
package scramble

import (
"database/sql"
Expand Down
2 changes: 1 addition & 1 deletion models.go → src/scramble/models.go
@@ -1,4 +1,4 @@
package main
package scramble

// User represents a single user, email address, and key pair
// The email address is <public key hash>@<host>
Expand Down
5 changes: 4 additions & 1 deletion notary.go → src/scramble/notary.go
@@ -1,6 +1,6 @@
// See: https://github.com/dcposch/scramble/wiki/Addr-Resolution-via-Notaries

package main
package scramble

import (
"code.google.com/p/go.crypto/openpgp"
Expand Down Expand Up @@ -86,6 +86,9 @@ func loadNotaries() {
notaryHosts := []string{}
for notaryHost, filename := range notaryFiles {
pubKeyBytes, err := ioutil.ReadFile(filename)
if err != nil {
pubKeyBytes, err = ioutil.ReadFile(os.Getenv("GOPATH") + "/" + filename)
}
if err != nil {
panic(err)
}
Expand Down
38 changes: 38 additions & 0 deletions src/scramble/recover.go
@@ -0,0 +1,38 @@
package scramble

import (
"fmt"
"log"
"runtime/debug"
"strings"
"time"
)

// Stick it as a deferred statement in gouroutines to prevent the program from crashing.
// Catches panics and emails a report to the configured AdminEmails
func Recover() {
if e := recover(); e != nil {
stack := string(debug.Stack())
errorString := fmt.Sprintf("%s:\n%s", e, stack)
log.Println("<!> " + errorString)
messageID := GenerateMessageID().String()
if len(GetConfig().AdminEmails) > 0 {
go smtpSendSafe(&OutgoingEmail{
Email: Email{
EmailHeader: EmailHeader{
MessageID: messageID,
ThreadID: messageID,
UnixTime: time.Now().Unix(),
From: "daemon@" + GetConfig().SMTPMxHost,
To: strings.Join(GetConfig().AdminEmails, ","),
},
},
IsPlaintext: true,
PlaintextSubject: "Panic from Scramble server " + GetConfig().SMTPMxHost,
PlaintextBody: errorString,
})
} else {
log.Printf("Set AdminEmails: ['your_email@host',...] in config to receive alerts")
}
}
}

0 comments on commit cabec80

Please sign in to comment.