cfg.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package base
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "reflect"
  7. )
  8. const (
  9. LinkModeTUN = "tun"
  10. LinkModeTAP = "tap"
  11. LinkModeMacvtap = "macvtap"
  12. LinkModeIpvtap = "ipvtap"
  13. )
  14. var (
  15. Cfg = &ServerConfig{}
  16. )
  17. // # ReKey time (in seconds)
  18. // rekey-time = 172800
  19. // # ReKey method
  20. // # Valid options: ssl, new-tunnel
  21. // # ssl: Will perform an efficient rehandshake on the channel allowing
  22. // # a seamless connection during rekey.
  23. // # new-tunnel: Will instruct the client to discard and re-establish the channel.
  24. // # Use this option only if the connecting clients have issues with the ssl
  25. // # option.
  26. // rekey-method = ssl
  27. type ServerConfig struct {
  28. // LinkAddr string `json:"link_addr"`
  29. Conf string `json:"conf"`
  30. Profile string `json:"profile"`
  31. ServerAddr string `json:"server_addr"`
  32. ServerDTLSAddr string `json:"server_dtls_addr"`
  33. ServerDTLS bool `json:"server_dtls"`
  34. AdminAddr string `json:"admin_addr"`
  35. ProxyProtocol bool `json:"proxy_protocol"`
  36. DbType string `json:"db_type"`
  37. DbSource string `json:"db_source"`
  38. CertFile string `json:"cert_file"`
  39. CertKey string `json:"cert_key"`
  40. FilesPath string `json:"files_path"`
  41. LogPath string `json:"log_path"`
  42. LogLevel string `json:"log_level"`
  43. Pprof bool `json:"pprof"`
  44. Issuer string `json:"issuer"`
  45. AdminUser string `json:"admin_user"`
  46. AdminPass string `json:"admin_pass"`
  47. AdminOtp string `json:"admin_otp"`
  48. JwtSecret string `json:"jwt_secret"`
  49. LinkMode string `json:"link_mode"` // tun tap macvtap ipvtap
  50. Ipv4Master string `json:"ipv4_master"` // eth0
  51. Ipv4CIDR string `json:"ipv4_cidr"` // 192.168.10.0/24
  52. Ipv4Gateway string `json:"ipv4_gateway"` // 192.168.10.1
  53. Ipv4Start string `json:"ipv4_start"` // 192.168.10.100
  54. Ipv4End string `json:"ipv4_end"` // 192.168.10.200
  55. IpLease int `json:"ip_lease"`
  56. MaxClient int `json:"max_client"`
  57. MaxUserClient int `json:"max_user_client"`
  58. DefaultGroup string `json:"default_group"`
  59. CstpKeepalive int `json:"cstp_keepalive"` // in seconds
  60. CstpDpd int `json:"cstp_dpd"` // Dead peer detection in seconds
  61. MobileKeepalive int `json:"mobile_keepalive"`
  62. MobileDpd int `json:"mobile_dpd"`
  63. Mtu int `json:"mtu"`
  64. DefaultDomain string `json:"default_domain"`
  65. SessionTimeout int `json:"session_timeout"` // in seconds
  66. // AuthTimeout int `json:"auth_timeout"` // in seconds
  67. AuditInterval int `json:"audit_interval"` // in seconds
  68. ShowSQL bool `json:"show_sql"` // bool
  69. IptablesNat bool `json:"iptables_nat"`
  70. Compression bool `json:"compression"` // bool
  71. NoCompressLimit int `json:"no_compress_limit"` // int
  72. DisplayError bool `json:"display_error"`
  73. }
  74. func initServerCfg() {
  75. // TODO 取消绝对地址转换
  76. // sf, _ := filepath.Abs(cfgFile)
  77. // base := filepath.Dir(sf)
  78. // 转换成绝对路径
  79. // Cfg.DbFile = getAbsPath(base, Cfg.DbFile)
  80. // Cfg.CertFile = getAbsPath(base, Cfg.CertFile)
  81. // Cfg.CertKey = getAbsPath(base, Cfg.CertKey)
  82. // Cfg.UiPath = getAbsPath(base, Cfg.UiPath)
  83. // Cfg.FilesPath = getAbsPath(base, Cfg.FilesPath)
  84. // Cfg.LogPath = getAbsPath(base, Cfg.LogPath)
  85. if Cfg.AdminPass == defaultPwd {
  86. fmt.Fprintln(os.Stderr, "=== 使用默认的admin_pass有安全风险,请设置新的admin_pass ===")
  87. }
  88. if Cfg.JwtSecret == defaultJwt {
  89. fmt.Fprintln(os.Stderr, "=== 使用默认的jwt_secret有安全风险,请设置新的jwt_secret ===")
  90. }
  91. fmt.Printf("ServerCfg: %+v \n", Cfg)
  92. }
  93. func getAbsPath(base, cfile string) string {
  94. if cfile == "" {
  95. return ""
  96. }
  97. abs := filepath.IsAbs(cfile)
  98. if abs {
  99. return cfile
  100. }
  101. return filepath.Join(base, cfile)
  102. }
  103. func initCfg() {
  104. ref := reflect.ValueOf(Cfg)
  105. s := ref.Elem()
  106. typ := s.Type()
  107. numFields := s.NumField()
  108. for i := 0; i < numFields; i++ {
  109. field := typ.Field(i)
  110. value := s.Field(i)
  111. tag := field.Tag.Get("json")
  112. for _, v := range configs {
  113. if v.Name == tag {
  114. if v.Typ == cfgStr {
  115. value.SetString(linkViper.GetString(v.Name))
  116. }
  117. if v.Typ == cfgInt {
  118. value.SetInt(int64(linkViper.GetInt(v.Name)))
  119. }
  120. if v.Typ == cfgBool {
  121. value.SetBool(linkViper.GetBool(v.Name))
  122. }
  123. }
  124. }
  125. }
  126. initServerCfg()
  127. }
  128. type SCfg struct {
  129. Name string `json:"name"`
  130. Env string `json:"env"`
  131. Info string `json:"info"`
  132. Data interface{} `json:"data"`
  133. }
  134. func ServerCfg2Slice() []SCfg {
  135. ref := reflect.ValueOf(Cfg)
  136. s := ref.Elem()
  137. var datas []SCfg
  138. typ := s.Type()
  139. numFields := s.NumField()
  140. for i := 0; i < numFields; i++ {
  141. field := typ.Field(i)
  142. value := s.Field(i)
  143. tag := field.Tag.Get("json")
  144. usage, env := getUsageEnv(tag)
  145. datas = append(datas, SCfg{Name: tag, Env: env, Info: usage, Data: value.Interface()})
  146. }
  147. return datas
  148. }
  149. func getUsageEnv(name string) (usage, env string) {
  150. for _, v := range configs {
  151. if v.Name == name {
  152. usage = v.Usage
  153. }
  154. }
  155. if e, ok := envs[name]; ok {
  156. env = e
  157. }
  158. return
  159. }