-
Notifications
You must be signed in to change notification settings - Fork 340
/
config.go
190 lines (158 loc) · 4.89 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Sshwifty - A Web SSH client
//
// Copyright (C) 2019-2020 Rui NI <nirui@gmx.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package configuration
import (
"errors"
"fmt"
"net"
"time"
"github.com/niruix/sshwifty/application/network"
)
// Server contains configuration of a HTTP server
type Server struct {
ListenInterface string
ListenPort uint16
InitialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
HeartbeatTimeout time.Duration
ReadDelay time.Duration
WriteDelay time.Duration
TLSCertificateFile string
TLSCertificateKeyFile string
}
func (s Server) defaultListenInterface() string {
if len(s.ListenInterface) > 0 {
return s.ListenInterface
}
return net.IPv4(127, 0, 0, 1).String()
}
func (s Server) defaultListenPort() uint16 {
if s.ListenPort > 0 {
return s.ListenPort
}
return 80
}
func (s Server) maxDur(cur, def time.Duration) time.Duration {
if cur > def {
return cur
}
return def
}
func (s Server) minDur(cur, def time.Duration) time.Duration {
if cur < def {
return cur
}
return def
}
// WithDefault build the configuration and fill the blank with default values
func (s Server) WithDefault() Server {
initialTimeout := s.maxDur(s.InitialTimeout, 1*time.Second)
readTimeout := s.maxDur(initialTimeout, 3*time.Second)
readTimeout = s.maxDur(s.ReadTimeout, readTimeout)
maxHeartBeatTimeout := time.Duration(float64(readTimeout) * 0.8)
heartBeatTimeout := s.minDur(s.HeartbeatTimeout, maxHeartBeatTimeout)
if heartBeatTimeout <= 0 {
heartBeatTimeout = maxHeartBeatTimeout
}
return Server{
ListenInterface: s.defaultListenInterface(),
ListenPort: s.defaultListenPort(),
InitialTimeout: initialTimeout,
ReadTimeout: readTimeout,
WriteTimeout: s.maxDur(s.WriteTimeout, 3*time.Second),
HeartbeatTimeout: heartBeatTimeout,
ReadDelay: s.ReadDelay,
WriteDelay: s.WriteDelay,
TLSCertificateFile: s.TLSCertificateFile,
TLSCertificateKeyFile: s.TLSCertificateKeyFile,
}
}
// IsTLS returns whether or not TLS should be used
func (s Server) IsTLS() bool {
return len(s.TLSCertificateFile) > 0 && len(s.TLSCertificateKeyFile) > 0
}
// Verify verifies current configuration
func (s Server) Verify() error {
if net.ParseIP(s.ListenInterface) == nil {
return fmt.Errorf("Invalid IP address \"%s\"", s.ListenInterface)
}
if (len(s.TLSCertificateFile) > 0 && len(s.TLSCertificateKeyFile) <= 0) ||
(len(s.TLSCertificateFile) <= 0 && len(s.TLSCertificateKeyFile) > 0) {
return errors.New("TLSCertificateFile and TLSCertificateKeyFile must " +
"both be specified in order to enable TLS")
}
return nil
}
// Preset contains data of a static remote host
type Preset struct {
Title string
Type string
Host string
Meta map[string]string
}
// Configuration contains configuration of the application
type Configuration struct {
HostName string
SharedKey string
Dialer network.Dial
DialTimeout time.Duration
Servers []Server
Presets []Preset
OnlyAllowPresetRemotes bool
}
// Common settings shared by mulitple servers
type Common struct {
HostName string
SharedKey string
Dialer network.Dial
DialTimeout time.Duration
Presets []Preset
OnlyAllowPresetRemotes bool
}
// Verify verifies current setting
func (c Configuration) Verify() error {
if len(c.Servers) <= 0 {
return errors.New("Must specify at least one server")
}
for i, c := range c.Servers {
vErr := c.Verify()
if vErr == nil {
continue
}
return fmt.Errorf("Invalid setting for server %d: %s", i, vErr)
}
return nil
}
// Common returns common settings
func (c Configuration) Common() Common {
return Common{
HostName: c.HostName,
SharedKey: c.SharedKey,
Dialer: c.Dialer,
DialTimeout: c.DialTimeout,
Presets: c.Presets,
OnlyAllowPresetRemotes: c.OnlyAllowPresetRemotes,
}
}
// DecideDialTimeout will return a reasonable timeout for dialing
func (c Common) DecideDialTimeout(max time.Duration) time.Duration {
if c.DialTimeout > max {
return max
}
return c.DialTimeout
}