Compare commits

...

6 Commits

Author SHA1 Message Date
Mohammed Al Sahaf
5a0ee39377
Merge abdeadfdf4be22fd641bc5f0828befd59c42a64a into fd4de7e0ae4debd54e2a9a4aba10947cd4c3b178 2025-02-20 12:03:02 -05:00
Mohammed Al Sahaf
abdeadfdf4
Merge branch 'master' into interface-network-type 2024-10-12 14:58:38 +03:00
Mohammed Al Sahaf
9ebd7fa221
Merge branch 'master' into interface-network-type 2024-08-22 20:34:25 +03:00
Mohammed Al Sahaf
32d6798822
Merge branch 'master' into interface-network-type 2024-08-13 09:36:07 +03:00
Mohammed Al Sahaf
9c4654a638
Merge branch 'master' into interface-network-type 2024-08-12 10:04:06 +03:00
Mohammed Al Sahaf
490fd1d63d
core: add iface as network type
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
2024-08-10 22:46:46 +03:00
2 changed files with 55 additions and 0 deletions

51
interface.go Normal file
View File

@ -0,0 +1,51 @@
package caddy
import (
"context"
"fmt"
"net"
"strings"
)
func init() {
RegisterNetwork("iface", getInterfaceListener)
RegisterNetwork("iface+tcp", getInterfaceListener)
RegisterNetwork("iface+udp", getInterfaceListener)
}
func getInterfaceListener(ctx context.Context, network, addr string, config net.ListenConfig) (any, error) {
// assuming addr = "interface+family:port"
// if family is missing, then assume tcp
family := "tcp"
parts := strings.Split(network, "+")
if len(parts) == 2 {
family = parts[1]
}
host, port, _ := net.SplitHostPort(addr)
iface, err := net.InterfaceByName(host)
if err != nil {
return nil, fmt.Errorf("interface %s not found", addr)
}
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("error on obtaining interface %s addresses: %s", iface.Name, err)
}
if len(addrs) == 0 {
return nil, fmt.Errorf("interface %s has no addresses", iface.Name)
}
for _, addr := range addrs {
if face, ok := addr.(*net.IPNet); ok {
if ip4 := face.IP.To4(); ip4 != nil {
switch family {
case "tcp":
return net.Listen(family, net.JoinHostPort(ip4.String(), port))
case "udp":
return net.ListenPacket(family, net.JoinHostPort(ip4.String(), port))
default:
return net.Listen(family, net.JoinHostPort(ip4.String(), port))
}
}
}
}
return nil, fmt.Errorf("interface %s has no IPv4 addresses", iface.Name)
}

View File

@ -42,6 +42,10 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddytls"
)
func init() {
RegisterNetworkHTTP3("iface", "iface+udp")
}
// Server describes an HTTP server.
type Server struct {
// Socket addresses to which to bind listeners. Accepts