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"
|
2015-09-19 17:54:36 -04:00
|
|
|
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,
|
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-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"`
|
2015-09-20 10:03:12 -04:00
|
|
|
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-13 12:27:29 -04:00
|
|
|
func (c VNextConfig) ToVNextServer(network string) (*VNextServer, error) {
|
|
|
|
users := make([]user.User, 0, len(c.Users))
|
|
|
|
for _, user := range c.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-10-13 12:27:29 -04:00
|
|
|
log.Error("Failed to convert %v to User.", user)
|
|
|
|
return nil, config.BadConfiguration
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
|
|
|
users = append(users, vuser)
|
|
|
|
}
|
2015-10-13 12:27:29 -04:00
|
|
|
ip := net.ParseIP(c.Address)
|
2015-09-15 18:06:22 -04:00
|
|
|
if ip == nil {
|
2015-10-13 12:27:29 -04:00
|
|
|
log.Error("Unable to parse VNext IP: %s", c.Address)
|
|
|
|
return nil, config.BadConfiguration
|
2015-09-15 18:06:22 -04:00
|
|
|
}
|
2015-10-13 12:27:29 -04:00
|
|
|
address := v2net.IPAddress(ip, c.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-10-13 12:27:29 -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-10-13 12:27:29 -04:00
|
|
|
}, nil
|
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
|
|
|
}
|