5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-09-20 06:02:34 +00:00

Use Close() for admin socket

This commit is contained in:
Neil Alexander 2018-07-07 20:04:11 +01:00
parent 2a931df07a
commit a5af69df8a
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 12 additions and 15 deletions

View File

@ -20,6 +20,7 @@ import (
type admin struct { type admin struct {
core *Core core *Core
listenaddr string listenaddr string
listener net.Listener
handlers []admin_handlerInfo handlers []admin_handlerInfo
} }
@ -228,39 +229,35 @@ func (a *admin) start() error {
} }
// cleans up when stopping // cleans up when stopping
func (a *admin) stop() error { func (a *admin) close() error {
if a.listenaddr[0:7] == "unix://" { return a.listener.Close()
if err := os.Remove(a.listenaddr[7:]); err != nil {
return err
}
}
return nil
} }
// listen is run by start and manages API connections. // listen is run by start and manages API connections.
func (a *admin) listen() { func (a *admin) listen() {
var l net.Listener
u, err := url.Parse(a.listenaddr) u, err := url.Parse(a.listenaddr)
if err == nil { if err == nil {
switch strings.ToLower(u.Scheme) { switch strings.ToLower(u.Scheme) {
case "unix": case "unix":
l, err = net.Listen("unix", a.listenaddr[7:]) a.listener, err = net.Listen("unix", a.listenaddr[7:])
case "tcp": case "tcp":
l, err = net.Listen("tcp", u.Host) a.listener, err = net.Listen("tcp", u.Host)
default: default:
err = errors.New("protocol not supported") err = errors.New("protocol not supported")
} }
} else { } else {
l, err = net.Listen("tcp", a.listenaddr) a.listener, err = net.Listen("tcp", a.listenaddr)
} }
if err != nil { if err != nil {
a.core.log.Printf("Admin socket failed to listen: %v", err) a.core.log.Printf("Admin socket failed to listen: %v", err)
os.Exit(1) os.Exit(1)
} }
a.core.log.Printf("%s admin socket listening on %s", strings.ToUpper(l.Addr().Network()), l.Addr().String()) a.core.log.Printf("%s admin socket listening on %s",
defer l.Close() strings.ToUpper(a.listener.Addr().Network()),
a.listener.Addr().String())
defer a.listener.Close()
for { for {
conn, err := l.Accept() conn, err := a.listener.Accept()
if err == nil { if err == nil {
a.handleRequest(conn) a.handleRequest(conn)
} }

View File

@ -135,7 +135,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
func (c *Core) Stop() { func (c *Core) Stop() {
c.log.Println("Stopping...") c.log.Println("Stopping...")
c.tun.close() c.tun.close()
c.admin.stop() c.admin.close()
} }
// Generates a new encryption keypair. The encryption keys are used to // Generates a new encryption keypair. The encryption keys are used to