1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-03 17:23:43 -04:00
v2fly/net/vmess/config.go

65 lines
1.4 KiB
Go
Raw Normal View History

2015-09-12 05:51:42 -04:00
package vmess
import (
"encoding/json"
2015-09-15 18:06:22 -04:00
"net"
2015-09-12 05:51:42 -04:00
"github.com/v2ray/v2ray-core"
2015-09-12 14:36:21 -04:00
"github.com/v2ray/v2ray-core/log"
2015-09-12 05:51:42 -04:00
v2net "github.com/v2ray/v2ray-core/net"
)
2015-09-12 14:36:21 -04:00
type VMessUser struct {
Id string `json:"id"`
Email string `json:"email"`
}
2015-09-12 16:11:54 -04:00
func (u *VMessUser) ToUser() (core.User, error) {
2015-09-14 12:19:17 -04:00
id, err := core.NewID(u.Id)
2015-09-12 16:11:54 -04:00
return core.User{id}, err
2015-09-12 14:36:21 -04:00
}
2015-09-12 05:51:42 -04:00
type VMessInboundConfig struct {
2015-09-12 14:36:21 -04:00
AllowedClients []VMessUser `json:"clients"`
2015-09-12 05:51:42 -04:00
}
func loadInboundConfig(rawConfig []byte) (VMessInboundConfig, error) {
config := VMessInboundConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
}
type VNextConfig struct {
2015-09-12 14:36:21 -04:00
Address string `json:"address"`
Port uint16 `json:"port"`
Users []VMessUser `json:"users"`
2015-09-12 05:51:42 -04:00
}
func (config VNextConfig) ToVNextServer() VNextServer {
2015-09-12 16:11:54 -04:00
users := make([]core.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-12 05:51:42 -04:00
return VNextServer{
2015-09-15 06:54:06 -04:00
v2net.IPAddress(ip, config.Port),
2015-09-12 14:36:21 -04:00
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
}
func loadOutboundConfig(rawConfig []byte) (VMessOutboundConfig, error) {
config := VMessOutboundConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
}