From 1c2b6d95364c632eb6f271e81de844b75430ce8b Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 22 Aug 2017 15:15:09 +0200 Subject: [PATCH] refactor --- common/net/destination.go | 20 +++++++++---------- common/net/ipnet.go | 20 +++++++++---------- common/net/network.go | 24 +++++++++++------------ common/net/port.go | 40 ++++++++++++++++++++------------------ common/protocol/context.go | 2 ++ 5 files changed, 55 insertions(+), 51 deletions(-) diff --git a/common/net/destination.go b/common/net/destination.go index c495a5b0..7709b5c9 100644 --- a/common/net/destination.go +++ b/common/net/destination.go @@ -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), } } diff --git a/common/net/ipnet.go b/common/net/ipnet.go index 590329e7..123ed334 100644 --- a/common/net/ipnet.go +++ b/common/net/ipnet.go @@ -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() { diff --git a/common/net/network.go b/common/net/network.go index 791b3d05..f232de0e 100644 --- a/common/net/network.go +++ b/common/net/network.go @@ -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) } diff --git a/common/net/port.go b/common/net/port.go index f3336e57..8988a45f 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -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), } } diff --git a/common/protocol/context.go b/common/protocol/context.go index c644e1db..6a2d2409 100644 --- a/common/protocol/context.go +++ b/common/protocol/context.go @@ -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 {