mirror of
https://github.com/xmdhs/natupnp.git
synced 2025-10-05 23:16:51 +08:00
fix
This commit is contained in:
15
upnp/upnp.go
15
upnp/upnp.go
@@ -2,7 +2,6 @@ package upnp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/huin/goupnp/dcps/ocf/internetgateway2"
|
"github.com/huin/goupnp/dcps/ocf/internetgateway2"
|
||||||
@@ -60,7 +59,6 @@ type routerClient interface {
|
|||||||
|
|
||||||
func pickRouterClient(ctx context.Context) ([]routerClient, error) {
|
func pickRouterClient(ctx context.Context) ([]routerClient, error) {
|
||||||
tasks, _ := errgroup.WithContext(ctx)
|
tasks, _ := errgroup.WithContext(ctx)
|
||||||
// Request each type of client in parallel, and return what is found.
|
|
||||||
var ip1Clients []*internetgateway2.WANIPConnection1
|
var ip1Clients []*internetgateway2.WANIPConnection1
|
||||||
tasks.Go(func() error {
|
tasks.Go(func() error {
|
||||||
var err error
|
var err error
|
||||||
@@ -84,23 +82,18 @@ func pickRouterClient(ctx context.Context) ([]routerClient, error) {
|
|||||||
return nil, fmt.Errorf("pickRouterClient: %w", err)
|
return nil, fmt.Errorf("pickRouterClient: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trivial handling for where we find exactly one device to talk to, you
|
|
||||||
// might want to provide more flexible handling than this if multiple
|
|
||||||
// devices are found.
|
|
||||||
switch {
|
switch {
|
||||||
case len(ip2Clients) == 1:
|
case len(ip2Clients) > 1:
|
||||||
return any2slice[routerClient](ip2Clients), nil
|
return any2slice[routerClient](ip2Clients), nil
|
||||||
case len(ip1Clients) == 1:
|
case len(ip1Clients) > 1:
|
||||||
return any2slice[routerClient](ip1Clients), nil
|
return any2slice[routerClient](ip1Clients), nil
|
||||||
case len(ppp1Clients) == 1:
|
case len(ppp1Clients) > 1:
|
||||||
return any2slice[routerClient](ppp1Clients), nil
|
return any2slice[routerClient](ppp1Clients), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("pickRouterClient: %w", ErrNoService)
|
return nil, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrNoService = errors.New("multiple or no services found")
|
|
||||||
|
|
||||||
func any2slice[T, E any](list []E) []T {
|
func any2slice[T, E any](list []E) []T {
|
||||||
nl := make([]T, 0, len(list))
|
nl := make([]T, 0, len(list))
|
||||||
for _, v := range list {
|
for _, v := range list {
|
||||||
|
Reference in New Issue
Block a user