-
Notifications
You must be signed in to change notification settings - Fork 0
/
outbound.go
63 lines (55 loc) · 1.6 KB
/
outbound.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package json
import (
"encoding/json"
"net"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config"
)
type RawConfigTarget struct {
Address string `json:"address"`
Port uint16 `json:"port"`
Users []*ConfigUser `json:"users"`
}
type ConfigTarget struct {
Address v2net.Address
Users []*ConfigUser
}
func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
var rawConfig RawConfigTarget
if err := json.Unmarshal(data, &rawConfig); err != nil {
return err
}
t.Users = rawConfig.Users
ip := net.ParseIP(rawConfig.Address)
if ip == nil {
log.Error("Unable to parse IP: %s", rawConfig.Address)
return proxyconfig.BadConfiguration
}
t.Address = v2net.IPAddress(ip, rawConfig.Port)
return nil
}
type Outbound struct {
TargetList []*ConfigTarget `json:"vnext"`
}
func (o *Outbound) Targets() []*vmessconfig.OutboundTarget {
targets := make([]*vmessconfig.OutboundTarget, 0, 2*len(o.TargetList))
for _, rawTarget := range o.TargetList {
users := make([]vmessconfig.User, 0, len(rawTarget.Users))
for _, rawUser := range rawTarget.Users {
users = append(users, rawUser)
}
targets = append(targets, &vmessconfig.OutboundTarget{
Destination: v2net.NewTCPDestination(rawTarget.Address),
Accounts: users,
})
}
return targets
}
func init() {
jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
return new(Outbound)
})
}