mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
add Listen to api and listenURL to tcp
This commit is contained in:
parent
fd5cda6329
commit
5f2bcaa71f
5 changed files with 58 additions and 14 deletions
|
@ -103,17 +103,21 @@ func (c *Core) GetSessions() []Session {
|
|||
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
|
||||
// "Listen" configuration item, e.g.
|
||||
// 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)
|
||||
}
|
||||
|
||||
// ListenTLS starts a new TLS listener. The input URI should match that of the
|
||||
// "Listen" configuration item, e.g.
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
@ -226,10 +230,6 @@ func (c *Core) RemovePeer(addr string, sintf string) error {
|
|||
// 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
|
||||
// peer will not be called again automatically.
|
||||
func (c *Core) CallPeer(addr string, sintf string) error {
|
||||
u, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
func (c *Core) CallPeer(u *url.URL, sintf string) error {
|
||||
return c.links.call(u, sintf)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/hex"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
iw "github.com/Arceliar/ironwood/encrypted"
|
||||
|
@ -69,7 +70,11 @@ func (c *Core) _addPeerLoop() {
|
|||
// Add peers from the Peers section
|
||||
for _, peer := range current.Peers {
|
||||
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)
|
||||
}
|
||||
}(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 _, peer := range intfpeers {
|
||||
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)
|
||||
}
|
||||
}(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 {
|
||||
// return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err)
|
||||
//}
|
||||
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
|
||||
tcpOpts := tcpOptions{}
|
||||
if pubkeys, ok := u.Query()["ed25519"]; ok && len(pubkeys) > 0 {
|
||||
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.Password, _ = u.User.Password()
|
||||
}
|
||||
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
|
||||
l.tcp.call(pathtokens[0], tcpOpts, sintf)
|
||||
case "tls":
|
||||
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) {
|
||||
var err error
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue