1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-04 21:15:24 +00:00
v2fly/proxy/vmess/config.go

76 lines
1.8 KiB
Go
Raw Normal View History

2015-09-12 09:51:42 +00:00
package vmess
import (
"encoding/json"
2015-09-15 22:06:22 +00:00
"net"
2015-09-12 09:51:42 +00:00
2015-09-19 22:50:21 +00:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-09-19 22:50:21 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
2015-09-12 09:51:42 +00:00
)
2015-09-21 17:56:58 +00:00
// VMessUser is an authenticated user account in VMess configuration.
2015-09-12 18:36:21 +00:00
type VMessUser struct {
Id string `json:"id"`
Email string `json:"email"`
}
2015-09-19 22:11:14 +00:00
func (u *VMessUser) ToUser() (user.User, error) {
id, err := user.NewID(u.Id)
return user.User{
2015-09-16 20:19:42 +00:00
Id: id,
}, err
2015-09-12 18:36:21 +00:00
}
2015-09-21 17:57:30 +00:00
// VMessInboundConfig is
2015-09-12 09:51:42 +00:00
type VMessInboundConfig struct {
2015-09-12 18:36:21 +00:00
AllowedClients []VMessUser `json:"clients"`
2015-09-12 09:51:42 +00:00
}
func loadInboundConfig(rawConfig []byte) (VMessInboundConfig, error) {
config := VMessInboundConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
}
type VNextConfig struct {
2015-09-12 18:36:21 +00:00
Address string `json:"address"`
Port uint16 `json:"port"`
Users []VMessUser `json:"users"`
Network string `json:"network"`
2015-09-12 09:51:42 +00:00
}
func (config VNextConfig) ToVNextServer() VNextServer {
2015-09-19 22:11:14 +00:00
users := make([]user.User, 0, len(config.Users))
2015-09-12 18:36:21 +00:00
for _, user := range config.Users {
2015-09-12 20:11:54 +00:00
vuser, err := user.ToUser()
2015-09-12 18:36:21 +00:00
if err != nil {
2015-09-12 20:11:54 +00:00
panic(log.Error("Failed to convert %v to User.", user))
2015-09-12 18:36:21 +00:00
}
users = append(users, vuser)
}
2015-09-15 22:06:22 +00:00
ip := net.ParseIP(config.Address)
if ip == nil {
panic(log.Error("Unable to parse VNext IP: %s", config.Address))
}
2015-09-20 16:22:29 +00:00
address := v2net.IPAddress(ip, config.Port)
dest := v2net.NewTCPDestination(address)
if config.Network == "udp" {
2015-09-20 16:22:29 +00:00
dest = v2net.NewUDPDestination(address)
}
2015-09-12 09:51:42 +00:00
return VNextServer{
2015-09-20 16:22:29 +00:00
Destination: dest,
Users: users,
}
2015-09-12 09:51:42 +00:00
}
type VMessOutboundConfig struct {
2015-09-12 18:36:21 +00:00
VNextList []VNextConfig `json:"vnext"`
2015-09-12 09:51:42 +00:00
}
func loadOutboundConfig(rawConfig []byte) (VMessOutboundConfig, error) {
config := VMessOutboundConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
}