-
Notifications
You must be signed in to change notification settings - Fork 25
/
routing_linux.go
132 lines (113 loc) · 4.06 KB
/
routing_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2023 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"net"
"github.com/vishvananda/netlink"
)
var ipRule *netlink.Rule = nil
func startRouting(proxyIP string, config *RoutingConfig) error {
if err := setupRoutingTable(config.RoutingTableID, config.TunDeviceName, config.TunGatewayCIDR, config.TunDeviceIP); err != nil {
return err
}
return setupIpRule(proxyIP+"/32", config.RoutingTableID, config.RoutingTablePriority)
}
func stopRouting(routingTable int) {
if err := cleanUpRoutingTable(routingTable); err != nil {
logging.Err.Printf("failed to clean up routing table '%v': %v\n", routingTable, err)
}
if err := cleanUpRule(); err != nil {
logging.Err.Printf("failed to clean up IP rule: %v\n", err)
}
}
func setupRoutingTable(routingTable int, tunName, gwSubnet string, tunIP string) error {
tun, err := netlink.LinkByName(tunName)
if err != nil {
return fmt.Errorf("failed to find tun device '%s': %w", tunName, err)
}
dst, err := netlink.ParseIPNet(gwSubnet)
if err != nil {
return fmt.Errorf("failed to parse gateway '%s': %w", gwSubnet, err)
}
r := netlink.Route{
LinkIndex: tun.Attrs().Index,
Table: routingTable,
Dst: dst,
Src: net.ParseIP(tunIP),
Scope: netlink.SCOPE_LINK,
}
if err = netlink.RouteAdd(&r); err != nil {
return fmt.Errorf("failed to add routing entry '%v' -> '%v': %w", r.Src, r.Dst, err)
}
logging.Info.Printf("routing traffic from %v to %v through nic %v\n", r.Src, r.Dst, r.LinkIndex)
r = netlink.Route{
LinkIndex: tun.Attrs().Index,
Table: routingTable,
Gw: dst.IP,
}
if err := netlink.RouteAdd(&r); err != nil {
return fmt.Errorf("failed to add gateway routing entry '%v': %w", r.Gw, err)
}
logging.Info.Printf("routing traffic via gw %v through nic %v...\n", r.Gw, r.LinkIndex)
return nil
}
func cleanUpRoutingTable(routingTable int) error {
filter := netlink.Route{Table: routingTable}
routes, err := netlink.RouteListFiltered(netlink.FAMILY_V4, &filter, netlink.RT_FILTER_TABLE)
if err != nil {
return fmt.Errorf("failed to list entries in routing table '%v': %w", routingTable, err)
}
var rtDelErr error = nil
for _, route := range routes {
if err := netlink.RouteDel(&route); err != nil {
rtDelErr = errors.Join(rtDelErr, fmt.Errorf("failed to remove routing entry: %w", err))
}
}
if rtDelErr == nil {
logging.Info.Printf("routing table '%v' has been cleaned up\n", routingTable)
}
return rtDelErr
}
func setupIpRule(svrIp string, routingTable, routingPriority int) error {
dst, err := netlink.ParseIPNet(svrIp)
if err != nil {
return fmt.Errorf("failed to parse server IP CIDR '%s': %w", svrIp, err)
}
// todo: exclude server IP will cause issues when accessing services on the same server,
// use fwmask to protect the shadowsocks socket instead
ipRule = netlink.NewRule()
ipRule.Priority = routingPriority
ipRule.Family = netlink.FAMILY_V4
ipRule.Table = routingTable
ipRule.Dst = dst
ipRule.Invert = true
if err := netlink.RuleAdd(ipRule); err != nil {
return fmt.Errorf("failed to add IP rule (table %v, dst %v): %w", ipRule.Table, ipRule.Dst, err)
}
logging.Info.Printf("ip rule 'from all not to %v via table %v' created\n", ipRule.Dst, ipRule.Table)
return nil
}
func cleanUpRule() error {
if ipRule == nil {
return nil
}
if err := netlink.RuleDel(ipRule); err != nil {
return fmt.Errorf("failed to delete IP rule of routing table '%v': %w", ipRule.Table, err)
}
logging.Info.Printf("ip rule of routing table '%v' deleted\n", ipRule.Table)
ipRule = nil
return nil
}