Xray-core/infra/conf/shadowtls.go

103 lines
3.2 KiB
Go
Raw Permalink Normal View History

2023-02-21 13:19:47 +02:00
package conf
import (
"github.com/golang/protobuf/proto"
"github.com/sagernet/sing/common"
"github.com/xtls/xray-core/proxy/shadowtls"
)
type ShadowTLSServerConfig struct {
Version uint16 `json:"version"`
Password string `json:"password,omitempty"`
Users []ShadowTLSUser `json:"users,omitempty"`
Handshake *ShadowTLSHandshakeConfig `json:"handshake"`
HandshakeForServerName map[string]*ShadowTLSHandshakeConfig `json:"handshakeForServerName,omitempty"`
StrictMode bool `json:"strictMode,omitempty"`
Detour string `json:"detour"`
}
type ShadowTLSUser struct {
Email string `json:"email,omitempty"`
Password string `json:"password,omitempty"`
}
type ShadowTLSHandshakeConfig struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
}
func (c *ShadowTLSServerConfig) Build() (proto.Message, error) {
2023-02-21 13:48:09 +02:00
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.")
}
2023-02-21 13:19:47 +02:00
var handshakeForServerName map[string]*shadowtls.HandshakeConfig
if c.HandshakeForServerName != nil {
for serverName, serverConfig := range c.HandshakeForServerName {
2023-02-21 13:48:09 +02:00
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),
}
2023-02-21 13:19:47 +02:00
}
}
2023-02-21 13:48:09 +02:00
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.")
}
2023-02-21 13:19:47 +02:00
return &shadowtls.ServerConfig{
2023-02-21 13:48:09 +02:00
Version: uint32(c.Version),
Password: c.Password,
Users: common.Map(c.Users, func(it ShadowTLSUser) *shadowtls.User {
return &shadowtls.User{
Email: it.Email,
Password: it.Password,
}
}),
Handshake: &shadowtls.HandshakeConfig{
Address: c.Handshake.Address.Build(),
Port: uint32(c.Handshake.Port),
},
2023-02-21 13:19:47 +02:00
HandshakeForServerName: handshakeForServerName,
StrictMode: c.StrictMode,
Detour: c.Detour,
}, nil
}
type ShadowTLSClientConfig struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
Version uint16 `json:"version"`
Password string `json:"password,omitempty"`
}
func (c *ShadowTLSClientConfig) Build() (proto.Message, error) {
2023-02-21 13:48:09 +02:00
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.")
}
2023-02-21 13:19:47 +02:00
return &shadowtls.ClientConfig{
Address: c.Address.Build(),
Port: uint32(c.Port),
Version: uint32(c.Version),
Password: c.Password,
}, nil
}