Skip to content

Commit

Permalink
优化结构
Browse files Browse the repository at this point in the history
  • Loading branch information
ppma committed Dec 26, 2020
1 parent 0e1f35a commit 417d6e2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[nat-type]()[RFC 3489][1]
的go实现,从 [NatTypeDetector](https://github.com/cdnbye/NatTypeDetector) 移植而来。
代码写得烂,思想跟不上,就这样了,补上一句一只企鹅狗司马
代码写得烂,思想跟不上,就这样了

## 使用

Expand Down
27 changes: 26 additions & 1 deletion attributetype.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package stun

type AttributeType int
type AttributeType uint

const (
Undefined AttributeType = iota
Expand All @@ -19,3 +19,28 @@ const (
XorOnly
ServerName
)

var attributeTypeNames = []string{
"Undefined",
"MappedAddress",
"ResponseAddress",
"ChangeRequest",
"SourceAddress",
"ChangedAddress",
"Username",
"Password",
"MessageIntegrity",
"ErrorCode",
"UnknownAttribute",
"ReflectedFrom",
"XorMappedAddress",
"XorOnly",
"ServerName",
}

func (t AttributeType) String() string {
if t <= ServerName {
return attributeTypeNames[t]
}
return ""
}
2 changes: 0 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ func (message *Message) Parse(data []byte) error {
offset += 2
switch attributeType {
case MappedAddress:
// System.out.println("type == AttributeType.MappedAddress");
message.mappedAddress = parseIPAddr(data, offset)
// System.out.println("mappedAddress " + mappedAddress.getAddress() + " " + mappedAddress.getPort());
offset += 8
case ResponseAddress:
// RESPONSE-ADDRESS
Expand Down
69 changes: 28 additions & 41 deletions nattype.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,45 @@
package stun

type NatType struct {
int
string
}
type NatType uint

func (natType NatType) String() string {
return natType.string
}

var (
const (
/// <summary>
/// UDP is always blocked.
/// </summary>
UdpBlocked = NatType{
int: 0,
string: "UdpBlocked",
}
UdpBlocked NatType = iota

/// <summary>
/// No NAT, public IP, no firewall.
/// </summary>
OpenInternet = NatType{
int: 1,
string: "OpenInternet",
}
OpenInternet

/// <summary>
/// No NAT, public IP, but symmetric UDP firewall.
/// </summary>
SymmetricUdpFirewall = NatType{
int: 2,
string: "SymmetricUdpFirewall",
}
SymmetricUdpFirewall

/// <summary>
/// A full cone NAT is one where all requests from the same internal IP address and port are
/// mapped to the same external IP address and port. Furthermore, any external host can send
/// a packet to the internal host, by sending a packet to the mapped external address.
/// </summary>
FullCone = NatType{
int: 3,
string: "FullCone",
}
FullCone

/// <summary>
/// A restricted cone NAT is one where all requests from the same internal IP address and
/// port are mapped to the same external IP address and port. Unlike a full cone NAT, an external
/// host (with IP address X) can send a packet to the internal host only if the internal host
/// had previously sent a packet to IP address X.
/// </summary>
RestrictedCone = NatType{
int: 4,
string: "RestrictedCone",
}
RestrictedCone

/// <summary>
/// A port restricted cone NAT is like a restricted cone NAT, but the restriction
/// includes port numbers. Specifically, an external host can send a packet, with source IP
/// address X and source port P, to the internal host only if the internal host had previously
/// sent a packet to IP address X and port P.
/// </summary>
PortRestrictedCone = NatType{
int: 5,
string: "PortRestrictedCone",
}
PortRestrictedCone

/// <summary>
/// A symmetric NAT is one where all requests from the same internal IP address and port,
Expand All @@ -73,13 +48,25 @@ var (
/// a different destination, a different mapping is used. Furthermore, only the external host that
/// receives a packet can send a UDP packet back to the internal host.
/// </summary>
Symmetric = NatType{
int: 6,
string: "Symmetric",
}
Symmetric

Unknown = NatType{
int: 7,
string: "Unknown",
}
Unknown
)

var natTypeNames = []string{
"UdpBlocked",
"OpenInternet",
"SymmetricUdpFirewall",
"FullCone",
"RestrictedCone",
"PortRestrictedCone",
"Symmetric",
"Unknown",
}

func (natType NatType) String() string {
if natType <= Unknown {
return natTypeNames[natType]
}
return ""
}

0 comments on commit 417d6e2

Please sign in to comment.