1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/proxy/vmess/config.go

83 lines
1.9 KiB
Go
Raw Normal View History

2015-09-12 05:51:42 -04:00
package vmess
import (
2015-09-15 18:06:22 -04:00
"net"
2015-10-02 15:55:37 -04:00
"strings"
2015-09-12 05:51:42 -04:00
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-10-06 17:11:08 -04:00
"github.com/v2ray/v2ray-core/config"
"github.com/v2ray/v2ray-core/config/json"
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
2015-09-12 05:51:42 -04:00
)
2015-09-21 13:56:58 -04:00
// VMessUser is an authenticated user account in VMess configuration.
2015-09-12 14:36:21 -04:00
type VMessUser struct {
Id string `json:"id"`
Email string `json:"email"`
}
2015-09-19 18:11:14 -04:00
func (u *VMessUser) ToUser() (user.User, error) {
id, err := user.NewID(u.Id)
return user.User{
2015-09-16 16:19:42 -04:00
Id: id,
}, err
2015-09-12 14:36:21 -04:00
}
2015-09-21 13:57:30 -04:00
// VMessInboundConfig is
2015-09-12 05:51:42 -04:00
type VMessInboundConfig struct {
2015-09-12 14:36:21 -04:00
AllowedClients []VMessUser `json:"clients"`
2015-10-03 05:34:01 -04:00
UDPEnabled bool `json:"udp"`
2015-09-12 05:51:42 -04:00
}
type VNextConfig struct {
2015-09-12 14:36:21 -04:00
Address string `json:"address"`
Port uint16 `json:"port"`
Users []VMessUser `json:"users"`
Network string `json:"network"`
2015-09-12 05:51:42 -04:00
}
2015-10-02 15:55:37 -04:00
func (config VNextConfig) HasNetwork(network string) bool {
return strings.Contains(config.Network, network)
}
2015-10-06 10:43:50 -04:00
func (config VNextConfig) ToVNextServer(network string) VNextServer {
2015-09-19 18:11:14 -04:00
users := make([]user.User, 0, len(config.Users))
2015-09-12 14:36:21 -04:00
for _, user := range config.Users {
2015-09-12 16:11:54 -04:00
vuser, err := user.ToUser()
2015-09-12 14:36:21 -04:00
if err != nil {
2015-09-12 16:11:54 -04:00
panic(log.Error("Failed to convert %v to User.", user))
2015-09-12 14:36:21 -04:00
}
users = append(users, vuser)
}
2015-09-15 18:06:22 -04:00
ip := net.ParseIP(config.Address)
if ip == nil {
panic(log.Error("Unable to parse VNext IP: %s", config.Address))
}
2015-09-20 12:22:29 -04:00
address := v2net.IPAddress(ip, config.Port)
2015-10-06 11:24:57 -04:00
var dest v2net.Destination
if network == "tcp" {
dest = v2net.NewTCPDestination(address)
} else {
dest = v2net.NewUDPDestination(address)
}
2015-09-12 05:51:42 -04:00
return VNextServer{
2015-09-20 12:22:29 -04:00
Destination: dest,
Users: users,
}
2015-09-12 05:51:42 -04:00
}
type VMessOutboundConfig struct {
2015-09-12 14:36:21 -04:00
VNextList []VNextConfig `json:"vnext"`
2015-09-12 05:51:42 -04:00
}
2015-10-06 17:11:08 -04:00
func init() {
json.RegisterConfigType("vmess", config.TypeInbound, func() interface{} {
return new(VMessInboundConfig)
})
json.RegisterConfigType("vmess", config.TypeOutbound, func() interface{} {
return new(VMessOutboundConfig)
})
2015-09-12 05:51:42 -04:00
}