mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-08 01:24:57 +08:00
add cidr hook in unix
This commit is contained in:
@@ -18,12 +18,14 @@ type NIC struct {
|
||||
ifce *water.Interface
|
||||
ip string
|
||||
subnet string
|
||||
cidrs []string
|
||||
hasstart bool
|
||||
}
|
||||
|
||||
// NewNIC 新建 TUN 网络接口卡
|
||||
// 网卡地址为 ip, 所属子网为 subnet
|
||||
func NewNIC(ip, subnet string) (n *NIC) {
|
||||
// 所有路由为 cidrs
|
||||
func NewNIC(ip, subnet string, cidrs ...string) (n *NIC) {
|
||||
ifce, err := water.New(water.Config{DeviceType: water.TUN})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -31,6 +33,7 @@ func NewNIC(ip, subnet string) (n *NIC) {
|
||||
n = &NIC{
|
||||
ifce: ifce,
|
||||
ip: ip,
|
||||
cidrs: cidrs,
|
||||
subnet: subnet,
|
||||
}
|
||||
n.prepare()
|
||||
|
||||
@@ -6,6 +6,9 @@ package lower
|
||||
func (n *NIC) prepare() {
|
||||
execute("ifconfig", n.ifce.Name(), "inet", n.ip, n.ip, "up")
|
||||
execute("route", "add", n.subnet, "-interface", n.ifce.Name())
|
||||
for _, c := range n.cidrs {
|
||||
execute("route", "add", c, "-interface", n.ifce.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NIC) Up() {
|
||||
|
||||
@@ -8,6 +8,9 @@ func (n *NIC) prepare() {
|
||||
execute("/sbin/ip", "addr", "add", n.ip, "dev", n.ifce.Name())
|
||||
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "up")
|
||||
execute("/sbin/ip", "route", "add", n.subnet, "dev", n.ifce.Name())
|
||||
for _, c := range n.cidrs {
|
||||
execute("/sbin/ip", "route", "add", c, "dev", n.ifce.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NIC) Up() {
|
||||
|
||||
Reference in New Issue
Block a user