Add config check

This commit is contained in:
世界 2023-02-21 19:48:09 +08:00
parent d6c2a9aab7
commit 6fb673aee4
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4

View File

@ -21,37 +21,55 @@ type ShadowTLSUser struct {
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
} }
func (c ShadowTLSUser) Build() *shadowtls.User {
return &shadowtls.User{
Email: c.Email,
Password: c.Password,
}
}
type ShadowTLSHandshakeConfig struct { type ShadowTLSHandshakeConfig struct {
Address *Address `json:"address"` Address *Address `json:"address"`
Port uint16 `json:"port"` Port uint16 `json:"port"`
} }
func (c ShadowTLSHandshakeConfig) Build() *shadowtls.HandshakeConfig {
return &shadowtls.HandshakeConfig{
Address: c.Address.Build(),
Port: uint32(c.Port),
}
}
func (c *ShadowTLSServerConfig) Build() (proto.Message, error) { func (c *ShadowTLSServerConfig) Build() (proto.Message, error) {
if c.Version == 0 {
return nil, newError("shadow-tls version is not set.")
}
if c.Version == 3 && len(c.Users) == 0 {
return nil, newError("shadow-tls users is not set.")
}
if c.Handshake == nil {
return nil, newError("shadow-tls handshake config is not set.")
}
var handshakeForServerName map[string]*shadowtls.HandshakeConfig var handshakeForServerName map[string]*shadowtls.HandshakeConfig
if c.HandshakeForServerName != nil { if c.HandshakeForServerName != nil {
for serverName, serverConfig := range c.HandshakeForServerName { for serverName, serverConfig := range c.HandshakeForServerName {
handshakeForServerName[serverName] = serverConfig.Build() if serverConfig.Address == nil {
return nil, newError("shadow-tls handshake server address is not set.")
}
if serverConfig.Port == 0 {
return nil, newError("shadow-tls handshake server port is not set.")
}
handshakeForServerName[serverName] = &shadowtls.HandshakeConfig{
Address: serverConfig.Address.Build(),
Port: uint32(serverConfig.Port),
}
} }
} }
if c.Handshake.Address == nil {
return nil, newError("shadow-tls handshake server address is not set.")
}
if c.Handshake.Port == 0 {
return nil, newError("shadow-tls handshake server port is not set.")
}
return &shadowtls.ServerConfig{ return &shadowtls.ServerConfig{
Version: uint32(c.Version), Version: uint32(c.Version),
Password: c.Password, Password: c.Password,
Users: common.Map(c.Users, ShadowTLSUser.Build), Users: common.Map(c.Users, func(it ShadowTLSUser) *shadowtls.User {
Handshake: c.Handshake.Build(), return &shadowtls.User{
Email: it.Email,
Password: it.Password,
}
}),
Handshake: &shadowtls.HandshakeConfig{
Address: c.Handshake.Address.Build(),
Port: uint32(c.Handshake.Port),
},
HandshakeForServerName: handshakeForServerName, HandshakeForServerName: handshakeForServerName,
StrictMode: c.StrictMode, StrictMode: c.StrictMode,
Detour: c.Detour, Detour: c.Detour,
@ -66,6 +84,15 @@ type ShadowTLSClientConfig struct {
} }
func (c *ShadowTLSClientConfig) Build() (proto.Message, error) { func (c *ShadowTLSClientConfig) Build() (proto.Message, error) {
if c.Version == 0 {
return nil, newError("shadow-tls version is not set.")
}
if c.Address == nil {
return nil, newError("shadow-tls server address is not set.")
}
if c.Port == 0 {
return nil, newError("shadow-tls server port is not set.")
}
return &shadowtls.ClientConfig{ return &shadowtls.ClientConfig{
Address: c.Address.Build(), Address: c.Address.Build(),
Port: uint32(c.Port), Port: uint32(c.Port),