Skip to content

Commit

Permalink
Config path (#13)
Browse files Browse the repository at this point in the history
* build:add scripts

* fix:certificate path
  • Loading branch information
nsavinda authored Jun 22, 2024
1 parent 47b3c43 commit db36635
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
33 changes: 18 additions & 15 deletions certgen/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"net"
"os"
Expand All @@ -23,38 +22,39 @@ import (
)

// file location by os
func getConfigFilePath() string {
func GetConfigDir() string {
var configDir string
var configFileName string

switch OS := runtime.GOOS; OS {
case "windows":
// On Windows, use the APPDATA directory
configDir = filepath.Join(os.Getenv("APPDATA"), "ServeIt")
configFileName = "config.yaml"
configDir = filepath.Join(os.Getenv("APPDATA"), "ServeFiles")
case "darwin":
// On macOS, use the home directory
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configDir = filepath.Join(home, "Library", "Application Support", "ServeIt")
configFileName = "config.yaml"
configDir = filepath.Join(home, "Library", "Application Support", "ServeFiles")
default:
// On Unix-like systems, use the home directory
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configDir = filepath.Join(home, ".ServeIt")
configFileName = "config.yaml"
configDir = filepath.Join(home, ".ServeFiles")
}

// Create configuration directory if it doesn't exist
if _, err := os.Stat(configDir); os.IsNotExist(err) {
os.MkdirAll(configDir, 0755)
}

return configDir
}

func getConfigFilePath() string {
configDir := GetConfigDir()
configFileName := "config.yaml"
return filepath.Join(configDir, configFileName)
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func loadConfig() (Config, error) {
return config, err
}
}
fmt.Println(config)
// fmt.Println(config)
return config, nil
}

Expand Down Expand Up @@ -208,10 +208,13 @@ func Certsetup() (serverTLSConf *tls.Config, clientTLSConf *tls.Config, err erro
}

// create directory if it does not exist
os.Mkdir("cert", 0755)

os.WriteFile("cert/cert.pem", certPEM.Bytes(), 0644)
os.WriteFile("cert/key.pem", certPrivKeyPEM.Bytes(), 0644)
// os.Mkdir("cert", 0755)
os.Mkdir(GetConfigDir()+"/cert", 0755)
// fmt.Println(GetConfigDir())
// os.WriteFile("cert/cert.pem", certPEM.Bytes(), 0644)
// os.WriteFile("cert/key.pem", certPrivKeyPEM.Bytes(), 0644)
os.WriteFile(GetConfigDir()+"/cert/cert.pem", certPEM.Bytes(), 0644)
os.WriteFile(GetConfigDir()+"/cert/key.pem", certPrivKeyPEM.Bytes(), 0644)

return
}
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ module github.com/Elixir-Craft/servefiles

go 1.22.3

require gopkg.in/yaml.v2 v2.4.0 // indirect
require (
github.com/fatih/color v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
38 changes: 21 additions & 17 deletions servefiles/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"path/filepath"

"github.com/fatih/color"

"github.com/Elixir-Craft/servefiles/localip"

"github.com/Elixir-Craft/servefiles/certgen"
Expand All @@ -18,8 +20,8 @@ import (
)

var (
CertFilePath = "cert/cert.pem"
KeyFilePath = "cert/key.pem"
CertFilePath = certgen.GetConfigDir() + "/cert/cert.pem"
KeyFilePath = certgen.GetConfigDir() + "/cert/key.pem"
HomeTemplate = template.Must(template.New("home.html").Parse(webtemplates.Home))
IndexTemplate = template.Must(template.New("index.html").Parse(webtemplates.Index))
AuthTemplate = template.Must(template.New("").Parse(webtemplates.Auth))
Expand Down Expand Up @@ -142,23 +144,22 @@ func passwordProtected(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Check for a session or a simple cookie to verify if the password was entered correctly
if cookie, err := r.Cookie("password"); err != nil || !checkPassword(cookie.Value) {
if cookie, err := r.Cookie("password"); err != nil || !checkPassword(cookie.Value) {
if r.Method == "POST" && checkPassword(r.FormValue("password")) {
// Set a simple cookie for demonstration purposes (not secure for production)
http.SetCookie(w, &http.Cookie{
Name: "password",
Value: r.FormValue("password"),
Path: "/",
MaxAge: 300, // Expires after 300 seconds
})
http.Redirect(w, r, r.URL.Path, http.StatusFound)
return
}
servePasswordPrompt(w, r)
if r.Method == "POST" && checkPassword(r.FormValue("password")) {
// Set a simple cookie for demonstration purposes (not secure for production)
http.SetCookie(w, &http.Cookie{
Name: "password",
Value: r.FormValue("password"),
Path: "/",
MaxAge: 3600, // Expires after 3600 seconds
})
http.Redirect(w, r, r.URL.Path, http.StatusFound)
return
}
next(w, r)
servePasswordPrompt(w, r)
return
}
next(w, r)

}
}

Expand All @@ -175,6 +176,7 @@ func servePasswordPrompt(w http.ResponseWriter, r *http.Request) {
}

func checkPassword(enteredPassword string) bool {
fmt.Println("Password check")
return enteredPassword == *password
}

Expand Down Expand Up @@ -227,8 +229,10 @@ func main() {

fmt.Println("Server is running on:")
for _, ip := range ips {
fmt.Printf(" https://%s:%s\n", ip, *port)
// fmt.Printf(" https://%s:%s\n", ip, *port)
color.Green("https://%s:%s\n", ip, *port)
}
fmt.Printf("Press Ctrl+C to stop the server\n\n")

defer server.Close()
log.Fatal(server.ListenAndServeTLS("", ""))
Expand Down

0 comments on commit db36635

Please sign in to comment.