-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
234 lines (212 loc) · 4.94 KB
/
config.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package xlat
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os/exec"
"strings"
"github.com/ghodss/yaml"
"github.com/songgao/water"
)
type XlatConfig struct {
// DeviceName string
device *water.Interface
Spec *XlatConfigSpec
Clat *ClatConfig
Plat *PlatConfig
}
type ClatConfig struct {
Src *net.IPNet
Dst *net.IPNet
}
type PlatConfig struct {
// SrcPool *net.IPNet
SrcIdx map[uint32]int
Src []net.IP
Dst *net.IPNet
}
type XlatConfigSpec struct {
DeviceName string `json:"device"`
MTU int `json:"mtu"`
PostCMD []string `json:"post_cmd"`
BufferSize int `json:"buffer_size"`
NATTimeout float64 `json:"nat_timeout"`
Clat *struct {
Enable bool `json:"enable"`
Src string `json:"src"`
Dst string `json:"dst"`
} `json:"clat"`
Plat *struct {
Enable bool `json:"enable"`
Src []string `json:"src"`
Dst string `json:"dst"`
} `json:"plat"`
Radvd *RadvdConfig `json:"ra"`
DNS *DNSConfig `json:"dns64"`
API *struct {
Enable bool `json:"enable"`
} `json:"api"`
DHCP6 *DHCP6Config `json:"dhcp6"`
}
type RadvdConfig struct {
Enable bool `json:"enable"`
Interface string `json:"interface"`
Prefixes []string `json:"prefixes"`
Rdnss string `json:"rdnss"`
}
type DNSConfig struct {
Enable bool `json:"enable"`
Forwarders []string `json:"forwarders"`
Prefix string `json:"prefix"`
}
type DHCP6Config struct {
Enable bool `json:"enable"`
Interface string `json:"interface"`
DNS []string `json:"dns"`
// modifiers []dhcpv6.Modifier
}
const (
ServiceClat = "clat"
ServicePlat = "plat"
ServiceRadvd = "radvd"
ServiceDns = "dns"
ServiceAPI = "api"
ServiceDHCP6 = "dhcp6"
)
// type ClatConfigSpec struct {
// Src string `json:"src"`
// Dst string `json:"dst"`
// }
// type PlatConfigSpec struct {
// }
var ConfigVar *XlatConfig
// func (c *XlatConfig) Device() *tuntap.Interface {
// if c.device != nil {
// return c.device
// }
// dev, err := tuntap.Open(c.Spec.DeviceName, tuntap.DevTun, false)
// if err != nil {
// log.Printf("Failed to load device %s: %s", c.Spec.DeviceName, err.Error())
// return nil
// }
// c.device = dev
// return c.device
// }
func (p *PlatConfig) SrcContains(ip net.IP) bool {
key := binary.BigEndian.Uint32(ip)
if _, ok := p.SrcIdx[key]; ok {
return true
}
return false
}
func (c *XlatConfig) Device() *water.Interface {
return c.device
}
func (c *XlatConfig) Enabled(service string) bool {
switch service {
case ServiceClat:
if c.Spec.Clat != nil && c.Spec.Clat.Enable == true {
return true
}
return false
case ServicePlat:
if c.Spec.Plat != nil && c.Spec.Plat.Enable == true {
return true
}
return false
case ServiceRadvd:
if c.Spec.Radvd != nil && c.Spec.Radvd.Enable == true {
return true
}
return false
case ServiceDns:
if c.Spec.DNS != nil && c.Spec.DNS.Enable == true {
return true
}
return false
case ServiceAPI:
if c.Spec.API != nil && c.Spec.API.Enable == true {
return true
}
case ServiceDHCP6:
if c.Spec.DHCP6 != nil && c.Spec.DHCP6.Enable == true {
return true
}
}
return false
}
func (c *XlatConfig) InitDevice() error {
deviceConfig := water.Config{
DeviceType: water.TUN,
}
deviceConfig.Name = c.Spec.DeviceName
deviceConfig.MultiQueue = true
dev, err := water.New(deviceConfig)
if err != nil {
// log.Printf("Failed to load device %s: %s", c.Spec.DeviceName, err.Error())
return err
}
c.device = dev
if c.Spec.PostCMD != nil {
for _, cmdStr := range c.Spec.PostCMD {
scmd := strings.Split(cmdStr, " ")
cmd := exec.Command(scmd[0], scmd[1:]...)
err := cmd.Run()
if err != nil {
log.Printf("Failed to execute '%s': %s", cmdStr, err.Error())
}
log.Printf("Executed '%s'", cmdStr)
}
}
return nil
}
func LoadConfig(configPath string) (*XlatConfig, error) {
data, err := ioutil.ReadFile(configPath)
if err != nil {
log.Printf("Failed to find config: %s", err.Error())
return nil, err
}
configSpec := &XlatConfigSpec{}
jdata, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, err
}
err = json.Unmarshal(jdata, configSpec)
if err != nil {
log.Printf("Failed to load config: %s", err.Error())
return nil, err
}
// log.Printf("Config Data: %+v", configSpec)
if configSpec.DeviceName == "" {
return nil, fmt.Errorf("no devicename found")
}
if configSpec.MTU == 0 {
configSpec.MTU = 1500
}
if configSpec.BufferSize == 0 {
configSpec.BufferSize = 1000
}
if configSpec.NATTimeout == 0 {
configSpec.NATTimeout = 30
}
ConfigVar = &XlatConfig{
Spec: configSpec,
}
log.Printf("Initializing device %s", ConfigVar.Spec.DeviceName)
err = ConfigVar.InitDevice()
if err != nil {
log.Printf("Failed to load device %s: %s", ConfigVar.Spec.DeviceName, err.Error())
return nil, err
}
return ConfigVar, nil
}
func Addr(ipNet *net.IPNet) string {
return ipNet.IP.String()
}
func Mask(ipNet *net.IPNet) int {
pos, _ := ipNet.Mask.Size()
return pos
}