|
|
|
@ -26,31 +26,12 @@ import (
|
|
|
|
|
|
|
|
|
|
var responseTimeout = 3 * time.Second
|
|
|
|
|
|
|
|
|
|
type Address struct {
|
|
|
|
|
IP string
|
|
|
|
|
Port int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
|
Body []byte
|
|
|
|
|
Addr string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Discover(serverAddress string, stunServers []string, key []byte) ([]string, error) {
|
|
|
|
|
// parse address to net.Address
|
|
|
|
|
stunAddresses := make([]net.Addr, 0, len(stunServers))
|
|
|
|
|
for _, stunServer := range stunServers {
|
|
|
|
|
addr, err := net.ResolveUDPAddr("udp4", stunServer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stunAddresses = append(stunAddresses, addr)
|
|
|
|
|
}
|
|
|
|
|
serverAddr, err := net.ResolveUDPAddr("udp4", serverAddress)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a discoverConn and get response from messageChan
|
|
|
|
|
discoverConn, err := listen()
|
|
|
|
|
if err != nil {
|
|
|
|
@ -61,68 +42,90 @@ func Discover(serverAddress string, stunServers []string, key []byte) ([]string,
|
|
|
|
|
go discoverConn.readLoop()
|
|
|
|
|
|
|
|
|
|
addresses := make([]string, 0, len(stunServers)+1)
|
|
|
|
|
if serverAddress != "" {
|
|
|
|
|
// get external address from frp server
|
|
|
|
|
externalAddr, err := discoverFromServer(discoverConn, serverAddr, key)
|
|
|
|
|
externalAddr, err := discoverConn.discoverFromServer(serverAddress, key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
addresses = append(addresses, externalAddr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, stunAddr := range stunAddresses {
|
|
|
|
|
for _, addr := range stunServers {
|
|
|
|
|
// get external address from stun server
|
|
|
|
|
externalAddr, err = discoverFromStunServer(discoverConn, stunAddr)
|
|
|
|
|
externalAddrs, err := discoverConn.discoverFromStunServer(addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
addresses = append(addresses, externalAddr)
|
|
|
|
|
addresses = append(addresses, externalAddrs...)
|
|
|
|
|
}
|
|
|
|
|
return addresses, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func discoverFromServer(c *discoverConn, addr net.Addr, key []byte) (string, error) {
|
|
|
|
|
m := &msg.NatHoleBinding{
|
|
|
|
|
TransactionID: NewTransactionID(),
|
|
|
|
|
type stunResponse struct {
|
|
|
|
|
externalAddr string
|
|
|
|
|
otherAddr string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf, err := EncodeMessage(m, key)
|
|
|
|
|
type discoverConn struct {
|
|
|
|
|
conn *net.UDPConn
|
|
|
|
|
|
|
|
|
|
localAddr net.Addr
|
|
|
|
|
messageChan chan *Message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func listen() (*discoverConn, error) {
|
|
|
|
|
conn, err := net.ListenUDP("udp4", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := c.conn.WriteTo(buf, addr); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
return &discoverConn{
|
|
|
|
|
conn: conn,
|
|
|
|
|
localAddr: conn.LocalAddr(),
|
|
|
|
|
messageChan: make(chan *Message, 10),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var respMsg msg.NatHoleBindingResp
|
|
|
|
|
select {
|
|
|
|
|
case rawMsg := <-c.messageChan:
|
|
|
|
|
if err := DecodeMessageInto(rawMsg.Body, key, &respMsg); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
func (c *discoverConn) Close() error {
|
|
|
|
|
if c.messageChan != nil {
|
|
|
|
|
close(c.messageChan)
|
|
|
|
|
c.messageChan = nil
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(responseTimeout):
|
|
|
|
|
return "", fmt.Errorf("wait response from frp server timeout")
|
|
|
|
|
return c.conn.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if respMsg.TransactionID == "" {
|
|
|
|
|
return "", fmt.Errorf("error format: no transaction id found")
|
|
|
|
|
func (c *discoverConn) readLoop() {
|
|
|
|
|
for {
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
n, addr, err := c.conn.ReadFromUDP(buf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
|
|
|
|
|
|
|
|
|
c.messageChan <- &Message{
|
|
|
|
|
Body: buf,
|
|
|
|
|
Addr: addr.String(),
|
|
|
|
|
}
|
|
|
|
|
if respMsg.Error != "" {
|
|
|
|
|
return "", fmt.Errorf("get externalAddr from frp server error: %s", respMsg.Error)
|
|
|
|
|
}
|
|
|
|
|
return respMsg.Address, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func discoverFromStunServer(c *discoverConn, addr net.Addr) (string, error) {
|
|
|
|
|
func (c *discoverConn) doSTUNRequest(addr string) (*stunResponse, error) {
|
|
|
|
|
serverAddr, err := net.ResolveUDPAddr("udp4", addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
request, err := stun.Build(stun.TransactionID, stun.BindingRequest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = request.NewTransactionID(); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if _, err := c.conn.WriteTo(request.Raw, addr); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
if _, err := c.conn.WriteTo(request.Raw, serverAddr); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var m stun.Message
|
|
|
|
@ -130,63 +133,92 @@ func discoverFromStunServer(c *discoverConn, addr net.Addr) (string, error) {
|
|
|
|
|
case msg := <-c.messageChan:
|
|
|
|
|
m.Raw = msg.Body
|
|
|
|
|
if err := m.Decode(); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(responseTimeout):
|
|
|
|
|
return "", fmt.Errorf("wait response from stun server timeout")
|
|
|
|
|
return nil, fmt.Errorf("wait response from stun server timeout")
|
|
|
|
|
}
|
|
|
|
|
xorAddrGetter := &stun.XORMappedAddress{}
|
|
|
|
|
mappedAddrGetter := &stun.MappedAddress{}
|
|
|
|
|
changedAddrGetter := ChangedAddress{}
|
|
|
|
|
otherAddrGetter := &stun.OtherAddress{}
|
|
|
|
|
|
|
|
|
|
xorAddr := &stun.XORMappedAddress{}
|
|
|
|
|
mappedAddr := &stun.MappedAddress{}
|
|
|
|
|
if err := xorAddr.GetFrom(&m); err == nil {
|
|
|
|
|
return xorAddr.String(), nil
|
|
|
|
|
resp := &stunResponse{}
|
|
|
|
|
if err := mappedAddrGetter.GetFrom(&m); err == nil {
|
|
|
|
|
resp.externalAddr = mappedAddrGetter.String()
|
|
|
|
|
}
|
|
|
|
|
if err := mappedAddr.GetFrom(&m); err == nil {
|
|
|
|
|
return mappedAddr.String(), nil
|
|
|
|
|
if err := xorAddrGetter.GetFrom(&m); err == nil {
|
|
|
|
|
resp.externalAddr = xorAddrGetter.String()
|
|
|
|
|
}
|
|
|
|
|
return "", fmt.Errorf("no address found")
|
|
|
|
|
if err := changedAddrGetter.GetFrom(&m); err == nil {
|
|
|
|
|
resp.otherAddr = changedAddrGetter.String()
|
|
|
|
|
}
|
|
|
|
|
if err := otherAddrGetter.GetFrom(&m); err == nil {
|
|
|
|
|
resp.otherAddr = otherAddrGetter.String()
|
|
|
|
|
}
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type discoverConn struct {
|
|
|
|
|
conn *net.UDPConn
|
|
|
|
|
|
|
|
|
|
localAddr net.Addr
|
|
|
|
|
messageChan chan *Message
|
|
|
|
|
func (c *discoverConn) discoverFromServer(serverAddress string, key []byte) (string, error) {
|
|
|
|
|
addr, err := net.ResolveUDPAddr("udp4", serverAddress)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
m := &msg.NatHoleBinding{
|
|
|
|
|
TransactionID: NewTransactionID(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func listen() (*discoverConn, error) {
|
|
|
|
|
conn, err := net.ListenUDP("udp4", nil)
|
|
|
|
|
buf, err := EncodeMessage(m, key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &discoverConn{
|
|
|
|
|
conn: conn,
|
|
|
|
|
localAddr: conn.LocalAddr(),
|
|
|
|
|
messageChan: make(chan *Message, 10),
|
|
|
|
|
}, nil
|
|
|
|
|
if _, err := c.conn.WriteTo(buf, addr); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *discoverConn) Close() error {
|
|
|
|
|
if c.messageChan != nil {
|
|
|
|
|
close(c.messageChan)
|
|
|
|
|
c.messageChan = nil
|
|
|
|
|
var respMsg msg.NatHoleBindingResp
|
|
|
|
|
select {
|
|
|
|
|
case rawMsg := <-c.messageChan:
|
|
|
|
|
if err := DecodeMessageInto(rawMsg.Body, key, &respMsg); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return c.conn.Close()
|
|
|
|
|
case <-time.After(responseTimeout):
|
|
|
|
|
return "", fmt.Errorf("wait response from frp server timeout")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *discoverConn) readLoop() {
|
|
|
|
|
for {
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
n, addr, err := c.conn.ReadFromUDP(buf)
|
|
|
|
|
if respMsg.TransactionID == "" {
|
|
|
|
|
return "", fmt.Errorf("error format: no transaction id found")
|
|
|
|
|
}
|
|
|
|
|
if respMsg.Error != "" {
|
|
|
|
|
return "", fmt.Errorf("get externalAddr from frp server error: %s", respMsg.Error)
|
|
|
|
|
}
|
|
|
|
|
return respMsg.Address, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *discoverConn) discoverFromStunServer(addr string) ([]string, error) {
|
|
|
|
|
resp, err := c.doSTUNRequest(addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if resp.externalAddr == "" {
|
|
|
|
|
return nil, fmt.Errorf("no external address found")
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
|
|
|
|
|
|
|
|
|
c.messageChan <- &Message{
|
|
|
|
|
Body: buf,
|
|
|
|
|
Addr: addr.String(),
|
|
|
|
|
externalAddrs := make([]string, 0, 2)
|
|
|
|
|
externalAddrs = append(externalAddrs, resp.externalAddr)
|
|
|
|
|
|
|
|
|
|
if resp.otherAddr == "" {
|
|
|
|
|
return externalAddrs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find external address from changed address
|
|
|
|
|
resp, err = c.doSTUNRequest(resp.otherAddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if resp.externalAddr != "" {
|
|
|
|
|
externalAddrs = append(externalAddrs, resp.externalAddr)
|
|
|
|
|
}
|
|
|
|
|
return externalAddrs, nil
|
|
|
|
|
}
|
|
|
|
|