2020-03-21 23:46:49 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
|
|
|
|
host, port, err := net.SplitHostPort(rawAddress)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("addrToMetadata failed: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
2021-03-24 01:00:21 +08:00
|
|
|
if ip == nil {
|
2020-03-21 23:46:49 +08:00
|
|
|
addr = &C.Metadata{
|
2022-07-05 20:26:43 +08:00
|
|
|
Host: host,
|
|
|
|
DstIP: nil,
|
|
|
|
DstPort: port,
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
return
|
2021-03-24 01:00:21 +08:00
|
|
|
} else if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
addr = &C.Metadata{
|
2022-07-05 20:26:43 +08:00
|
|
|
Host: "",
|
|
|
|
DstIP: ip4,
|
|
|
|
DstPort: port,
|
2021-03-24 01:00:21 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = &C.Metadata{
|
2022-07-05 20:26:43 +08:00
|
|
|
Host: "",
|
|
|
|
DstIP: ip,
|
|
|
|
DstPort: port,
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
2021-03-24 01:00:21 +08:00
|
|
|
return
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func tcpKeepAlive(c net.Conn) {
|
|
|
|
if tcp, ok := c.(*net.TCPConn); ok {
|
|
|
|
tcp.SetKeepAlive(true)
|
|
|
|
tcp.SetKeepAlivePeriod(30 * time.Second)
|
|
|
|
}
|
|
|
|
}
|