修改配置方式,支持配置命令、环境变量、配置文件

This commit is contained in:
bjdgyc
2021-04-07 18:31:46 +08:00
parent 7fae5423b7
commit 157001be18
12 changed files with 693 additions and 200 deletions

172
server/base/cfg.go Normal file
View File

@@ -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
}

View File

@@ -1,135 +0,0 @@
package base
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/pelletier/go-toml"
)
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 `toml:"link_addr" info:"vpn服务对外地址"`
ServerAddr string `toml:"server_addr" info:"前台服务监听地址"`
AdminAddr string `toml:"admin_addr" info:"后台服务监听地址"`
ProxyProtocol bool `toml:"proxy_protocol" info:"TCP代理协议"`
DbFile string `toml:"db_file" info:"数据库地址"`
CertFile string `toml:"cert_file" info:"证书文件"`
CertKey string `toml:"cert_key" info:"证书密钥"`
UiPath string `toml:"ui_path" info:"ui文件路径"`
FilesPath string `toml:"files_path" info:"外部下载文件路径"`
LogPath string `toml:"log_path" info:"日志文件路径"`
LogLevel string `toml:"log_level" info:"日志等级"`
Issuer string `toml:"issuer" info:"系统名称"`
AdminUser string `toml:"admin_user" info:"管理用户名"`
AdminPass string `toml:"admin_pass" info:"管理用户密码"`
JwtSecret string `toml:"jwt_secret" info:"JWT密钥"`
LinkMode string `toml:"link_mode" info:"虚拟网络类型"` // tun tap
Ipv4CIDR string `toml:"ipv4_cidr" info:"ip地址网段"` // 192.168.1.0/24
Ipv4Gateway string `toml:"ipv4_gateway" info:"ipv4_gateway"`
Ipv4Pool []string `toml:"ipv4_pool" info:"IPV4起止地址池"` // Pool[0]=192.168.1.100 Pool[1]=192.168.1.200
IpLease int `toml:"ip_lease" info:"IP租期(秒)"`
MaxClient int `toml:"max_client" info:"最大用户连接"`
MaxUserClient int `toml:"max_user_client" info:"最大单用户连接"`
DefaultGroup string `toml:"default_group" info:"默认用户组"`
CstpKeepalive int `toml:"cstp_keepalive" info:"keepalive时间(秒)"` // in seconds
CstpDpd int `toml:"cstp_dpd" info:"死链接检测时间(秒)"` // Dead peer detection in seconds
MobileKeepalive int `toml:"mobile_keepalive" info:"移动端keepalive接检测时间(秒)"`
MobileDpd int `toml:"mobile_dpd" info:"移动端死链接检测时间(秒)"`
SessionTimeout int `toml:"session_timeout" info:"session过期时间(秒)"` // in seconds
AuthTimeout int `toml:"auth_timeout" info:"auth_timeout"` // in seconds
}
func initServerCfg() {
b, err := ioutil.ReadFile(serverFile)
if err != nil {
panic(err)
}
err = toml.Unmarshal(b, Cfg)
if err != nil {
panic(err)
}
sf, _ := filepath.Abs(serverFile)
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)
}
type SCfg struct {
Name string `json:"name"`
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("toml")
tags := strings.Split(tag, ",")
info := field.Tag.Get("info")
datas = append(datas, SCfg{Name: tags[0], Info: info, Data: value.Interface()})
}
return datas
}

131
server/base/cmd.go Normal file
View File

