@@ -2,12 +2,11 @@ package config
22
33import (
44 "encoding/json"
5- "fmt"
65 "os"
76 "path/filepath"
8- "strings"
97
108 "github.com/patrickhener/goshs/logger"
9+ "github.com/patrickhener/goshs/options"
1110)
1211
1312type Config struct {
@@ -61,19 +60,76 @@ type Config struct {
6160 SMTPDomain string `json:"smtp_domain"`
6261}
6362
64- func Load ( configpath string ) (Config , error ) {
63+ func LoadConfig ( opts * options. Options ) (* options. Options , error ) {
6564 var cfg Config
6665
67- cfile , err := os . ReadFile ( configpath )
66+ absPath , err := filepath . Abs ( opts . ConfigFile )
6867 if err != nil {
69- return Config {}, err
68+ logger .Fatalf ("Failed to get absolute path of config file: %+v" , err )
69+ return opts , err
70+ }
71+ logger .Infof ("Using config file %s" , absPath )
72+ opts .ConfigPath = absPath
73+
74+ cfile , err := os .ReadFile (absPath )
75+ if err != nil {
76+ return opts , err
7077 }
7178
7279 if err = json .Unmarshal (cfile , & cfg ); err != nil {
73- return Config {} , err
80+ return opts , err
7481 }
7582
76- return cfg , nil
83+ // Set the config values
84+ opts .IP = cfg .Interface
85+ opts .Port = cfg .Port
86+ opts .Webroot = cfg .Directory
87+ opts .UploadFolder = cfg .UploadFolder
88+ opts .SSL = cfg .SSL
89+ opts .SelfSigned = cfg .SelfSigned
90+ opts .MyKey = cfg .PrivateKey
91+ opts .MyCert = cfg .Certificate
92+ opts .MyP12 = cfg .P12
93+ opts .P12NoPass = cfg .P12NoPass
94+ opts .LetsEncrypt = cfg .LetsEncrypt
95+ opts .LEDomains = cfg .LetsEncryptDomain
96+ opts .LEEmail = cfg .LetsEncryptEmail
97+ opts .LEHTTPPort = cfg .LetsEncryptHTTPPort
98+ opts .LETLSPort = cfg .LetsEncryptTLSPort
99+ opts .BasicAuth = cfg .AuthUsername + ":" + cfg .AuthPassword
100+ opts .CertAuth = cfg .CertificateAuth
101+ opts .WebDav = cfg .Webdav
102+ opts .WebDavPort = cfg .WebdavPort
103+ opts .UploadOnly = cfg .UploadOnly
104+ opts .ReadOnly = cfg .ReadOnly
105+ opts .NoClipboard = cfg .NoClipboard
106+ opts .NoDelete = cfg .NoDelete
107+ opts .Verbose = cfg .Verbose
108+ opts .Silent = cfg .Silent
109+ opts .DropUser = cfg .RunningUser
110+ opts .CLI = cfg .CLI
111+ opts .Embedded = cfg .Embedded
112+ opts .Output = cfg .Output
113+ opts .WebhookEnabled = cfg .WebhookEnabled
114+ opts .WebhookURL = cfg .WebhookURL
115+ opts .WebhookProvider = cfg .WebhookProvider
116+ opts .WebhookEventsParsed = cfg .WebhookEvents
117+ opts .SFTP = cfg .SFTP
118+ opts .SFTPPort = cfg .SFTPPort
119+ opts .SFTPKeyFile = cfg .SFTPKeyFile
120+ opts .SFTPHostKeyFile = cfg .SFTPHostKeyFile
121+ opts .Whitelist = cfg .Whitelist
122+ opts .TrustedProxies = cfg .TrustedProxies
123+ opts .Invisible = cfg .Invisible
124+ opts .Tunnel = cfg .Tunnel
125+ opts .DNS = cfg .DNSServer
126+ opts .DNSPort = cfg .DNSPort
127+ opts .DNSIP = cfg .DNSIP
128+ opts .SMTP = cfg .SMTPServer
129+ opts .SMTPPort = cfg .SMTPPort
130+ opts .SMTPDomain = cfg .SMTPDomain
131+
132+ return opts , nil
77133}
78134
79135func PrintExample () (string , error ) {
@@ -134,22 +190,3 @@ func PrintExample() (string, error) {
134190 }
135191 return string (b ), nil
136192}
137-
138- func SanityChecks (webroot string , configpath string , AuthPassword string ) error {
139- if webroot == filepath .Dir (configpath ) {
140- logger .Warn ("You are hosting your config file in the webroot of goshs. This is not recommended." )
141- // Check if the process user can write the config file
142- file , err := os .OpenFile (configpath , os .O_WRONLY | os .O_APPEND , 0600 )
143- if err != nil {
144- return err
145- }
146- file .Close ()
147- return fmt .Errorf ("%s" , "The config file is accessible via goshs and is writeable by the user running goshs. This is a security issue. Read the docs at https://goshs.de/en/usage/config/index.html" )
148- }
149-
150- if ! strings .HasPrefix (AuthPassword , "$2a$" ) {
151- logger .Warn ("The password in the config file is not hashed. This is not recommended. Use goshs -H to hash the password." )
152- }
153-
154- return nil
155- }
0 commit comments