Xray-core/infra/conf/trojan.go

179 lines
4.6 KiB
Go
Raw Normal View History

2020-11-25 13:01:53 +02:00
package conf
import (
"encoding/json"
"runtime"
"strconv"
"syscall"
2020-12-04 03:36:16 +02:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/proxy/trojan"
"google.golang.org/protobuf/proto"
2020-11-25 13:01:53 +02:00
)
// TrojanServerTarget is configuration of a single trojan server
type TrojanServerTarget struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
Password string `json:"password"`
Email string `json:"email"`
Level byte `json:"level"`
Flow string `json:"flow"`
}
// TrojanClientConfig is configuration of trojan servers
type TrojanClientConfig struct {
Servers []*TrojanServerTarget `json:"servers"`
}
// Build implements Buildable
func (c *TrojanClientConfig) Build() (proto.Message, error) {
if len(c.Servers) == 0 {
return nil, newError("0 Trojan server configured.")
}
2023-07-06 18:18:05 +03:00
config := &trojan.ClientConfig{
Server: make([]*protocol.ServerEndpoint, len(c.Servers)),
}
2020-11-25 13:01:53 +02:00
for idx, rec := range c.Servers {
if rec.Address == nil {
return nil, newError("Trojan server address is not set.")
}
if rec.Port == 0 {
return nil, newError("Invalid Trojan port.")
}
if rec.Password == "" {
return nil, newError("Trojan password is not specified.")
}
2023-07-06 18:18:05 +03:00
if rec.Flow != "" {
return nil, newError(`Trojan doesn't support "flow" anymore.`)
2020-12-14 11:05:15 +02:00
}
2023-07-06 18:18:05 +03:00
config.Server[idx] = &protocol.ServerEndpoint{
2020-11-25 13:01:53 +02:00
Address: rec.Address.Build(),
Port: uint32(rec.Port),
User: []*protocol.User{
{
2023-07-06 18:18:05 +03:00
Level: uint32(rec.Level),
Email: rec.Email,
Account: serial.ToTypedMessage(&trojan.Account{
Password: rec.Password,
}),
2020-11-25 13:01:53 +02:00
},
},
}
}
return config, nil
}
// TrojanInboundFallback is fallback configuration
type TrojanInboundFallback struct {
Name string `json:"name"`
2020-11-25 13:01:53 +02:00
Alpn string `json:"alpn"`
Path string `json:"path"`
Type string `json:"type"`
Dest json.RawMessage `json:"dest"`
Xver uint64 `json:"xver"`
}
// TrojanUserConfig is user configuration
type TrojanUserConfig struct {
Password string `json:"password"`
Level byte `json:"level"`
Email string `json:"email"`
Flow string `json:"flow"`
}
// TrojanServerConfig is Inbound configuration
type TrojanServerConfig struct {
Clients []*TrojanUserConfig `json:"clients"`
Fallback *TrojanInboundFallback `json:"fallback"`
2020-11-25 13:01:53 +02:00
Fallbacks []*TrojanInboundFallback `json:"fallbacks"`
}
// Build implements Buildable
func (c *TrojanServerConfig) Build() (proto.Message, error) {
2023-07-06 18:18:05 +03:00
config := &trojan.ServerConfig{
Users: make([]*protocol.User, len(c.Clients)),
}
2020-11-25 13:01:53 +02:00
for idx, rawUser := range c.Clients {
2023-07-06 18:18:05 +03:00
if rawUser.Flow != "" {
return nil, newError(`Trojan doesn't support "flow" anymore.`)
2020-11-25 13:01:53 +02:00
}
2023-07-06 18:18:05 +03:00
config.Users[idx] = &protocol.User{
Level: uint32(rawUser.Level),
Email: rawUser.Email,
Account: serial.ToTypedMessage(&trojan.Account{
Password: rawUser.Password,
}),
2020-12-14 11:05:15 +02:00
}
2020-11-25 13:01:53 +02:00
}
if c.Fallback != nil {
return nil, newError(`Trojan settings: please use "fallbacks":[{}] instead of "fallback":{}`)
}
for _, fb := range c.Fallbacks {
var i uint16
var s string
if err := json.Unmarshal(fb.Dest, &i); err == nil {
s = strconv.Itoa(int(i))
} else {
_ = json.Unmarshal(fb.Dest, &s)
}
config.Fallbacks = append(config.Fallbacks, &trojan.Fallback{
Name: fb.Name,
2020-11-25 13:01:53 +02:00
Alpn: fb.Alpn,
Path: fb.Path,
Type: fb.Type,
Dest: s,
Xver: fb.Xver,
})
}
for _, fb := range config.Fallbacks {
/*
if fb.Alpn == "h2" && fb.Path != "" {
return nil, newError(`Trojan fallbacks: "alpn":"h2" doesn't support "path"`)
}
*/
if fb.Path != "" && fb.Path[0] != '/' {
return nil, newError(`Trojan fallbacks: "path" must be empty or start with "/"`)
}
if fb.Type == "" && fb.Dest != "" {
if fb.Dest == "serve-ws-none" {
fb.Type = "serve"
} else {
switch fb.Dest[0] {
case '@', '/':
fb.Type = "unix"
2020-12-24 21:45:35 +02:00
if fb.Dest[0] == '@' && len(fb.Dest) > 1 && fb.Dest[1] == '@' && (runtime.GOOS == "linux" || runtime.GOOS == "android") {
2020-11-25 13:01:53 +02:00
fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy
copy(fullAddr, fb.Dest[1:])
fb.Dest = string(fullAddr)
}
default:
if _, err := strconv.Atoi(fb.Dest); err == nil {
fb.Dest = "127.0.0.1:" + fb.Dest
}
if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
fb.Type = "tcp"
}
}
}
}
if fb.Type == "" {
return nil, newError(`Trojan fallbacks: please fill in a valid value for every "dest"`)
}
if fb.Xver > 2 {
return nil, newError(`Trojan fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
}
}
return config, nil
}