diff options
| -rw-r--r-- | .gitignore | 6 | ||||
| -rw-r--r-- | a_test.go | 3 | ||||
| -rw-r--r-- | domain.go | 10 | ||||
| -rw-r--r-- | domain_test.go | 17 | ||||
| -rw-r--r-- | handler.go | 78 | ||||
| -rw-r--r-- | main.go | 9 | ||||
| -rw-r--r-- | main_test.go | 3 | ||||
| -rw-r--r-- | server.go | 9 | ||||
| -rw-r--r-- | server_test.go | 3 | ||||
| -rw-r--r-- | struct.go | 12 | ||||
| -rw-r--r-- | utilities.go | 14 | ||||
| -rw-r--r-- | utilities_test.go | 3 |
12 files changed, 145 insertions, 22 deletions
@@ -1,5 +1,5 @@ -test-results/ -tmp/ -routes/ *.swp *.db +*tar.gz +start.sh +_env.sh @@ -3,9 +3,12 @@ package main import ( + "flag" "testing" ) +var testExternal = flag.Bool("external", false, "allow use of external networks during long test") + func TestMain(t *testing.T) { InitDB() } @@ -3,6 +3,7 @@ package main import ( "log" "net" + "reflect" "strings" ) @@ -31,7 +32,7 @@ func (vD VirtualDomain) CreateDomain() bool { return true } -func (vD VirtualDomain) ValidateDomain() bool { +func (vD VirtualDomain) ValidateDomain(ref string) bool { if vD.Name == "" { return false } @@ -43,7 +44,12 @@ func (vD VirtualDomain) ValidateDomain() bool { if len(addr) == 0 { return false } - return true + serverIP, err := net.LookupIP(ref) + if err != nil { + log.Println(err) + return false + } + return reflect.DeepEqual(serverIP, addr) } func (vD VirtualDomain) ValidateDomainMX(ref string) bool { if vD.Name == "" { diff --git a/domain_test.go b/domain_test.go index 456d301..20d927a 100644 --- a/domain_test.go +++ b/domain_test.go @@ -1,13 +1,10 @@ package main import ( - "flag" "os" "testing" ) -var testExternal = flag.Bool("external", false, "allow use of external networks during long test") - func TestCreateDomain(t *testing.T) { d := VirtualDomain{Name: "example.org"} if !d.CreateDomain() { @@ -27,20 +24,26 @@ func TestDomainExists(t *testing.T) { } func TestValidateDomain(t *testing.T) { + if testing.Short() || !*testExternal { + t.Skip("Skipping test to avoid external network.") + } d := VirtualDomain{Name: "blablabla"} - if d.ValidateDomain() { + if d.ValidateDomain("blablabla") { t.Fatal(d.Name + " is not a valid domain.") } d.Name = "dfalsdf.adfjalf.example.org" - if d.ValidateDomain() { + if d.ValidateDomain("dfalsdf.adfjalf.example.org") { t.Fatal("'" + d.Name + "' is not a valid domain.") } d.Name = "https://google.com" - if d.ValidateDomain() { + if d.ValidateDomain("https://google.com") { t.Fatal("'" + d.Name + "' is not a valid domain.") } d.Name = "google.com" - if !d.ValidateDomain() { + if d.ValidateDomain("wikipedia.de") { + t.Fatal("'" + d.Name + "' and wikipedia.de do not share IPs.") + } + if !d.ValidateDomain("google.com") { t.Fatal("'" + d.Name + "' is a valid domain.") } } diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..e708a20 --- /dev/null +++ b/handler.go @@ -0,0 +1,78 @@ +package main + +import ( + "log" + "net/http" +) + +func IndexHandler(w http.ResponseWriter, r *http.Request) { + session, err := store.Get(r, "_SID") + if err != nil { + log.Println(err) + } + + flash := Flash{} + flash.Error = session.Flashes("error") + flash.Success = session.Flashes("success") + + index := mainTempl.Lookup("index.html") + + err = index.ExecuteTemplate(w, "index.html", flash) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + session.Save(r, w) +} + +func CreateNewEntryHandler(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + log.Panic(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + req := Request{} + err = decoder.Decode(&req, r.PostForm) + if err != nil { + log.Panic(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + session, err := store.Get(r, "_SID") + if err != nil { + log.Println(err) + } + + if !CompareStrings(req.Email, req.ConfirmEmail) { + session.AddFlash("E-Mail don't match.", "error") + session.Save(r, w) + http.Redirect(w, r, "/", 302) + return + } + + if !CompareStrings(req.Password, req.ConfirmPassword) { + session.AddFlash("Passwords don't match.", "error") + session.Save(r, w) + http.Redirect(w, r, "/", 302) + return + } + + req.Password = Md5Hash(req.Password) + + err = CreateNewEntry(req.Email, req.Password) + if err != nil { + session.AddFlash(err, "error") + session.Save(r, w) + http.Redirect(w, r, "/", 302) + return + } + + session.AddFlash("Success! You can login now with your new mail account.") + session.Save(r, w) + http.Redirect(w, r, "/", 302) +} @@ -4,7 +4,6 @@ import ( "github.com/gorilla/mux" "github.com/gorilla/schema" "github.com/gorilla/sessions" - "github.com/robfig/cron" "html/template" "log" "net/http" @@ -13,14 +12,11 @@ import ( var decoder = schema.NewDecoder() -//var store = sessions.NewCookieStore([]byte(RandomKey())) var store = sessions.NewCookieStore([]byte(os.Getenv("FREEMAIL_SECRET"))) var mainTempl = template.Must(template.New("global").Funcs(template.FuncMap{"add": add}).ParseGlob("./views/*.html")) var emailTempl = template.Must(template.New("email").Funcs(template.FuncMap{"add": add}).ParseGlob("./views/email/*.html")) -var c = cron.New() - func add(x, y int) int { return x + y } @@ -36,8 +32,9 @@ func main() { InitDB() r := mux.NewRouter() - // r.HandleFunc("/", IndexHandler) - r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./../static")))) + r.HandleFunc("/", IndexHandler) + r.HandleFunc("/create", CreateNewEntryHandler).Methods("POST") + r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) http.Handle("/", r) diff --git a/main_test.go b/main_test.go index dfb296e..46cdf44 100644 --- a/main_test.go +++ b/main_test.go @@ -6,8 +6,11 @@ package main import ( "os" "testing" + "flag" ) +var testExternal = flag.Bool("external", false, "allow use of external networks during long test") + func TestMain(m *testing.M) { InitDB() os.Exit(m.Run()) @@ -19,10 +19,11 @@ func CreateNewEntry(email, password string) error { vD := VirtualDomain{} vD.Name = GetDomain(vU.Email) - if !vD.ValidateDomain() { - if !vD.ValidateDomainMX(os.Getenv("FREEMAIL_SMTP_MAILER_MX")) { - return errors.New("This doesn't look like a good domain. Host not found.") - } + if !vD.ValidateDomainMX(os.Getenv("FREEMAIL_SMTP_MAILER_MX")) { + return errors.New("The MX record doesn't point to this server.") + } + if !vD.ValidateDomain(os.Getenv("FREEMAIL_SMTP_MAILER_MX")) { + return errors.New("This doesn't look like a good domain. Host not found.") } if !vD.DomainExists() { diff --git a/server_test.go b/server_test.go index 209e3c4..57d96c3 100644 --- a/server_test.go +++ b/server_test.go @@ -5,6 +5,9 @@ import ( ) func TestCreateNewEntry(t *testing.T) { + if testing.Short() || !*testExternal { + t.Skip("Skipping test to avoid external network.") + } err := CreateNewEntry("foobar@example.org", "password") if err != nil { t.Fatal(err) @@ -40,3 +40,15 @@ type VirtualAliase struct { Source string Destination string } + +type Request struct { + Email string + ConfirmEmail string + Password string + ConfirmPassword string +} + +type Flash struct { + Success []interface{} + Error []interface{} +} diff --git a/utilities.go b/utilities.go index 60423dc..199a8b9 100644 --- a/utilities.go +++ b/utilities.go @@ -26,3 +26,17 @@ func CompareStrings(strs ...string) bool { } return true } + +// Checks for equality in two slices from typ IP +/* +func CompareIPs(a, b []IP) bool { + if len(a) != len(b) { + return false + } + for k := range a { + if a[k] != b[i] { + return false + } + } +} +*/ diff --git a/utilities_test.go b/utilities_test.go index 61d0b27..6e4d31c 100644 --- a/utilities_test.go +++ b/utilities_test.go @@ -23,3 +23,6 @@ func TestCompareStrings(t *testing.T) { t.Fatal("Strings are not equal. Exptected false") } } + +func TestCompareIPs(t *testing.T) { +} |
