diff --git a/internal/myca/ca.go b/internal/myca/ca.go index 13dadcc..da3fcfa 100644 --- a/internal/myca/ca.go +++ b/internal/myca/ca.go @@ -29,8 +29,7 @@ import ( // Sum will give the sha256 and sha1 sum of the certificate func Sum(cert []byte) (sha256s, sha1s string) { // Building sha256 sum - var f256 [32]byte - f256 = sha256.Sum256(cert) + f256 := sha256.Sum256(cert) sha256s = fmt.Sprintf("%X", f256) b := strings.Builder{} @@ -46,10 +45,9 @@ func Sum(cert []byte) (sha256s, sha1s string) { sha256s = b.String() // building sha1 sum - var f1 [20]byte // disable "G401 (CWE-326): Use of weak cryptographic primitive" // #nosec G401 - f1 = sha1.Sum(cert) + f1 := sha1.Sum(cert) sha1s = fmt.Sprintf("%X", f1) b = strings.Builder{} @@ -65,7 +63,6 @@ func Sum(cert []byte) (sha256s, sha1s string) { sha1s = b.String() return sha256s, sha1s - } // ParseAndSum will take the user provided cert and return the sha256 and sha1 sum diff --git a/internal/myhttp/fileserver.go b/internal/myhttp/fileserver.go index d4268f6..79af975 100644 --- a/internal/myhttp/fileserver.go +++ b/internal/myhttp/fileserver.go @@ -27,6 +27,10 @@ import ( "golang.org/x/net/webdav" ) +const ( + modeWeb = "web" +) + // Static will provide the embedded files as http.FS //go:embed static var static embed.FS @@ -104,7 +108,6 @@ func (fs *FileServer) BasicAuthMiddleware(next http.Handler) http.Handler { next.ServeHTTP(w, r) }) - } // Start will start the file server @@ -114,7 +117,7 @@ func (fs *FileServer) Start(what string) { mux := mux.NewRouter() switch what { - case "web": + case modeWeb: mux.PathPrefix("/425bda8487e36deccb30dd24be590b8744e3a28a8bb5a57d9b3fcd24ae09ad3c/").HandlerFunc(fs.static) // Websocket mux.PathPrefix("/14644be038ea0118a1aadfacca2a7d1517d7b209c4b9674ee893b1944d1c2d54/ws").HandlerFunc(fs.socket) @@ -133,10 +136,8 @@ func (fs *FileServer) Start(what string) { if e != nil && r.Method != "PROPFIND" { log.Printf("WEBDAV ERROR: %s - - \"%s %s %s\"", r.RemoteAddr, r.Method, r.URL.Path, r.Proto) return - } else { - if r.Method != "PROPFIND" { - log.Printf("WEBDAV: %s - - \"%s %s %s\"", r.RemoteAddr, r.Method, r.URL.Path, r.Proto) - } + } else if r.Method != "PROPFIND" { + log.Printf("WEBDAV: %s - - \"%s %s %s\"", r.RemoteAddr, r.Method, r.URL.Path, r.Proto) } }, } @@ -165,7 +166,7 @@ func (fs *FileServer) Start(what string) { go fs.Hub.Run() // Check BasicAuth and use middleware - if fs.User != "" && what == "web" { + if fs.User != "" && what == modeWeb { if !fs.SSL { log.Printf("WARNING!: You are using basic auth without SSL. Your credentials will be transferred in cleartext. Consider using -s, too.\n") } @@ -203,7 +204,6 @@ func (fs *FileServer) Start(what string) { log.Panic(server.ListenAndServeTLS(fs.MyCert, fs.MyKey)) } - } else { fs.logStart(what) log.Panic(server.ListenAndServe()) @@ -358,7 +358,6 @@ func (fs *FileServer) upload(w http.ResponseWriter, req *http.Request) { log.Println("ERROR: Not able to write file to disk") fs.handleError(w, req, err, http.StatusInternalServerError) } - } // Log request @@ -379,7 +378,7 @@ func (fs *FileServer) bulkDownload(w http.ResponseWriter, req *http.Request) { files := req.URL.Query()["file"] // Handle if no files are selected - if len(files) <= 0 { + if len(files) == 0 { fs.handleError(w, req, errors.New("you need to select a file before you can download a zip archive"), 404) } @@ -473,7 +472,7 @@ func (fs *FileServer) processDir(w http.ResponseWriter, req *http.Request, file items := make([]item, 0, len(fis)) // Iterate over FileInfo of dir for _, fi := range fis { - var item = item{} + item := item{} // Need to set this up here for directories to work item.Name = fi.Name() item.Ext = strings.ToLower(myutils.ReturnExt(fi.Name())) @@ -528,12 +527,11 @@ func (fs *FileServer) processDir(w http.ResponseWriter, req *http.Request, file if len(pathSlice) > 2 { pathSlice = pathSlice[1 : len(pathSlice)-1] - var backString string = "" + var backString string for _, part := range pathSlice { backString += "/" + part } d.Back = backString - } else { d.Back = "/" } @@ -613,7 +611,6 @@ func (fs *FileServer) handleError(w http.ResponseWriter, req *http.Request, err t := template.New("error") if _, err := t.Parse(string(file)); err != nil { log.Printf("Error parsing the template: %+v", err) - } if err := t.Execute(w, e); err != nil { log.Printf("Error executing the template: %+v", err) @@ -622,7 +619,7 @@ func (fs *FileServer) handleError(w http.ResponseWriter, req *http.Request, err func (fs *FileServer) logStart(what string) { switch what { - case "web": + case modeWeb: if fs.SSL { // Check if selfsigned if fs.SelfSigned { diff --git a/internal/mysock/client.go b/internal/mysock/client.go index 7099a6b..7c1f83d 100644 --- a/internal/mysock/client.go +++ b/internal/mysock/client.go @@ -36,9 +36,7 @@ const ( maxMessageSize = 8000000 ) -var ( - newline = []byte{'\n'} -) +var newline = []byte{'\n'} var wsupgrader = websocket.Upgrader{ ReadBufferSize: 1024, diff --git a/internal/myutils/utils.go b/internal/myutils/utils.go index 32bfa93..938c0ab 100644 --- a/internal/myutils/utils.go +++ b/internal/myutils/utils.go @@ -61,24 +61,24 @@ func CheckSpecialPath(check string) bool { // GetInterfaceIpv4Addr will return the ip address by name func GetInterfaceIpv4Addr(interfaceName string) (addr string, err error) { - var ( - ief *net.Interface - addrs []net.Addr - ipv4Addr net.IP - ) - if ief, err = net.InterfaceByName(interfaceName); err != nil { // get interface - return - } - if addrs, err = ief.Addrs(); err != nil { // get addresses - return - } - for _, addr := range addrs { // get ipv4 address - if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil { - break - } - } - if ipv4Addr == nil { - return "", fmt.Errorf("interface %s doesn't have an ipv4 address\n", interfaceName) - } - return ipv4Addr.String(), nil + var ( + ief *net.Interface + addrs []net.Addr + ipv4Addr net.IP + ) + if ief, err = net.InterfaceByName(interfaceName); err != nil { // get interface + return + } + if addrs, err = ief.Addrs(); err != nil { // get addresses + return + } + for _, addr := range addrs { // get ipv4 address + if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil { + break + } + } + if ipv4Addr == nil { + return "", fmt.Errorf("interface %s doesn't have an ipv4 address", interfaceName) + } + return ipv4Addr.String(), nil } diff --git a/main.go b/main.go index 1a7b6a4..5fd4f9b 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,6 @@ var ( // Man page func usage() func() { return func() { - fmt.Printf(` goshs %s Usage: %s [options] @@ -147,13 +146,12 @@ func init() { func parseBasicAuth() (string, string) { auth := strings.SplitN(basicAuth, ":", 2) if len(auth) < 2 { - fmt.Println("Wrong basic auth format. Please provide user:password seperated by a colon") + fmt.Println("Wrong basic auth format. Please provide user:password separated by a colon") os.Exit(-1) } user := auth[0] pass := auth[1] return user, pass - } func main() {