mirror of https://github.com/v2ray/v2ray-core
refactor
parent
4059a965ff
commit
1c2b6d9536
|
@ -41,23 +41,23 @@ func UDPDestination(address Address, port Port) Destination {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Destination) NetAddr() string {
|
func (d Destination) NetAddr() string {
|
||||||
return v.Address.String() + ":" + v.Port.String()
|
return d.Address.String() + ":" + d.Port.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Destination) String() string {
|
func (d Destination) String() string {
|
||||||
return v.Network.URLPrefix() + ":" + v.NetAddr()
|
return d.Network.URLPrefix() + ":" + d.NetAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Destination) IsValid() bool {
|
func (d Destination) IsValid() bool {
|
||||||
return v.Network != Network_Unknown
|
return d.Network != Network_Unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsDestination converts current Enpoint into Destination.
|
// AsDestination converts current Enpoint into Destination.
|
||||||
func (v *Endpoint) AsDestination() Destination {
|
func (p *Endpoint) AsDestination() Destination {
|
||||||
return Destination{
|
return Destination{
|
||||||
Network: v.Network,
|
Network: p.Network,
|
||||||
Address: v.Address.AsAddress(),
|
Address: p.Address.AsAddress(),
|
||||||
Port: Port(v.Port),
|
Port: Port(p.Port),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,32 +35,32 @@ func ipMaskToByte(mask net.IPMask) byte {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *IPNet) Add(ipNet *net.IPNet) {
|
func (n *IPNet) Add(ipNet *net.IPNet) {
|
||||||
ipv4 := ipNet.IP.To4()
|
ipv4 := ipNet.IP.To4()
|
||||||
if ipv4 == nil {
|
if ipv4 == nil {
|
||||||
// For now, we don't support IPv6
|
// For now, we don't support IPv6
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mask := ipMaskToByte(ipNet.Mask)
|
mask := ipMaskToByte(ipNet.Mask)
|
||||||
v.AddIP(ipv4, mask)
|
n.AddIP(ipv4, mask)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *IPNet) AddIP(ip []byte, mask byte) {
|
func (n *IPNet) AddIP(ip []byte, mask byte) {
|
||||||
k := ipToUint32(ip)
|
k := ipToUint32(ip)
|
||||||
existing, found := v.cache[k]
|
existing, found := n.cache[k]
|
||||||
if !found || existing > mask {
|
if !found || existing > mask {
|
||||||
v.cache[k] = mask
|
n.cache[k] = mask
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *IPNet) Contains(ip net.IP) bool {
|
func (n *IPNet) Contains(ip net.IP) bool {
|
||||||
ipv4 := ip.To4()
|
ipv4 := ip.To4()
|
||||||
if ipv4 == nil {
|
if ipv4 == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
originalValue := ipToUint32(ipv4)
|
originalValue := ipToUint32(ipv4)
|
||||||
|
|
||||||
if entry, found := v.cache[originalValue]; found {
|
if entry, found := n.cache[originalValue]; found {
|
||||||
if entry == 32 {
|
if entry == 32 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ func (v *IPNet) Contains(ip net.IP) bool {
|
||||||
mask += 1 << uint32(32-maskbit)
|
mask += 1 << uint32(32-maskbit)
|
||||||
|
|
||||||
maskedValue := originalValue & mask
|
maskedValue := originalValue & mask
|
||||||
if entry, found := v.cache[maskedValue]; found {
|
if entry, found := n.cache[maskedValue]; found {
|
||||||
if entry == maskbit {
|
if entry == maskbit {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -80,8 +80,8 @@ func (v *IPNet) Contains(ip net.IP) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *IPNet) IsEmpty() bool {
|
func (n *IPNet) IsEmpty() bool {
|
||||||
return len(v.cache) == 0
|
return len(n.cache) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -18,14 +18,14 @@ func ParseNetwork(nwStr string) Network {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Network) AsList() *NetworkList {
|
func (n Network) AsList() *NetworkList {
|
||||||
return &NetworkList{
|
return &NetworkList{
|
||||||
Network: []Network{v},
|
Network: []Network{n},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Network) SystemString() string {
|
func (n Network) SystemString() string {
|
||||||
switch v {
|
switch n {
|
||||||
case Network_TCP:
|
case Network_TCP:
|
||||||
return "tcp"
|
return "tcp"
|
||||||
case Network_UDP:
|
case Network_UDP:
|
||||||
|
@ -35,8 +35,8 @@ func (v Network) SystemString() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Network) URLPrefix() string {
|
func (n Network) URLPrefix() string {
|
||||||
switch v {
|
switch n {
|
||||||
case Network_TCP:
|
case Network_TCP:
|
||||||
return "tcp"
|
return "tcp"
|
||||||
case Network_UDP:
|
case Network_UDP:
|
||||||
|
@ -47,8 +47,8 @@ func (v Network) URLPrefix() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasNetwork returns true if the given network is in v NetworkList.
|
// HasNetwork returns true if the given network is in v NetworkList.
|
||||||
func (v NetworkList) HasNetwork(network Network) bool {
|
func (l NetworkList) HasNetwork(network Network) bool {
|
||||||
for _, value := range v.Network {
|
for _, value := range l.Network {
|
||||||
if string(value) == string(network) {
|
if string(value) == string(network) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -56,11 +56,11 @@ func (v NetworkList) HasNetwork(network Network) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v NetworkList) Get(idx int) Network {
|
func (l NetworkList) Get(idx int) Network {
|
||||||
return v.Network[idx]
|
return l.Network[idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the number of networks in this network list.
|
// Size returns the number of networks in this network list.
|
||||||
func (v NetworkList) Size() int {
|
func (l NetworkList) Size() int {
|
||||||
return len(v.Network)
|
return len(l.Network)
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,38 +34,40 @@ func PortFromString(s string) (Port, error) {
|
||||||
return PortFromInt(uint32(val))
|
return PortFromInt(uint32(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value return the correspoding uint16 value of v Port.
|
// Value return the correspoding uint16 value of a Port.
|
||||||
func (v Port) Value() uint16 {
|
func (p Port) Value() uint16 {
|
||||||
return uint16(v)
|
return uint16(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes returns the correspoding bytes of v Port, in big endian order.
|
// Bytes returns the correspoding bytes of a Port, in big endian order.
|
||||||
func (v Port) Bytes(b []byte) []byte {
|
func (p Port) Bytes(b []byte) []byte {
|
||||||
return serial.Uint16ToBytes(v.Value(), b)
|
return serial.Uint16ToBytes(p.Value(), b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the string presentation of v Port.
|
// String returns the string presentation of a Port.
|
||||||
func (v Port) String() string {
|
func (p Port) String() string {
|
||||||
return serial.Uint16ToString(v.Value())
|
return serial.Uint16ToString(p.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v PortRange) FromPort() Port {
|
// FromPort returns the begining port of this PortRange.
|
||||||
return Port(v.From)
|
func (p PortRange) FromPort() Port {
|
||||||
|
return Port(p.From)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v PortRange) ToPort() Port {
|
// ToPort returns the end port of this PortRange.
|
||||||
return Port(v.To)
|
func (p PortRange) ToPort() Port {
|
||||||
|
return Port(p.To)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contains returns true if the given port is within the range of v PortRange.
|
// Contains returns true if the given port is within the range of a PortRange.
|
||||||
func (v PortRange) Contains(port Port) bool {
|
func (p PortRange) Contains(port Port) bool {
|
||||||
return v.FromPort() <= port && port <= v.ToPort()
|
return p.FromPort() <= port && port <= p.ToPort()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SinglePortRange returns a PortRange contains a single port.
|
// SinglePortRange returns a PortRange contains a single port.
|
||||||
func SinglePortRange(v Port) *PortRange {
|
func SinglePortRange(p Port) *PortRange {
|
||||||
return &PortRange{
|
return &PortRange{
|
||||||
From: uint32(v),
|
From: uint32(p),
|
||||||
To: uint32(v),
|
To: uint32(p),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,12 @@ const (
|
||||||
userKey key = iota
|
userKey key = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ContextWithUser returns a context combined with an User.
|
||||||
func ContextWithUser(ctx context.Context, user *User) context.Context {
|
func ContextWithUser(ctx context.Context, user *User) context.Context {
|
||||||
return context.WithValue(ctx, userKey, user)
|
return context.WithValue(ctx, userKey, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserFromContext extracts an User from the given context, if any.
|
||||||
func UserFromContext(ctx context.Context) *User {
|
func UserFromContext(ctx context.Context) *User {
|
||||||
v := ctx.Value(userKey)
|
v := ctx.Value(userKey)
|
||||||
if v == nil {
|
if v == nil {
|
||||||
|
|
Loading…
Reference in New Issue