Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新配置方式 #14

Merged
merged 19 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
修改配置方式,支持配置命令、环境变量、配置文件
  • Loading branch information
bjdgyc committed Apr 7, 2021
commit 157001be187ce1b0f1357880128d00ecd3ea68c1
3 changes: 2 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ vendor/

ui/
.idea/
anylink
anylink
data.db
172 changes: 172 additions & 0 deletions server/base/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package base

import (
"fmt"
"os"
"path/filepath"
"reflect"

"github.com/spf13/viper"
)

const (
LinkModeTUN = "tun"
LinkModeTAP = "tap"
)

var (
Cfg = &ServerConfig{}
)

// # ReKey time (in seconds)
// rekey-time = 172800
// # ReKey method
// # Valid options: ssl, new-tunnel
// # ssl: Will perform an efficient rehandshake on the channel allowing
// # a seamless connection during rekey.
// # new-tunnel: Will instruct the client to discard and re-establish the channel.
// # Use this option only if the connecting clients have issues with the ssl
// # option.
// rekey-method = ssl

type ServerConfig struct {
LinkAddr string `json:"link_addr"`
ServerAddr string `json:"server_addr"`
AdminAddr string `json:"admin_addr"`
ProxyProtocol bool `json:"proxy_protocol"`
DbFile string `json:"db_file"`
CertFile string `json:"cert_file"`
CertKey string `json:"cert_key"`
UiPath string `json:"ui_path"`
FilesPath string `json:"files_path"`
LogPath string `json:"log_path"`
LogLevel string `json:"log_level"`
Issuer string `json:"issuer"`
AdminUser string `json:"admin_user"`
AdminPass string `json:"admin_pass"`
JwtSecret string `json:"jwt_secret"`

LinkMode string `json:"link_mode"` // tun tap
Ipv4CIDR string `json:"ipv4_cidr"` // 192.168.1.0/24
Ipv4Gateway string `json:"ipv4_gateway"`
Ipv4Start string `json:"ipv4_start"` // 192.168.1.100
Ipv4End string `json:"ipv4_end"` // 192.168.1.200
IpLease int `json:"ip_lease"`

MaxClient int `json:"max_client"`
MaxUserClient int `json:"max_user_client"`
DefaultGroup string `json:"default_group"`
CstpKeepalive int `json:"cstp_keepalive"` // in seconds
CstpDpd int `json:"cstp_dpd"` // Dead peer detection in seconds
MobileKeepalive int `json:"mobile_keepalive"`
MobileDpd int `json:"mobile_dpd"`

SessionTimeout int `json:"session_timeout"` // in seconds
AuthTimeout int `json:"auth_timeout"` // in seconds
}

func initServerCfg() {

sf, _ := filepath.Abs(cfgFile)
base := filepath.Dir(sf)

// 转换成绝对路径
Cfg.DbFile = getAbsPath(base, Cfg.DbFile)
Cfg.CertFile = getAbsPath(base, Cfg.CertFile)
Cfg.CertKey = getAbsPath(base, Cfg.CertKey)
Cfg.UiPath = getAbsPath(base, Cfg.UiPath)
Cfg.FilesPath = getAbsPath(base, Cfg.FilesPath)
Cfg.LogPath = getAbsPath(base, Cfg.LogPath)

if len(Cfg.JwtSecret) < 20 {
fmt.Println("请设置 jwt_secret 长度20位以上")
os.Exit(0)
}

fmt.Printf("ServerCfg: %+v \n", Cfg)
}

func getAbsPath(base, cfile string) string {
if cfile == "" {
return ""
}

abs := filepath.IsAbs(cfile)
if abs {
return cfile
}
return filepath.Join(base, cfile)
}

func initCfg() {
ref := reflect.ValueOf(Cfg)
s := ref.Elem()

typ := s.Type()
numFields := s.NumField()
for i := 0; i < numFields; i++ {
field := typ.Field(i)
value := s.Field(i)
tag := field.Tag.Get("json")

for _, v := range configs {
if v.Name == tag {
if v.Typ == cfgStr {
value.SetString(viper.GetString(v.Name))
}
if v.Typ == cfgInt {
value.SetInt(int64(viper.GetInt(v.Name)))
}
if v.Typ == cfgBool {
value.SetBool(viper.GetBool(v.Name))
}
}
}
}

initServerCfg()
}

type SCfg struct {
Name string `json:"name"`
Env string `json:"env"`
Info string `json:"info"`
Data interface{} `json:"data"`
}

func ServerCfg2Slice() []SCfg {
ref := reflect.ValueOf(Cfg)
s := ref.Elem()

var datas []SCfg

typ := s.Type()
numFields := s.NumField()
for i := 0; i < numFields; i++ {
field := typ.Field(i)
value := s.Field(i)
tag := field.Tag.Get("json")
usage, env := getUsageEnv(tag)
if usage == "" {
continue
}

datas = append(datas, SCfg{Name: tag, Env: env, Info: usage, Data: value.Interface()})
}

return datas
}

func getUsageEnv(name string) (usage, env string) {
for _, v := range configs {
if v.Name == name {
usage = v.Usage
}
}

if e, ok := envs[name]; ok {
env = e
}

return
}
135 changes: 0 additions & 135 deletions server/base/cfg_server.go

This file was deleted.

Loading