forked from JNPRAutomate/nitro-tftp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
67 lines (61 loc) · 2.2 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
package main
import (
"encoding/json"
"io/ioutil"
"net"
)
//Config server configration file
type Config struct {
IncomingDir string `json:"incomingdir"` //IncomingDir incoming directory
OutgoingDir string `json:"outgoingdir"` //OutgoingDir outgoing directory, can be the same as incoming
IP net.IP `json:"listenip"` //IP IP address to listen on
Port int `json:"port"` //Port port to listen on 69 is the default, requires root or administrator privledges
Protocol string `json:"protocol"` //Protocol protocol to listen on can be udp,udp4,udp6
Stats bool `json:"stats"` //Stats determines if stats are to be collected or not
StatsIP net.IP `json:"statsip"` //StatsIP the IP address to listen on for stats api 127.0.0.1 default
StatsPort int `json:"statsport"` //StatsPort the port to listen on for stats api 126969 default
}
//NewConfig creates a new config struct and returns it
//If not loaded with Parse then the method returns the defaults
// Default configuration
// IncomingDir "./incoming"
// OutgoingDir "./outgong"
// IP "0.0.0.0"
// Port 69
// Protocol "udp4"
// Stats true
// StatsIP "127.0.0.1" IPv4 localhost IP
// StatsPort 126969
func NewConfig() *Config {
return &Config{IncomingDir: "./incoming", OutgoingDir: "./outgoing", IP: net.ParseIP("0.0.0.0"), Port: 6969, Protocol: "udp4", Stats: true, StatsIP: net.ParseIP("127.0.0.1"), StatsPort: 16969}
}
//Open open a new config file
func (c *Config) Open(config string) error {
file, err := ioutil.ReadFile(config)
if err != nil {
return err
}
newConfig := NewConfig()
if err := json.Unmarshal(file, &newConfig); err != nil {
return err
}
c.IncomingDir = newConfig.IncomingDir
c.OutgoingDir = newConfig.OutgoingDir
c.IP = newConfig.IP
c.Port = newConfig.Port
c.Protocol = newConfig.Protocol
return nil
}
//StringParse parse a JSON string configuration file
func (c *Config) StringParse(config string) error {
newConfig := NewConfig()
if err := json.Unmarshal([]byte(config), &newConfig); err != nil {
return err
}
c.IncomingDir = newConfig.IncomingDir
c.OutgoingDir = newConfig.OutgoingDir
c.IP = newConfig.IP
c.Port = newConfig.Port
c.Protocol = newConfig.Protocol
return nil
}