func Upload(w http.ResponseWriter, req *http.Request) { req.ParseMultipartForm(32 << 20) uploadedFile, handler, err := req.FormFile("file") if log.PrintError("frontend upload", err) != nil { fmt.Fprintf(w, "ERROR") return } defer uploadedFile.Close() err = os.MkdirAll(viper.GetString("upload"), 0700) if log.PrintError("frontend upload", err) != nil { fmt.Fprintf(w, "ERROR") return } serverFile, err := os.OpenFile( viper.GetString("upload")+handler.Filename, os.O_WRONLY|os.O_CREATE, 0600, ) if log.PrintError("frontend upload", err) != nil { fmt.Fprintf(w, "ERROR") return } defer serverFile.Close() if !Check(uploadedFile) { fmt.Fprintf(w, "ERROR") return } // io.Copy(serverFile, uploadedFile) fmt.Fprintf(w, "OK") }
func Start(mux *http.ServeMux, address, port, cert, key string) { listen := fmt.Sprintf("%s:%s", address, port) if cert == "" || key == "" { fmt.Printf("About to listen on http://%s/\n", listen) err := http.ListenAndServe(listen, mux) log.PrintError("serving frontend", err) } else { fmt.Printf("About to listen on https://%s/\n", listen) err := http.ListenAndServeTLS(listen, cert, key, mux) log.PrintError("serving frontend", err) } }
func NewPair(name, comment, email string) (*openpgp.Entity, error) { entity, err := openpgp.NewEntity(name, comment, email, nil) if err != nil { log.PrintError("telem.crypto.NewPair", err) return nil, err } // Sign all the identities for _, id := range entity.Identities { err := id.SelfSignature.SignUserId(id.UserId.Id, entity.PrimaryKey, entity.PrivateKey, nil) if err != nil { log.PrintError("telem.crypto.NewPair", err) return nil, err } } return entity, nil }
func Private(entity *openpgp.Entity) (string, error) { var output bytes.Buffer writeBuffer := bufio.NewWriter(&output) private, err := armor.Encode(writeBuffer, openpgp.PrivateKeyType, nil) if err != nil { log.PrintError("telem.crypto.Private", err) return "", err } entity.SerializePrivate(private, nil) private.Close() writeBuffer.Flush() return output.String(), nil }
func Gen() { pair, err := NewPair( viper.GetString("name"), viper.GetString("comment"), viper.GetString("email"), ) if log.PrintError("crypto", err) != nil { return } public, err := Public(pair) if log.PrintError("crypto", err) != nil { return } private, err := Private(pair) if log.PrintError("crypto", err) != nil { return } if viper.GetBool("stdout") { fmt.Println(public) fmt.Println(private) } if viper.GetString("path") != "" { err := os.MkdirAll(viper.GetString("path"), 0700) if log.PrintError("crypto", err) != nil { return } err = ioutil.WriteFile( viper.GetString("path")+"public.pgp", []byte(public), 0600, ) if log.PrintError("crypto", err) != nil { return } err = ioutil.WriteFile( viper.GetString("path")+"private.pgp", []byte(private), 0600, ) if log.PrintError("crypto", err) != nil { return } } }