config.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. "reflect"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. )
  12. type Configuration struct {
  13. Version int `xml:"version,attr" default:"1"`
  14. Repositories []RepositoryConfiguration `xml:"repository"`
  15. Options OptionsConfiguration `xml:"options"`
  16. XMLName xml.Name `xml:"configuration" json:"-"`
  17. }
  18. type RepositoryConfiguration struct {
  19. Directory string `xml:"directory,attr"`
  20. Nodes []NodeConfiguration `xml:"node"`
  21. }
  22. type NodeConfiguration struct {
  23. NodeID string `xml:"id,attr"`
  24. Name string `xml:"name,attr"`
  25. Addresses []string `xml:"address"`
  26. }
  27. type OptionsConfiguration struct {
  28. ListenAddress []string `xml:"listenAddress" default:":22000" ini:"listen-address"`
  29. ReadOnly bool `xml:"readOnly" ini:"read-only"`
  30. AllowDelete bool `xml:"allowDelete" default:"true" ini:"allow-delete"`
  31. FollowSymlinks bool `xml:"followSymlinks" default:"true" ini:"follow-symlinks"`
  32. GUIEnabled bool `xml:"guiEnabled" default:"true" ini:"gui-enabled"`
  33. GUIAddress string `xml:"guiAddress" default:"127.0.0.1:8080" ini:"gui-address"`
  34. GlobalAnnServer string `xml:"globalAnnounceServer" default:"syncthing.nym.se:22025" ini:"global-announce-server"`
  35. GlobalAnnEnabled bool `xml:"globalAnnounceEnabled" default:"true" ini:"global-announce-enabled"`
  36. LocalAnnEnabled bool `xml:"localAnnounceEnabled" default:"true" ini:"local-announce-enabled"`
  37. ParallelRequests int `xml:"parallelRequests" default:"16" ini:"parallel-requests"`
  38. MaxSendKbps int `xml:"maxSendKbps" ini:"max-send-kbps"`
  39. RescanIntervalS int `xml:"rescanIntervalS" default:"60" ini:"rescan-interval"`
  40. ReconnectIntervalS int `xml:"reconnectionIntervalS" default:"60" ini:"reconnection-interval"`
  41. MaxChangeKbps int `xml:"maxChangeKbps" default:"1000" ini:"max-change-bw"`
  42. }
  43. func setDefaults(data interface{}) error {
  44. s := reflect.ValueOf(data).Elem()
  45. t := s.Type()
  46. for i := 0; i < s.NumField(); i++ {
  47. f := s.Field(i)
  48. tag := t.Field(i).Tag
  49. v := tag.Get("default")
  50. if len(v) > 0 {
  51. switch f.Interface().(type) {
  52. case string:
  53. f.SetString(v)
  54. case []string:
  55. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), 1, 1)
  56. rv.Index(0).SetString(v)
  57. f.Set(rv)
  58. case int:
  59. i, err := strconv.ParseInt(v, 10, 64)
  60. if err != nil {
  61. return err
  62. }
  63. f.SetInt(i)
  64. case bool:
  65. f.SetBool(v == "true")
  66. default:
  67. panic(f.Type())
  68. }
  69. }
  70. }
  71. return nil
  72. }
  73. func readConfigINI(m map[string]string, data interface{}) error {
  74. s := reflect.ValueOf(data).Elem()
  75. t := s.Type()
  76. for i := 0; i < s.NumField(); i++ {
  77. f := s.Field(i)
  78. tag := t.Field(i).Tag
  79. name := tag.Get("ini")
  80. if len(name) == 0 {
  81. name = strings.ToLower(t.Field(i).Name)
  82. }
  83. if v, ok := m[name]; ok {
  84. switch f.Interface().(type) {
  85. case string:
  86. f.SetString(v)
  87. case int:
  88. i, err := strconv.ParseInt(v, 10, 64)
  89. if err == nil {
  90. f.SetInt(i)
  91. }
  92. case bool:
  93. f.SetBool(v == "true")
  94. default:
  95. panic(f.Type())
  96. }
  97. }
  98. }
  99. return nil
  100. }
  101. func writeConfigXML(wr io.Writer, cfg Configuration) error {
  102. e := xml.NewEncoder(wr)
  103. e.Indent("", " ")
  104. err := e.Encode(cfg)
  105. if err != nil {
  106. return err
  107. }
  108. _, err = wr.Write([]byte("\n"))
  109. return err
  110. }
  111. func uniqueStrings(ss []string) []string {
  112. var m = make(map[string]bool, len(ss))
  113. for _, s := range ss {
  114. m[s] = true
  115. }
  116. var us = make([]string, 0, len(m))
  117. for k := range m {
  118. us = append(us, k)
  119. }
  120. return us
  121. }
  122. func readConfigXML(rd io.Reader) (Configuration, error) {
  123. var cfg Configuration
  124. setDefaults(&cfg)
  125. setDefaults(&cfg.Options)
  126. var err error
  127. if rd != nil {
  128. err = xml.NewDecoder(rd).Decode(&cfg)
  129. }
  130. cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
  131. return cfg, err
  132. }
  133. type NodeConfigurationList []NodeConfiguration
  134. func (l NodeConfigurationList) Less(a, b int) bool {
  135. return l[a].NodeID < l[b].NodeID
  136. }
  137. func (l NodeConfigurationList) Swap(a, b int) {
  138. l[a], l[b] = l[b], l[a]
  139. }
  140. func (l NodeConfigurationList) Len() int {
  141. return len(l)
  142. }
  143. func clusterHash(nodes []NodeConfiguration) string {
  144. sort.Sort(NodeConfigurationList(nodes))
  145. h := sha256.New()
  146. for _, n := range nodes {
  147. h.Write([]byte(n.NodeID))
  148. }
  149. return fmt.Sprintf("%x", h.Sum(nil))
  150. }
  151. func cleanNodeList(nodes []NodeConfiguration, myID string) []NodeConfiguration {
  152. var myIDExists bool
  153. for _, node := range nodes {
  154. if node.NodeID == myID {
  155. myIDExists = true
  156. break
  157. }
  158. }
  159. if !myIDExists {
  160. nodes = append(nodes, NodeConfiguration{
  161. NodeID: myID,
  162. Addresses: []string{"dynamic"},
  163. Name: "",
  164. })
  165. }
  166. sort.Sort(NodeConfigurationList(nodes))
  167. return nodes
  168. }