-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add behavior test mode. #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,3 +164,90 @@ func (c *Client) discover(conn net.PacketConn, addr *net.UDPAddr) (NATType, *Hos | |
} | ||
return NATSymmetric, mappedAddr, nil | ||
} | ||
|
||
func (c *Client) behaviorTest(conn net.PacketConn, addr *net.UDPAddr) (*NATBehavior, error) { | ||
natBehavior := &NATBehavior{} | ||
|
||
// Test1 ->(IP1,port1) | ||
// Perform test to check if it is under NAT. | ||
c.logger.Debugln("Do Test1") | ||
resp1, err := c.test(conn, addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// identical used to check if it is open Internet or not. | ||
if resp1.identical { | ||
return nil, errors.New("Not behind a NAT.") | ||
} | ||
// use otherAddr or changedAddr | ||
otherAddr := resp1.otherAddr | ||
if otherAddr == nil { | ||
if resp1.changedAddr != nil { | ||
otherAddr = resp1.changedAddr | ||
} else { | ||
return nil, errors.New("Server error: no other address and changed address.") | ||
} | ||
} | ||
|
||
// Test2 ->(IP2,port1) | ||
// Perform test to see if mapping to the same IP and port when | ||
// send to another IP. | ||
c.logger.Debugln("Do Test2") | ||
tmpAddr := &net.UDPAddr{IP: net.ParseIP(otherAddr.IP()), Port: addr.Port} | ||
resp2, err := c.test(conn, tmpAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp2.mappedAddr.IP() == resp1.mappedAddr.IP() && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got the following error here
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be nat blocked , but will be detected in the first step.Maybe the network is not stable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is it better to use mapped instead of local? |
||
resp2.mappedAddr.Port() == resp1.mappedAddr.Port() { | ||
natBehavior.MappingType = BehaviorTypeEndpoint | ||
} | ||
|
||
// Test3 ->(IP2,port2) | ||
// Perform test to see if mapping to the same IP and port when | ||
// send to another port. | ||
if natBehavior.MappingType == BehaviorTypeUnknown { | ||
c.logger.Debugln("Do Test3") | ||
tmpAddr.Port = int(otherAddr.Port()) | ||
resp3, err := c.test(conn, tmpAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp3.mappedAddr.IP() == resp2.mappedAddr.IP() && | ||
resp3.mappedAddr.Port() == resp2.mappedAddr.Port() { | ||
natBehavior.MappingType = BehaviorTypeAddr | ||
} else { | ||
natBehavior.MappingType = BehaviorTypeAddrAndPort | ||
} | ||
} | ||
|
||
// Test4 ->(IP1,port1) (IP2,port2)-> | ||
// Perform test to see if the client can receive packet sent from | ||
// another IP and port. | ||
c.logger.Debugln("Do Test4") | ||
resp4, err := c.testChangeBoth(conn, addr) | ||
if err != nil { | ||
return natBehavior, err | ||
} | ||
if resp4 != nil { | ||
natBehavior.FilteringType = BehaviorTypeEndpoint | ||
} | ||
|
||
// Test5 ->(IP1,port1) (IP1,port2)-> | ||
// Perform test to see if the client can receive packet sent from | ||
// another port. | ||
if natBehavior.FilteringType == BehaviorTypeUnknown { | ||
c.logger.Debugln("Do Test5") | ||
resp5, err := c.testChangePort(conn, addr) | ||
if err != nil { | ||
return natBehavior, err | ||
} | ||
if resp5 != nil { | ||
natBehavior.FilteringType = BehaviorTypeAddr | ||
} else { | ||
natBehavior.FilteringType = BehaviorTypeAddrAndPort | ||
} | ||
} | ||
|
||
return natBehavior, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats up with these cryptic names? Are you paying per character?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for response. It's just some ridiculous thoughts: don't want it too long. Is it better use MType and FType?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I move these to a test branch? There may need much more adjust.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is already called NATBehaviour, why can't you just call them FilteringType and BehaviourType?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good advice.