1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-01 16:57:46 -04:00
v2fly/infra/conf/vless.go

174 lines
4.9 KiB
Go
Raw Normal View History

2020-07-28 11:00:23 -04:00
package conf
import (
"encoding/json"
"runtime"
2020-08-28 03:51:09 -04:00
"strconv"
"syscall"
2020-07-28 11:00:23 -04:00
"github.com/golang/protobuf/proto"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/proxy/vless"
"github.com/v2fly/v2ray-core/v4/proxy/vless/inbound"
"github.com/v2fly/v2ray-core/v4/proxy/vless/outbound"
2020-07-28 11:00:23 -04:00
)
type VLessInboundFallback struct {
2020-08-28 03:51:09 -04:00
Alpn string `json:"alpn"`
Path string `json:"path"`
Type string `json:"type"`
Dest json.RawMessage `json:"dest"`
Xver uint64 `json:"xver"`
2020-07-28 11:00:23 -04:00
}
type VLessInboundConfig struct {
2020-08-28 03:51:09 -04:00
Clients []json.RawMessage `json:"clients"`
Decryption string `json:"decryption"`
Fallback json.RawMessage `json:"fallback"`
Fallbacks []*VLessInboundFallback `json:"fallbacks"`
2020-07-28 11:00:23 -04:00
}
// Build implements Buildable
func (c *VLessInboundConfig) Build() (proto.Message, error) {
config := new(inbound.Config)
2020-08-28 03:51:09 -04:00
config.Clients = make([]*protocol.User, len(c.Clients))
for idx, rawUser := range c.Clients {
user := new(protocol.User)
if err := json.Unmarshal(rawUser, user); err != nil {
return nil, newError(`VLESS clients: invalid user`).Base(err)
2020-08-03 02:13:26 -04:00
}
2020-08-28 03:51:09 -04:00
account := new(vless.Account)
if err := json.Unmarshal(rawUser, account); err != nil {
return nil, newError(`VLESS clients: invalid user`).Base(err)
2020-07-28 11:00:23 -04:00
}
2020-08-28 03:51:09 -04:00
if account.Encryption != "" {
return nil, newError(`VLESS clients: "encryption" should not in inbound settings`)
2020-08-03 02:13:26 -04:00
}
2020-08-28 03:51:09 -04:00
user.Account = serial.ToTypedMessage(account)
config.Clients[idx] = user
2020-08-03 02:13:26 -04:00
}
2020-08-28 03:51:09 -04:00
if c.Decryption != "none" {
return nil, newError(`VLESS settings: please add/set "decryption":"none" to every settings`)
}
config.Decryption = c.Decryption
if c.Fallback != nil {
return nil, newError(`VLESS 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))
2020-08-03 02:13:26 -04:00
} else {
2020-08-28 03:51:09 -04:00
_ = json.Unmarshal(fb.Dest, &s)
}
config.Fallbacks = append(config.Fallbacks, &inbound.Fallback{
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(`VLESS fallbacks: "alpn":"h2" doesn't support "path"`)
2020-08-03 02:13:26 -04:00
}
2020-08-28 03:51:09 -04:00
*/
if fb.Path != "" && fb.Path[0] != '/' {
return nil, newError(`VLESS 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] {
2020-09-07 04:39:53 -04:00
case '@', '/':
2020-08-28 03:51:09 -04:00
fb.Type = "unix"
if fb.Dest[0] == '@' && len(fb.Dest) > 1 && fb.Dest[1] == '@' && (runtime.GOOS == "linux" || runtime.GOOS == "android") {
2020-10-29 03:30:38 -04:00
fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy
copy(fullAddr, fb.Dest[1:])
fb.Dest = string(fullAddr)
}
2020-08-28 03:51:09 -04:00
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"
}
}
2020-08-03 02:13:26 -04:00
}
}
2020-08-28 03:51:09 -04:00
if fb.Type == "" {
return nil, newError(`VLESS fallbacks: please fill in a valid value for every "dest"`)
2020-07-28 11:00:23 -04:00
}
2020-08-28 03:51:09 -04:00
if fb.Xver > 2 {
return nil, newError(`VLESS fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
2020-07-28 11:00:23 -04:00
}
}
return config, nil
}
2020-08-28 03:51:09 -04:00
type VLessOutboundVnext struct {
2020-07-28 11:00:23 -04:00
Address *Address `json:"address"`
Port uint16 `json:"port"`
Users []json.RawMessage `json:"users"`
}
type VLessOutboundConfig struct {
2020-08-28 03:51:09 -04:00
Vnext []*VLessOutboundVnext `json:"vnext"`
2020-07-28 11:00:23 -04:00
}
// Build implements Buildable
func (c *VLessOutboundConfig) Build() (proto.Message, error) {
config := new(outbound.Config)
2020-08-28 03:51:09 -04:00
if len(c.Vnext) == 0 {
return nil, newError(`VLESS settings: "vnext" is empty`)
2020-07-28 11:00:23 -04:00
}
2020-08-28 03:51:09 -04:00
config.Vnext = make([]*protocol.ServerEndpoint, len(c.Vnext))
for idx, rec := range c.Vnext {
2020-07-28 11:00:23 -04:00
if rec.Address == nil {
2020-08-28 03:51:09 -04:00
return nil, newError(`VLESS vnext: "address" is not set`)
}
if len(rec.Users) == 0 {
return nil, newError(`VLESS vnext: "users" is empty`)
2020-07-28 11:00:23 -04:00
}
spec := &protocol.ServerEndpoint{
Address: rec.Address.Build(),
Port: uint32(rec.Port),
2020-08-28 03:51:09 -04:00
User: make([]*protocol.User, len(rec.Users)),
2020-07-28 11:00:23 -04:00
}
2020-08-28 03:51:09 -04:00
for idx, rawUser := range rec.Users {
2020-07-28 11:00:23 -04:00
user := new(protocol.User)
if err := json.Unmarshal(rawUser, user); err != nil {
2020-08-28 03:51:09 -04:00
return nil, newError(`VLESS users: invalid user`).Base(err)
2020-07-28 11:00:23 -04:00
}
account := new(vless.Account)
if err := json.Unmarshal(rawUser, account); err != nil {
2020-08-28 03:51:09 -04:00
return nil, newError(`VLESS users: invalid user`).Base(err)
2020-07-28 11:00:23 -04:00
}
if account.Encryption != "none" {
2020-08-28 03:51:09 -04:00
return nil, newError(`VLESS users: please add/set "encryption":"none" for every user`)
2020-07-28 11:00:23 -04:00
}
user.Account = serial.ToTypedMessage(account)
2020-08-28 03:51:09 -04:00
spec.User[idx] = user
2020-07-28 11:00:23 -04:00
}
2020-08-28 03:51:09 -04:00
config.Vnext[idx] = spec
2020-07-28 11:00:23 -04:00
}
return config, nil
}