mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-03 02:08:45 +00:00
Routing config: Add processName (#5489)
This commit is contained in:
+41
-1
@@ -1,7 +1,13 @@
|
||||
// Package net is a drop-in replacement to Golang's net package, with some more functionalities.
|
||||
package net // import "github.com/xtls/xray-core/common/net"
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
)
|
||||
|
||||
// defines the maximum time an idle TCP session can survive in the tunnel, so
|
||||
// it should be consistent across HTTP versions and with other transports.
|
||||
@@ -12,3 +18,37 @@ const QuicgoH3KeepAlivePeriod = 10 * time.Second
|
||||
|
||||
// consistent with chrome
|
||||
const ChromeH2KeepAlivePeriod = 45 * time.Second
|
||||
|
||||
var ErrNotLocal = errors.New("the source address is not from local machine.")
|
||||
|
||||
type localIPCahceEntry struct {
|
||||
addrs []net.Addr
|
||||
lastUpdate time.Time
|
||||
}
|
||||
|
||||
var localIPCahce = atomic.Pointer[localIPCahceEntry]{}
|
||||
|
||||
func IsLocal(ip net.IP) (bool, error) {
|
||||
var addrs []net.Addr
|
||||
if entry := localIPCahce.Load(); entry == nil || time.Since(entry.lastUpdate) > time.Minute {
|
||||
var err error
|
||||
addrs, err = net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
localIPCahce.Store(&localIPCahceEntry{
|
||||
addrs: addrs,
|
||||
lastUpdate: time.Now(),
|
||||
})
|
||||
} else {
|
||||
addrs = entry.addrs
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
if ipnet, ok := addr.(*net.IPNet); ok {
|
||||
if ipnet.IP.Equal(ip) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user