@@ -0,0 +1,131 @@
package base
import (
"fmt"
"math/rand"
"os"
"runtime"
"strings"
"time"
"github.com/bjdgyc/anylink/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
// 提交id
CommitId string
// 配置文件
cfgFile string
// pass明文
passwd string
// 生成密钥
secret bool
// 显示版本信息
rev bool
// 获取env名称
env bool
// Used for flags.
runSrv bool
rootCmd *cobra.Command
)
// Execute executes the root command.
func execute() {
err := rootCmd.Execute()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
// viper.Debug()
if !runSrv {
os.Exit(0)
}
}
func init() {
rootCmd = &cobra.Command{
Use: "anylink",
Short: "AnyLink VPN Server",
Long: `AnyLink is a VPN Server application`,
Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("cmd", cmd.Use, args)
runSrv = true
},
}
cobra.OnInitialize(func() {
viper.SetConfigFile(cfgFile)
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
fmt.Println("Using config file:", err)
}
})
viper.SetEnvPrefix("link")
// 基础配置
rootCmd.Flags().StringVarP(&cfgFile, "config", "c", "./conf/server.toml", "config file")
for _, v := range configs {
if v.Typ == cfgStr {
rootCmd.Flags().String(v.Name, v.ValStr, v.Usage)
}
if v.Typ == cfgInt {
rootCmd.Flags().Int(v.Name, v.ValInt, v.Usage)
}
if v.Typ == cfgBool {
rootCmd.Flags().Bool(v.Name, v.ValBool, v.Usage)
}
_ = viper.BindPFlag(v.Name, rootCmd.Flags().Lookup(v.Name))
_ = viper.BindEnv(v.Name)
// viper.SetDefault(v.Name, v.Value)
}
rootCmd.AddCommand(initToolCmd())
}
func initToolCmd() *cobra.Command {
toolCmd := &cobra.Command{
Use: "tool",
Short: "AnyLink tool",
Long: `AnyLink tool is a application`,
}
toolCmd.Flags().BoolVarP(&rev, "version", "v", false, "display version info")
toolCmd.Flags().BoolVarP(&secret, "secret", "s", false, "generate a random jwt secret")
toolCmd.Flags().StringVarP(&passwd, "passwd", "p", "", "convert the password plaintext")
toolCmd.Flags().BoolVarP(&env, "env", "e", false, "list the config name and env key")
toolCmd.Run = func(cmd *cobra.Command, args []string) {
switch {
case rev:
fmt.Printf("%s v%s build on %s [%s, %s] commit_id(%s) \n",
APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, CommitId)
case secret:
rand.Seed(time.Now().UnixNano())
s, _ := utils.RandSecret(40, 60)
s = strings.Trim(s, "=")
fmt.Printf("Secret:%s\n", s)
case passwd != "":
pass, _ := utils.PasswordHash(passwd)
fmt.Printf("Passwd:%s\n", pass)
case env:
for k, v := range envs {
fmt.Printf("%s => %s\n", k, v)
}
default:
fmt.Println("Using [anylink tool -h] for help")
}
}
return toolCmd
}

52
server/base/config.go Normal file
View File

