Skip to content

Commit

Permalink
Clean up code inside the configuration loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
NI committed Feb 8, 2020
1 parent 921b887 commit 9b2513c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
24 changes: 1 addition & 23 deletions application/configuration/config.go
Expand Up @@ -170,32 +170,10 @@ func (c Configuration) Verify() error {

// Common returns common settings
func (c Configuration) Common() Common {
dialer := c.Dialer

if dialer == nil {
dialer = network.TCPDial()
}

if c.OnlyAllowPresetRemotes {
accessList := make(network.AllowedHosts, len(c.Presets))

for _, k := range c.Presets {
accessList[k.Host] = struct{}{}
}

dialer = network.AccessControlDial(accessList, dialer)
}

dialTimeout := c.DialTimeout

if dialTimeout <= 1*time.Second {
dialTimeout = 1 * time.Second
}

return Common{
HostName: c.HostName,
SharedKey: c.SharedKey,
Dialer: dialer,
Dialer: c.Dialer,
DialTimeout: c.DialTimeout,
Presets: c.Presets,
OnlyAllowPresetRemotes: c.OnlyAllowPresetRemotes,
Expand Down
19 changes: 11 additions & 8 deletions application/configuration/loader_enviro.go
Expand Up @@ -57,6 +57,10 @@ func Enviro() Loader {
Socks5: parseEviro("SSHWIFTY_SOCKS5"),
Socks5User: parseEviro("SSHWIFTY_SOCKS5_USER"),
Socks5Password: parseEviro("SSHWIFTY_SOCKS5_PASSWORD"),
Servers: nil,
Presets: nil,
OnlyAllowPresetRemotes: len(
parseEviro("SSHWIFTY_ONLYALLOWPRESETREMOTES")) > 0,
}.build()

if cfgErr != nil {
Expand Down Expand Up @@ -116,14 +120,13 @@ func Enviro() Loader {
}

return enviroTypeName, Configuration{
HostName: cfg.HostName,
SharedKey: cfg.SharedKey,
Dialer: dialer,
DialTimeout: time.Duration(cfg.DialTimeout) * time.Second,
Servers: []Server{cfgSer.build()},
Presets: presets,
OnlyAllowPresetRemotes: len(
parseEviro("SSHWIFTY_ONLYALLOWPRESETREMOTES")) > 0,
HostName: cfg.HostName,
SharedKey: cfg.SharedKey,
Dialer: dialer,
DialTimeout: time.Duration(cfg.DialTimeout) * time.Second,
Servers: []Server{cfgSer.build()},
Presets: presets,
OnlyAllowPresetRemotes: cfg.OnlyAllowPresetRemotes,
}, nil
}
}
14 changes: 14 additions & 0 deletions application/configuration/loader_file.go
Expand Up @@ -144,6 +144,20 @@ func (f fileCfgCommon) build() (fileCfgCommon, network.Dial, error) {
dialer = sDial
}

if f.OnlyAllowPresetRemotes {
accessList := make(network.AllowedHosts, len(f.Presets))

for _, k := range f.Presets {
if len(k.Host) <= 0 {
continue
}

accessList[k.Host] = struct{}{}
}

dialer = network.AccessControlDial(accessList, dialer)
}

return fileCfgCommon{
HostName: f.HostName,
SharedKey: f.SharedKey,
Expand Down

0 comments on commit 9b2513c

Please sign in to comment.