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
|
|
|
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
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,
|
2015-09-16 11:07:05 -04:00
|
|
|
}, 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-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-20 10:03:12 -04:00
|
|
|
Network string `json:"network"`
|
2015-09-12 05:51:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config VNextConfig) ToVNextServer() 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)
|
|
|
|
dest := v2net.NewTCPDestination(address)
|
2015-09-20 10:03:12 -04:00
|
|
|
if config.Network == "udp" {
|
2015-09-20 12:22:29 -04:00
|
|
|
dest = v2net.NewUDPDestination(address)
|
2015-09-20 10:03:12 -04:00
|
|
|
}
|
2015-09-12 05:51:42 -04:00
|
|
|
return VNextServer{
|
2015-09-20 12:22:29 -04:00
|
|
|
Destination: dest,
|
2015-09-20 10:03:12 -04:00
|
|
|
Users: users,
|
2015-09-16 11:07:05 -04:00
|
|
|
}
|
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
|
|
|
|
}
|