1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-15 16:56:08 -05:00
This commit is contained in:
Darien Raymond 2017-08-22 15:15:09 +02:00
parent 4059a965ff
commit 1c2b6d9536
5 changed files with 55 additions and 51 deletions

View File

@ -41,23 +41,23 @@ func UDPDestination(address Address, port Port) Destination {
}
}
func (v Destination) NetAddr() string {
return v.Address.String() + ":" + v.Port.String()
func (d Destination) NetAddr() string {
return d.Address.String() + ":" + d.Port.String()
}
func (v Destination) String() string {
return v.Network.URLPrefix() + ":" + v.NetAddr()
func (d Destination) String() string {
return d.Network.URLPrefix() + ":" + d.NetAddr()
}
func (v Destination) IsValid() bool {
return v.Network != Network_Unknown
func (d Destination) IsValid() bool {
return d.Network != Network_Unknown
}
// AsDestination converts current Enpoint into Destination.
func (v *Endpoint) AsDestination() Destination {
func (p *Endpoint) AsDestination() Destination {
return Destination{
Network: v.Network,
Address: v.Address.AsAddress(),
Port: Port(v.Port),
Network: p.Network,
Address: p.Address.AsAddress(),
Port: Port(p.Port),
}
}

View File

@ -35,32 +35,32 @@ func ipMaskToByte(mask net.IPMask) byte {
return value
}
func (v *IPNet) Add(ipNet *net.IPNet) {
func (n *IPNet) Add(ipNet *net.IPNet) {
ipv4 := ipNet.IP.To4()
if ipv4 == nil {
// For now, we don't support IPv6
return
}
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)
existing, found := v.cache[k]
existing, found := n.cache[k]
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()
if ipv4 == nil {
return false
}
originalValue := ipToUint32(ipv4)
if entry, found := v.cache[originalValue]; found {
if entry, found := n.cache[originalValue]; found {
if entry == 32 {
return true
}
@ -71,7 +71,7 @@ func (v *IPNet) Contains(ip net.IP) bool {
mask += 1 << uint32(32-maskbit)
maskedValue := originalValue & mask
if entry, found := v.cache[maskedValue]; found {
if entry, found := n.cache[maskedValue]; found {
if entry == maskbit {
return true
}
@ -80,8 +80,8 @@ func (v *IPNet) Contains(ip net.IP) bool {
return false
}
func (v *IPNet) IsEmpty() bool {
return len(v.cache) == 0
func (n *IPNet) IsEmpty() bool {
return len(n.cache) == 0
}
func init() {

View File

@ -18,14 +18,14 @@ func ParseNetwork(nwStr string) Network {
}
}
func (v Network) AsList() *NetworkList {
func (n Network) AsList() *NetworkList {
return &NetworkList{
Network: []Network{v},
Network: []Network{n},
}
}
func (v Network) SystemString() string {
switch v {
func (n Network) SystemString() string {
switch n {
case Network_TCP:
return "tcp"
case Network_UDP:
@ -35,8 +35,8 @@ func (v Network) SystemString() string {
}
}
func (v Network) URLPrefix() string {
switch v {
func (n Network) URLPrefix() string {
switch n {
case Network_TCP:
return "tcp"
case Network_UDP:
@ -47,8 +47,8 @@ func (v Network) URLPrefix() string {
}
// HasNetwork returns true if the given network is in v NetworkList.
func (v NetworkList) HasNetwork(network Network) bool {
for _, value := range v.Network {
func (l NetworkList) HasNetwork(network Network) bool {
for _, value := range l.Network {
if string(value) == string(network) {
return true
}
@ -56,11 +56,11 @@ func (v NetworkList) HasNetwork(network Network) bool {
return false
}
func (v NetworkList) Get(idx int) Network {
return v.Network[idx]
func (l NetworkList) Get(idx int) Network {
return l.Network[idx]
}
// Size returns the number of networks in this network list.
func (v NetworkList) Size() int {
return len(v.Network)
func (l NetworkList) Size() int {
return len(l.Network)
}

View File

@ -34,38 +34,40 @@ func PortFromString(s string) (Port, error) {
return PortFromInt(uint32(val))
}
// Value return the correspoding uint16 value of v Port.
func (v Port) Value() uint16 {
return uint16(v)
// Value return the correspoding uint16 value of a Port.
func (p Port) Value() uint16 {
return uint16(p)
}
// Bytes returns the correspoding bytes of v Port, in big endian order.
func (v Port) Bytes(b []byte) []byte {
return serial.Uint16ToBytes(v.Value(), b)
// Bytes returns the correspoding bytes of a Port, in big endian order.
func (p Port) Bytes(b []byte) []byte {
return serial.Uint16ToBytes(p.Value(), b)
}
// String returns the string presentation of v Port.
func (v Port) String() string {
return serial.Uint16ToString(v.Value())
// String returns the string presentation of a Port.
func (p Port) String() string {
return serial.Uint16ToString(p.Value())
}
func (v PortRange) FromPort() Port {
return Port(v.From)
// FromPort returns the begining port of this PortRange.
func (p PortRange) FromPort() Port {
return Port(p.From)
}
func (v PortRange) ToPort() Port {
return Port(v.To)
// ToPort returns the end port of this PortRange.
func (p PortRange) ToPort() Port {
return Port(p.To)
}
// Contains returns true if the given port is within the range of v PortRange.
func (v PortRange) Contains(port Port) bool {
return v.FromPort() <= port && port <= v.ToPort()
// Contains returns true if the given port is within the range of a PortRange.
func (p PortRange) Contains(port Port) bool {
return p.FromPort() <= port && port <= p.ToPort()
}
// SinglePortRange returns a PortRange contains a single port.
func SinglePortRange(v Port) *PortRange {
func SinglePortRange(p Port) *PortRange {
return &PortRange{
From: uint32(v),
To: uint32(v),
From: uint32(p),
To: uint32(p),
}
}

View File

@ -10,10 +10,12 @@ const (
userKey key = iota
)
// ContextWithUser returns a context combined with an User.
func ContextWithUser(ctx context.Context, user *User) context.Context {
return context.WithValue(ctx, userKey, user)
}
// UserFromContext extracts an User from the given context, if any.
func UserFromContext(ctx context.Context) *User {
v := ctx.Value(userKey)
if v == nil {