@@ -0,0 +1,52 @@
package base
const (
cfgStr = iota
cfgInt
cfgBool
)
type config struct {
Typ int
Name string
Usage string
ValStr string
ValInt int
ValBool bool
}
var configs = []config{
{Typ: cfgStr, Name: "link_addr", Usage: "vpn服务对外地址", ValStr: "vpn.xx.com"},
{Typ: cfgStr, Name: "server_addr", Usage: "前台服务监听地址", ValStr: ":443"},
{Typ: cfgStr, Name: "admin_addr", Usage: "后台服务监听地址", ValStr: ":8800"},
{Typ: cfgBool, Name: "proxy_protocol", Usage: "TCP代理协议", ValBool: false},
{Typ: cfgStr, Name: "db_file", Usage: "数据库地址", ValStr: "./conf/data.db"},
{Typ: cfgStr, Name: "cert_file", Usage: "证书文件", ValStr: "./conf/vpn_cert.pem"},
{Typ: cfgStr, Name: "cert_key", Usage: "证书密钥", ValStr: "./conf/vpn_cert.key"},
{Typ: cfgStr, Name: "ui_path", Usage: "ui文件路径", ValStr: "./ui"},
{Typ: cfgStr, Name: "files_path", Usage: "外部下载文件路径", ValStr: "./conf/files"},
{Typ: cfgStr, Name: "log_path", Usage: "日志文件路径", ValStr: ""},
{Typ: cfgStr, Name: "log_level", Usage: "日志等级", ValStr: "info"},
{Typ: cfgStr, Name: "issuer", Usage: "系统名称", ValStr: "XX公司VPN"},
{Typ: cfgStr, Name: "admin_user", Usage: "管理用户名", ValStr: "admin"},
{Typ: cfgStr, Name: "admin_pass", Usage: "管理用户密码", ValStr: ""},
{Typ: cfgStr, Name: "jwt_secret", Usage: "JWT密钥", ValStr: ""},
{Typ: cfgStr, Name: "link_mode", Usage: "虚拟网络类型", ValStr: "tun"},
{Typ: cfgStr, Name: "ipv4_cidr", Usage: "ip地址网段", ValStr: "192.168.10.0/24"},
{Typ: cfgStr, Name: "ipv4_gateway", Usage: "ipv4_gateway", ValStr: "192.168.10.1"},
{Typ: cfgStr, Name: "ipv4_start", Usage: "IPV4开始地址", ValStr: "192.168.10.100"},
{Typ: cfgStr, Name: "ipv4_end", Usage: "IPV4结束", ValStr: "192.168.10.200"},
{Typ: cfgStr, Name: "default_group", Usage: "默认用户组", ValStr: "one"},
{Typ: cfgInt, Name: "ip_lease", Usage: "IP租期(秒)", ValInt: 1209600},
{Typ: cfgInt, Name: "max_client", Usage: "最大用户连接", ValInt: 100},
{Typ: cfgInt, Name: "max_user_client", Usage: "最大单用户连接", ValInt: 3},
{Typ: cfgInt, Name: "cstp_keepalive", Usage: "keepalive时间(秒)", ValInt: 20},
{Typ: cfgInt, Name: "cstp_dpd", Usage: "死链接检测时间(秒)", ValInt: 30},
{Typ: cfgInt, Name: "mobile_keepalive", Usage: "移动端keepalive接检测时间(秒)", ValInt: 50},
{Typ: cfgInt, Name: "mobile_dpd", Usage: "移动端死链接检测时间(秒)", ValInt: 60},
{Typ: cfgInt, Name: "session_timeout", Usage: "session过期时间(秒)", ValInt: 3600},
// {Typ: cfgInt, Name: "auth_timeout", Usage: "auth_timeout", ValInt: 0},
}
var envs = map[string]string{"admin_addr": "LINK_ADMIN_ADDR", "admin_pass": "LINK_ADMIN_PASS", "admin_user": "LINK_ADMIN_USER", "cert_file": "LINK_CERT_FILE", "cert_key": "LINK_CERT_KEY", "cstp_dpd": "LINK_CSTP_DPD", "cstp_keepalive": "LINK_CSTP_KEEPALIVE", "db_file": "LINK_DB_FILE", "default_group": "LINK_DEFAULT_GROUP", "files_path": "LINK_FILES_PATH", "ip_lease": "LINK_IP_LEASE", "ipv4_cidr": "LINK_IPV4_CIDR", "ipv4_end": "LINK_IPV4_END", "ipv4_gateway": "LINK_IPV4_GATEWAY", "ipv4_start": "LINK_IPV4_START", "issuer": "LINK_ISSUER", "jwt_secret": "LINK_JWT_SECRET", "link_addr": "LINK_LINK_ADDR", "link_mode": "LINK_LINK_MODE", "log_level": "LINK_LOG_LEVEL", "log_path": "LINK_LOG_PATH", "max_client": "LINK_MAX_CLIENT", "max_user_client": "LINK_MAX_USER_CLIENT", "mobile_dpd": "LINK_MOBILE_DPD", "mobile_keepalive": "LINK_MOBILE_KEEPALIVE", "proxy_protocol": "LINK_PROXY_PROTOCOL", "server_addr": "LINK_SERVER_ADDR", "session_timeout": "LINK_SESSION_TIMEOUT", "ui_path": "LINK_UI_PATH"}

View File

@@ -1,54 +0,0 @@
package base
import (
"flag"
"fmt"
"math/rand"
"os"
"runtime"
"strings"
"time"
"github.com/bjdgyc/anylink/pkg/utils"
)
var (
// 提交id
CommitId string
// 配置文件
serverFile string
// pass明文
passwd string
// 生成密钥
secret bool
// 显示版本信息
rev bool
)
func initFlag() {
flag.StringVar(&serverFile, "conf", "./conf/server.toml", "server config files path")
flag.StringVar(&passwd, "passwd", "", "convert the password plaintext")
flag.BoolVar(&secret, "secret", false, "generate a random jwt secret")
flag.BoolVar(&rev, "rev", false, "display version info")
flag.Parse()
if passwd != "" {
pass, _ := utils.PasswordHash(passwd)
fmt.Printf("Passwd:%s\n", pass)
os.Exit(0)
}
if secret {
rand.Seed(time.Now().UnixNano())
s, _ := utils.RandSecret(40, 60)
s = strings.Trim(s, "=")
fmt.Printf("Secret:%s\n", s)
os.Exit(0)
}
if rev {
fmt.Printf("%s v%s build on %s [%s, %s] commit_id(%s) \n",
APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, CommitId)
os.Exit(0)
}
}

View File

@@ -1,11 +1,11 @@
package base
func Start() {
initFlag()
initServerCfg()
execute()
initCfg()
initLog()
}
func Test() {
func Test() {
initLog()
}
}