修改为sql数据库

This commit is contained in:
bjdgyc 2021-07-16 11:25:06 +08:00
parent 884f41d2f8
commit e7ef29c4ad
9 changed files with 36 additions and 27 deletions

View File

@ -77,7 +77,7 @@ sudo ./anylink
## Config
默认配置文件内有详细的注释,根据注释填写配置即可。
> 默认配置文件内有详细的注释,根据注释填写配置即可。
```shell
# 生成后台密码
@ -87,7 +87,17 @@ sudo ./anylink
./anylink tool -s
```
[conf/server.toml](server/conf/server.toml)
> 数据库配置示例
| db_type | db_source |
| ---- | ---- |
| sqlite3 | ./conf/anylink.db |
| mysql | user:password@tcp(127.0.0.1:3306)/anylink?charset=utf8 |
| postgres | user:password@localhost/anylink?sslmode=verify-full |
> 示例配置文件
>
> [conf/server-sample.toml](server/conf/server-sample.toml)
## Setting

View File

@ -5,11 +5,10 @@ import (
"net/http"
"runtime"
"github.com/bjdgyc/anylink/dbdata"
"github.com/bjdgyc/anylink/sessdata"
"github.com/bjdgyc/anylink/base"
"github.com/bjdgyc/anylink/dbdata"
"github.com/bjdgyc/anylink/pkg/utils"
"github.com/bjdgyc/anylink/sessdata"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"

View File

@ -36,19 +36,18 @@ type ServerConfig struct {
ServerDTLS bool `json:"server_dtls"`
AdminAddr string `json:"admin_addr"`
ProxyProtocol bool `json:"proxy_protocol"`
// DbFile string `json:"db_file"`
DbType string `json:"db_type"`
DbDsn string `json:"db_dsn"`
CertFile string `json:"cert_file"`
CertKey string `json:"cert_key"`
FilesPath string `json:"files_path"`
LogPath string `json:"log_path"`
LogLevel string `json:"log_level"`
Pprof bool `json:"pprof"`
Issuer string `json:"issuer"`
AdminUser string `json:"admin_user"`
AdminPass string `json:"admin_pass"`
JwtSecret string `json:"jwt_secret"`
DbType string `json:"db_type"`
DbSource string `json:"db_source"`
CertFile string `json:"cert_file"`
CertKey string `json:"cert_key"`
FilesPath string `json:"files_path"`
LogPath string `json:"log_path"`
LogLevel string `json:"log_level"`
Pprof bool `json:"pprof"`
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

View File

@ -23,13 +23,13 @@ var configs = []config{
{Typ: cfgStr, Name: "server_dtls_addr", Usage: "DTLS监听地址", ValStr: ":4433"},
{Typ: cfgStr, Name: "admin_addr", Usage: "后台服务监听地址", ValStr: ":8800"},
{Typ: cfgBool, Name: "proxy_protocol", Usage: "TCP代理协议", ValBool: false},
{Typ: cfgStr, Name: "db_type", Usage: "数据库类型[sqlite3、mysql、postgres]", ValStr: "sqlite3"},
{Typ: cfgStr, Name: "db_dsn", Usage: "数据库dsn", ValStr: "./conf/sqlite3.db"},
{Typ: cfgStr, Name: "db_type", Usage: "数据库类型 [sqlite3、mysql、postgres]", ValStr: "sqlite3"},
{Typ: cfgStr, Name: "db_source", Usage: "数据库source", ValStr: "./conf/anylink.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: "files_path", Usage: "外部下载文件路径", ValStr: "./conf/files"},
{Typ: cfgStr, Name: "log_path", Usage: "日志文件路径,默认标准输出", ValStr: ""},
{Typ: cfgStr, Name: "log_level", Usage: "日志等级 debug、info、warn、error", ValStr: "info"},
{Typ: cfgStr, Name: "log_level", Usage: "日志等级 [debug、info、warn、error]", ValStr: "info"},
{Typ: cfgBool, Name: "pprof", Usage: "开启pprof", ValBool: false},
{Typ: cfgStr, Name: "issuer", Usage: "系统名称", ValStr: "XX公司VPN"},
{Typ: cfgStr, Name: "admin_user", Usage: "管理用户名", ValStr: "admin"},

View File

@ -4,7 +4,8 @@
#或者相对于 anylink 二进制文件的路径
#数据文件
db_file = "./conf/data.db"
db_type = "sqlite3"
db_source = "./conf/anylink.db"
#证书文件
cert_file = "./conf/vpn_cert.pem"
cert_key = "./conf/vpn_cert.key"

View File

@ -20,7 +20,7 @@ func GetXdb() *xorm.Engine {
func initDb() {
var err error
xdb, err = xorm.NewEngine(base.Cfg.DbType, base.Cfg.DbDsn)
xdb, err = xorm.NewEngine(base.Cfg.DbType, base.Cfg.DbSource)
// xdb.ShowSQL(true)
if err != nil {
base.Fatal(err)

View File

@ -69,12 +69,12 @@ func Find(data interface{}, limit, page int) error {
}
func CountPrefix(fieldName string, prefix string, data interface{}) int {
n, _ := xdb.Where(fieldName + " like '" + prefix + "%' ").Count(data)
n, _ := xdb.Where(fieldName+" like ?", prefix+"%").Count(data)
return int(n)
}
func Prefix(fieldName string, prefix string, data interface{}, limit, page int) error {
where := xdb.Where(fieldName + " like '" + prefix + "%' ")
where := xdb.Where(fieldName+" like ?", prefix+"%")
if limit == 0 {
return where.Find(data)
}

View File

@ -12,7 +12,7 @@ import (
func preIpData() {
tmpDb := path.Join(os.TempDir(), "anylink_test.db")
base.Cfg.DbType = "sqlite3"
base.Cfg.DbDsn = tmpDb
base.Cfg.DbSource = tmpDb
initDb()
}

View File

@ -16,7 +16,7 @@ func preData(tmpDir string) {
base.Test()
tmpDb := path.Join(tmpDir, "test.db")
base.Cfg.DbType = "sqlite3"
base.Cfg.DbDsn = tmpDb
base.Cfg.DbSource = tmpDb
base.Cfg.Ipv4CIDR = "192.168.3.0/24"
base.Cfg.Ipv4Start = "192.168.3.1"
base.Cfg.Ipv4End = "192.168.3.199"