mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-11-27 12:05:23 +00:00
add Listen to api and listenURL to tcp
This commit is contained in:
parent
fd5cda6329
commit
5f2bcaa71f
@ -103,17 +103,21 @@ func (c *Core) GetSessions() []Session {
|
|||||||
return sessions
|
return sessions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Core) Listen(u *url.URL, sintf string) (*TcpListener, error) {
|
||||||
|
return c.links.tcp.listenURL(u, sintf)
|
||||||
|
}
|
||||||
|
|
||||||
// ListenTCP starts a new TCP listener. The input URI should match that of the
|
// ListenTCP starts a new TCP listener. The input URI should match that of the
|
||||||
// "Listen" configuration item, e.g.
|
// "Listen" configuration item, e.g.
|
||||||
// tcp://a.b.c.d:e
|
// tcp://a.b.c.d:e
|
||||||
func (c *Core) ListenTCP(uri string, metric uint8) (*TcpListener, error) {
|
func (c *Core) xListenTCP(uri string, metric uint8) (*TcpListener, error) {
|
||||||
return c.links.tcp.listen(uri, nil, metric)
|
return c.links.tcp.listen(uri, nil, metric)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenTLS starts a new TLS listener. The input URI should match that of the
|
// ListenTLS starts a new TLS listener. The input URI should match that of the
|
||||||
// "Listen" configuration item, e.g.
|
// "Listen" configuration item, e.g.
|
||||||
// tls://a.b.c.d:e
|
// tls://a.b.c.d:e
|
||||||
func (c *Core) ListenTLS(uri string, metric uint8) (*TcpListener, error) {
|
func (c *Core) xListenTLS(uri string, metric uint8) (*TcpListener, error) {
|
||||||
return c.links.tcp.listen(uri, c.links.tcp.tls.forListener, metric)
|
return c.links.tcp.listen(uri, c.links.tcp.tls.forListener, metric)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,10 +230,6 @@ func (c *Core) RemovePeer(addr string, sintf string) error {
|
|||||||
// socks://a.b.c.d:e/f.g.h.i:j
|
// socks://a.b.c.d:e/f.g.h.i:j
|
||||||
// This does not add the peer to the peer list, so if the connection drops, the
|
// This does not add the peer to the peer list, so if the connection drops, the
|
||||||
// peer will not be called again automatically.
|
// peer will not be called again automatically.
|
||||||
func (c *Core) CallPeer(addr string, sintf string) error {
|
func (c *Core) CallPeer(u *url.URL, sintf string) error {
|
||||||
u, err := url.Parse(addr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return c.links.call(u, sintf)
|
return c.links.call(u, sintf)
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
iw "github.com/Arceliar/ironwood/encrypted"
|
iw "github.com/Arceliar/ironwood/encrypted"
|
||||||
@ -69,7 +70,11 @@ func (c *Core) _addPeerLoop() {
|
|||||||
// Add peers from the Peers section
|
// Add peers from the Peers section
|
||||||
for _, peer := range current.Peers {
|
for _, peer := range current.Peers {
|
||||||
go func(peer string, intf string) {
|
go func(peer string, intf string) {
|
||||||
if err := c.CallPeer(peer, intf); err != nil {
|
u, err := url.Parse(peer)
|
||||||
|
if err != nil {
|
||||||
|
c.log.Errorln("Failed to parse peer url:", peer, err)
|
||||||
|
}
|
||||||
|
if err := c.CallPeer(u, intf); err != nil {
|
||||||
c.log.Errorln("Failed to add peer:", err)
|
c.log.Errorln("Failed to add peer:", err)
|
||||||
}
|
}
|
||||||
}(peer, "") // TODO: this should be acted and not in a goroutine?
|
}(peer, "") // TODO: this should be acted and not in a goroutine?
|
||||||
@ -79,7 +84,11 @@ func (c *Core) _addPeerLoop() {
|
|||||||
for intf, intfpeers := range current.InterfacePeers {
|
for intf, intfpeers := range current.InterfacePeers {
|
||||||
for _, peer := range intfpeers {
|
for _, peer := range intfpeers {
|
||||||
go func(peer string, intf string) {
|
go func(peer string, intf string) {
|
||||||
if err := c.CallPeer(peer, intf); err != nil {
|
u, err := url.Parse(peer)
|
||||||
|
if err != nil {
|
||||||
|
c.log.Errorln("Failed to parse peer url:", peer, err)
|
||||||
|
}
|
||||||
|
if err := c.CallPeer(u, intf); err != nil {
|
||||||
c.log.Errorln("Failed to add peer:", err)
|
c.log.Errorln("Failed to add peer:", err)
|
||||||
}
|
}
|
||||||
}(peer, intf) // TODO: this should be acted and not in a goroutine?
|
}(peer, intf) // TODO: this should be acted and not in a goroutine?
|
||||||
|
@ -80,7 +80,6 @@ func (l *links) call(u *url.URL, sintf string) error {
|
|||||||
//if err != nil {
|
//if err != nil {
|
||||||
// return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err)
|
// return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err)
|
||||||
//}
|
//}
|
||||||
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
|
|
||||||
tcpOpts := tcpOptions{}
|
tcpOpts := tcpOptions{}
|
||||||
if pubkeys, ok := u.Query()["ed25519"]; ok && len(pubkeys) > 0 {
|
if pubkeys, ok := u.Query()["ed25519"]; ok && len(pubkeys) > 0 {
|
||||||
tcpOpts.pinnedEd25519Keys = make(map[keyArray]struct{})
|
tcpOpts.pinnedEd25519Keys = make(map[keyArray]struct{})
|
||||||
@ -106,6 +105,7 @@ func (l *links) call(u *url.URL, sintf string) error {
|
|||||||
tcpOpts.socksProxyAuth.User = u.User.Username()
|
tcpOpts.socksProxyAuth.User = u.User.Username()
|
||||||
tcpOpts.socksProxyAuth.Password, _ = u.User.Password()
|
tcpOpts.socksProxyAuth.Password, _ = u.User.Password()
|
||||||
}
|
}
|
||||||
|
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
|
||||||
l.tcp.call(pathtokens[0], tcpOpts, sintf)
|
l.tcp.call(pathtokens[0], tcpOpts, sintf)
|
||||||
case "tls":
|
case "tls":
|
||||||
tcpOpts.upgrade = l.tcp.tls.forDialer
|
tcpOpts.upgrade = l.tcp.tls.forDialer
|
||||||
|
@ -185,6 +185,32 @@ func (t *tcp) reconfigure() {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tcp) listenURL(u *url.URL, sintf string) (*TcpListener, error) {
|
||||||
|
var metric uint8
|
||||||
|
if ms := u.Query()["metric"]; len(ms) == 1 {
|
||||||
|
m64, _ := strconv.ParseUint(ms[0], 10, 8)
|
||||||
|
metric = uint8(m64)
|
||||||
|
}
|
||||||
|
var listener *TcpListener
|
||||||
|
var err error
|
||||||
|
hostport := u.Host // Used for tcp and tls
|
||||||
|
if len(sintf) != 0 {
|
||||||
|
host, port, err := net.SplitHostPort(hostport)
|
||||||
|
if err == nil {
|
||||||
|
hostport = fmt.Sprintf("[%s%%%s]:%s", host, sintf, port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch u.Scheme {
|
||||||
|
case "tcp":
|
||||||
|
listener, err = t.listen(hostport, nil, metric)
|
||||||
|
case "tls":
|
||||||
|
listener, err = t.listen(hostport, t.tls.forListener, metric)
|
||||||
|
default:
|
||||||
|
t.links.core.log.Errorln("Failed to add listener: listener", u.String(), "is not correctly formatted, ignoring")
|
||||||
|
}
|
||||||
|
return listener, err
|
||||||
|
}
|
||||||
|
|
||||||
func (t *tcp) listen(listenaddr string, upgrade *TcpUpgrade, metric uint8) (*TcpListener, error) {
|
func (t *tcp) listen(listenaddr string, upgrade *TcpUpgrade, metric uint8) (*TcpListener, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -273,9 +274,13 @@ func (m *Multicast) _announce() {
|
|||||||
var info *listenerInfo
|
var info *listenerInfo
|
||||||
if nfo, ok := m.listeners[iface.Name]; !ok || nfo.listener.Listener == nil {
|
if nfo, ok := m.listeners[iface.Name]; !ok || nfo.listener.Listener == nil {
|
||||||
// No listener was found - let's create one
|
// No listener was found - let's create one
|
||||||
listenaddr := fmt.Sprintf("[%s%%%s]:%d", addrIP, iface.Name, m.listenPort)
|
var metric uint8 // TODO parse this from the config
|
||||||
var metric uint8 // TODO parse this from config
|
urlString := fmt.Sprintf("tcp://[%s]:%d/?metric=%d", addrIP, m.listenPort, metric)
|
||||||
if li, err := m.core.ListenTCP(listenaddr, metric); err == nil {
|
u, err := url.Parse(urlString)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if li, err := m.core.Listen(u, iface.Name); err == nil {
|
||||||
m.log.Debugln("Started multicasting on", iface.Name)
|
m.log.Debugln("Started multicasting on", iface.Name)
|
||||||
// Store the listener so that we can stop it later if needed
|
// Store the listener so that we can stop it later if needed
|
||||||
info = &listenerInfo{listener: li, time: time.Now()}
|
info = &listenerInfo{listener: li, time: time.Now()}
|
||||||
@ -353,7 +358,11 @@ func (m *Multicast) listen() {
|
|||||||
})
|
})
|
||||||
if _, ok := interfaces[from.Zone]; ok {
|
if _, ok := interfaces[from.Zone]; ok {
|
||||||
addr.Zone = ""
|
addr.Zone = ""
|
||||||
if err := m.core.CallPeer("tcp://"+addr.String(), from.Zone); err != nil {
|
u, err := url.Parse("tcp://" + addr.String())
|
||||||
|
if err != nil {
|
||||||
|
m.log.Debugln("Call from multicast failed, parse error:", addr.String(), err)
|
||||||
|
}
|
||||||
|
if err := m.core.CallPeer(u, from.Zone); err != nil {
|
||||||
m.log.Debugln("Call from multicast failed:", err)
|
m.log.Debugln("Call from multicast failed:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user