From 5a9f16d1ae1a53dc67523e8c09f0f6403ed28165 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Wed, 11 Oct 2023 23:06:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E7=94=A8=20toml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/authlibskin/config.yaml.template | 64 ---------------------- cmd/authlibskin/main.go | 26 ++++----- config/config.go | 80 +++++++++++++++++++--------- config/config.yaml | 2 - config/yaml.go | 16 ------ config/yaml_test.go | 20 ------- go.mod | 6 +-- go.sum | 12 +++-- 8 files changed, 77 insertions(+), 149 deletions(-) delete mode 100644 cmd/authlibskin/config.yaml.template delete mode 100644 config/config.yaml delete mode 100644 config/yaml.go delete mode 100644 config/yaml_test.go diff --git a/cmd/authlibskin/config.yaml.template b/cmd/authlibskin/config.yaml.template deleted file mode 100644 index 62843d9..0000000 --- a/cmd/authlibskin/config.yaml.template +++ /dev/null @@ -1,64 +0,0 @@ -# 为 true 则 uuid 生成方式于离线模式相同,若从离线模式切换不会丢失数据。 -# 已有用户数据的情况下勿更改此项 -offlineUUID: true - -port: "0.0.0.0:8080" - -Log: - level: "debug" - # json 格式输出 - json: false - -sql: - # 可填 mysql 或 sqlite3 - driverName: "sqlite3" - # 填写见 mysql https://github.com/go-sql-driver/mysql#dsn-data-source-name - # sqlite https://github.com/mattn/go-sqlite3 - # 例如 mysql `用户名:密码@tcp(mysqlIP:端口)/数据库名` - # sqlite `data.db?_txlock=IMMEDIATE&_journal_mode=WAL&_fk=true`` - dsn: "data.db?_txlock=IMMEDIATE&_journal_mode=WAL&_fk=true" - -# 输出每条执行的 sql 语句 -debug: false - -cache: - # 默认使用内存缓存,若需要集群部署,请填写 redis - type: "" - # 内存缓存使用大小,单位 b - ram: 10000000 - # redis 服务端地址,如 127.0.0.1:6379 - addr: "" - # redis 密码 - password: "" - -# 位于反向代理后启用,用于记录真实 ip -raelIP: false - -# ip 段最大注册用户,ipv4 为 /24 ipv6 为 /48 -maxIpUser: 10 - -# 运行后勿修改,若为集群需设置为一致 -rsaPriKey: "" - -# 材质文件保存路径,如果需要对象存储可以把对象储存挂载到本地目录上 -texturePath: "skin" - -# 材质静态文件提供基础地址 -# 如果静态文件位于 oss 上,比如 https://s3.amazonaws.com/example/1.png -# 则填写 https://s3.amazonaws.com/example -# 若不需要可不填写 -textureBaseUrl: "" - -# 用于在支持的启动器中展示本站的注册地址 -# 填写类似 https://example.com -webBaseUrl: "" - -# 皮肤站名字,用于在多个地方展示 -serverName: "没有设置名字" - -captcha: - # 验证码类型,目前只支持 cloudflare turnstile - # 填写 turnstile - type: "" - siteKey: "" - secret: "" diff --git a/cmd/authlibskin/main.go b/cmd/authlibskin/main.go index b8aae5f..765d48d 100644 --- a/cmd/authlibskin/main.go +++ b/cmd/authlibskin/main.go @@ -11,20 +11,17 @@ import ( _ "embed" + "github.com/pelletier/go-toml/v2" "github.com/samber/lo" "github.com/xmdhs/authlib-skin/config" "github.com/xmdhs/authlib-skin/server" "github.com/xmdhs/authlib-skin/utils/sign" - "gopkg.in/yaml.v3" ) var configPath string -//go:embed config.yaml.template -var configTempLate []byte - func init() { - flag.StringVar(&configPath, "c", "config.yaml", "") + flag.StringVar(&configPath, "c", "config.toml", "") flag.Parse() } @@ -33,21 +30,20 @@ func main() { b, err := os.ReadFile(configPath) if err != nil { if errors.Is(err, os.ErrNotExist) { - lo.Must0(os.WriteFile(configPath, configTempLate, 0600)) + rsa2048 := lo.Must(rsa.GenerateKey(rand.Reader, 4096)) + as := sign.NewAuthlibSignWithKey(rsa2048) + + c := config.Default() + c.RsaPriKey = lo.Must(as.GetPriKey()) + + lo.Must0(os.WriteFile(configPath, lo.Must(toml.Marshal(c)), 0600)) fmt.Println("未找到配置文件,已写入模板配置文件") return } panic(err) } - config := lo.Must(config.YamlDeCode(b)) - - if config.RsaPriKey == "" { - rsa2048 := lo.Must(rsa.GenerateKey(rand.Reader, 4096)) - as := sign.NewAuthlibSignWithKey(rsa2048) - config.RsaPriKey = lo.Must(as.GetPriKey()) - lo.Must0(os.WriteFile(configPath, lo.Must(yaml.Marshal(config)), 0600)) - } - + var config config.Config + lo.Must0(toml.Unmarshal(b, &config)) s, cancel := lo.Must2(server.InitializeRoute(ctx, config)) defer cancel() panic(s.ListenAndServe()) diff --git a/config/config.go b/config/config.go index 769d515..4fa1283 100644 --- a/config/config.go +++ b/config/config.go @@ -1,41 +1,71 @@ package config type Config struct { - OfflineUUID bool `yaml:"offlineUUID"` - Port string `yaml:"port"` - Log Log `yaml:"log"` - Sql Sql `yaml:"sql"` - Debug bool `yaml:"debug"` - Cache Cache `yaml:"cache"` - RaelIP bool `yaml:"raelIP"` - MaxIpUser int `yaml:"maxIpUser"` - RsaPriKey string `yaml:"rsaPriKey"` - TexturePath string `yaml:"texturePath"` - TextureBaseUrl string `yaml:"textureBaseUrl"` - WebBaseUrl string `yaml:"webBaseUrl"` - ServerName string `yaml:"serverName"` - Captcha Captcha `yaml:"captcha"` + OfflineUUID bool `toml:"offlineUUID" comment:"为 true 则 uuid 生成方式于离线模式相同,若从离线模式切换不会丢失数据。\n已有用户数据的情况下勿更改此项"` + Port string `toml:"port"` + Log Log `toml:"log"` + Sql Sql `toml:"sql"` + Debug bool `toml:"debug" comment:"输出每条执行的 sql 语句"` + Cache Cache `toml:"cache"` + RaelIP bool `toml:"raelIP" comment:"位于反向代理后启用,用于记录真实 ip\n若直接提供服务,请勿打开,否则会被伪造 ip"` + MaxIpUser int `toml:"maxIpUser" comment:"ip 段最大注册用户,ipv4 为 /24 ipv6 为 /48"` + RsaPriKey string `toml:"rsaPriKey,multiline" comment:"运行后勿修改,若为集群需设置为一致"` + TexturePath string `toml:"texturePath" comment:"材质文件保存路径,如果需要对象存储可以把对象储存挂载到本地目录上"` + TextureBaseUrl string `toml:"textureBaseUrl" comment:"材质静态文件提供基础地址\n如果静态文件位于 oss 上,比如 https://s3.amazonaws.com/example/1.png\n则填写 https://s3.amazonaws.com/example"` + WebBaseUrl string `toml:"webBaseUrl" comment:"用于在支持的启动器中展示本站的注册地址\n填写类似 https://example.com"` + ServerName string `toml:"serverName" comment:"皮肤站名字,用于在多个地方展示"` + Captcha Captcha `toml:"captcha"` } type Log struct { - Level string `yaml:"level"` - Json bool `yaml:"json"` + Level string `toml:"level"` + Json bool `toml:"json" comment:"json 格式输出"` } type Sql struct { - DriverName string `yaml:"driverName"` - Dsn string `yaml:"dsn"` + DriverName string `toml:"driverName" comment:"可填 mysql 或 sqlite3"` + Dsn string `toml:"dsn" comment:"填写见 mysql https://github.com/go-sql-driver/mysql#dsn-data-source-name\nsqlite https://github.com/mattn/go-sqlite3\n例如 mysql 用户名:密码@tcp(mysqlIP:端口)/数据库名\nsqlite data.db?_txlock=IMMEDIATE&_journal_mode=WAL&_fk=true"` } type Cache struct { - Type string `yaml:"type"` - Ram int `yaml:"ram"` - Addr string `yaml:"addr"` - Password string `yaml:"password"` + Type string `toml:"type" comment:"默认使用内存缓存,若需要集群部署,填写 redis"` + Ram int `toml:"ram" comment:"内存缓存使用大小,单位 b"` + Addr string `toml:"addr" comment:"redis 服务端地址,如 127.0.0.1:6379"` + Password string `toml:"password" comment:"redis 密码"` } type Captcha struct { - Type string `yaml:"type"` - SiteKey string `yaml:"siteKey"` - Secret string `yaml:"secret"` + Type string `toml:"type" comment:"验证码类型,目前只支持 cloudflare turnstile,若需要填写 turnstile"` + SiteKey string `toml:"siteKey"` + Secret string `toml:"secret"` +} + +func Default() Config { + return Config{ + OfflineUUID: true, + Port: ":8080", + Log: Log{ + Level: "debug", + Json: false, + }, + Sql: Sql{ + DriverName: "sqlite3", + Dsn: "data.db?_txlock=IMMEDIATE&_journal_mode=WAL&_fk=true", + }, + Debug: false, + Cache: Cache{ + Type: "", + Ram: 10000000, + Addr: "", + Password: "", + }, + RaelIP: false, + MaxIpUser: 0, + RsaPriKey: "", + TexturePath: "", + TextureBaseUrl: "", + WebBaseUrl: "", + ServerName: "没有设置名字", + Captcha: Captcha{}, + } } diff --git a/config/config.yaml b/config/config.yaml deleted file mode 100644 index 810cffb..0000000 --- a/config/config.yaml +++ /dev/null @@ -1,2 +0,0 @@ -sql: - dsn: "123" diff --git a/config/yaml.go b/config/yaml.go deleted file mode 100644 index b471736..0000000 --- a/config/yaml.go +++ /dev/null @@ -1,16 +0,0 @@ -package config - -import ( - "fmt" - - "gopkg.in/yaml.v3" -) - -func YamlDeCode(b []byte) (Config, error) { - var c Config - err := yaml.Unmarshal(b, &c) - if err != nil { - return c, fmt.Errorf("YamlDeCode: %w", err) - } - return c, nil -} diff --git a/config/yaml_test.go b/config/yaml_test.go deleted file mode 100644 index 71b7232..0000000 --- a/config/yaml_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package config - -import ( - "os" - "testing" -) - -func TestYamlDeCode(t *testing.T) { - b, err := os.ReadFile("config.yaml") - if err != nil { - t.Fatal(err) - } - c, err := YamlDeCode(b) - if err != nil { - t.Fatal(err) - } - if c.Sql.Dsn != "123" { - t.FailNow() - } -} diff --git a/go.mod b/go.mod index 642ef05..84d1a98 100644 --- a/go.mod +++ b/go.mod @@ -13,9 +13,11 @@ require ( github.com/golang-jwt/jwt/v5 v5.0.0 github.com/google/uuid v1.3.1 github.com/google/wire v0.5.0 + github.com/mattn/go-sqlite3 v1.14.17 + github.com/pelletier/go-toml/v2 v2.1.0 + github.com/redis/go-redis/v9 v9.2.1 github.com/samber/lo v1.38.1 golang.org/x/crypto v0.7.0 - gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -32,9 +34,7 @@ require ( github.com/google/go-cmp v0.5.6 // indirect github.com/hashicorp/hcl/v2 v2.13.0 // indirect github.com/leodido/go-urn v1.2.4 // indirect - github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect - github.com/redis/go-redis/v9 v9.2.1 // indirect github.com/zclconf/go-cty v1.8.0 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/mod v0.10.0 // indirect diff --git a/go.sum b/go.sum index cd05e44..b9be0bd 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,10 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw= github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -68,12 +72,12 @@ github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3v github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= -github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= -github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg= @@ -88,8 +92,9 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4= github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI= github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA= @@ -122,7 +127,6 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=