Skip to content

Commit c64a28d

Browse files
committed
big rewrite of main.go, restructuring for better readability
1 parent c2acc4e commit c64a28d

13 files changed

Lines changed: 732 additions & 805 deletions

File tree

config/brokenConfig.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

config/config.go

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package config
22

33
import (
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

1312
type 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

79135
func 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-
}

config/config_test.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

config/goodConfig.json

Lines changed: 0 additions & 37 deletions
This file was deleted.

config/goodConfigReadOnly.json

Lines changed: 0 additions & 37 deletions
This file was deleted.

dnsserver/server.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/miekg/dns"
99
"github.com/patrickhener/goshs/logger"
10+
"github.com/patrickhener/goshs/options"
1011
"github.com/patrickhener/goshs/webhook"
1112
"github.com/patrickhener/goshs/ws"
1213
)
@@ -20,6 +21,18 @@ type DNSServer struct {
2021
WebHook *webhook.Webhook
2122
}
2223

24+
func NewDNSServer(opts *options.Options, hub *ws.Hub, wh *webhook.Webhook) *DNSServer {
25+
return &DNSServer{
26+
IP: "0.0.0.0",
27+
ReplyIP: opts.DNSIP,
28+
Port: opts.DNSPort,
29+
Hub: hub,
30+
Silent: opts.Silent,
31+
WebHook: wh,
32+
}
33+
34+
}
35+
2336
func (d *DNSServer) handler(w dns.ResponseWriter, r *dns.Msg) {
2437
m := new(dns.Msg)
2538
m.SetReply(r)

httpserver/server.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,57 @@ import (
1414

1515
"github.com/howeyc/gopass"
1616
"github.com/patrickhener/goshs/ca"
17+
"github.com/patrickhener/goshs/clipboard"
18+
"github.com/patrickhener/goshs/goshsversion"
1719
"github.com/patrickhener/goshs/logger"
20+
"github.com/patrickhener/goshs/options"
1821
"github.com/patrickhener/goshs/tunnel"
22+
"github.com/patrickhener/goshs/webhook"
23+
"github.com/patrickhener/goshs/ws"
1924
"golang.org/x/net/webdav"
2025
"software.sslmate.com/src/go-pkcs12"
2126
)
2227

28+
func NewHttpServer(opts *options.Options, hub *ws.Hub, clip *clipboard.Clipboard, wl *Whitelist, wh webhook.Webhook) *FileServer {
29+
fs := &FileServer{
30+
IP: opts.IP,
31+
Port: opts.Port,
32+
CLI: opts.CLI,
33+
Webroot: opts.Webroot,
34+
Clipboard: clip,
35+
Hub: hub,
36+
UploadFolder: opts.UploadFolder,
37+
SSL: opts.SSL,
38+
SelfSigned: opts.SelfSigned,
39+
LetsEncrypt: opts.LetsEncrypt,
40+
MyCert: opts.MyCert,
41+
MyKey: opts.MyKey,
42+
MyP12: opts.MyP12,
43+
P12NoPass: opts.P12NoPass,
44+
User: opts.Username,
45+
Pass: opts.Password,
46+
CACert: opts.CertAuth,
47+
DropUser: opts.DropUser,
48+
UploadOnly: opts.UploadOnly,
49+
ReadOnly: opts.ReadOnly,
50+
NoClipboard: opts.NoClipboard,
51+
NoDelete: opts.NoDelete,
52+
Silent: opts.Silent,
53+
Invisible: opts.Invisible,
54+
Embedded: opts.Embedded,
55+
Verbose: opts.Verbose,
56+
Tunnel: opts.Tunnel,
57+
Version: goshsversion.GoshsVersion,
58+
}
59+
60+
fs.Hub = hub
61+
fs.Clipboard = clip
62+
fs.Webhook = wh
63+
fs.Whitelist = wl
64+
65+
return fs
66+
}
67+
2368
func (fs *FileServer) SetupMux(mux *CustomMux, what string) string {
2469
var addr string
2570
switch what {

0 commit comments

Comments
 (0)