mirror of
https://github.com/net-byte/vtun
synced 2024-03-14 10:50:03 +08:00
optimise code.
This commit is contained in:
parent
cf34ebb9a8
commit
f7665d1dba
10
app/app.go
10
app/app.go
@ -39,7 +39,6 @@ type App struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewApp(config *config.Config, version string) *App {
|
func NewApp(config *config.Config, version string) *App {
|
||||||
|
|
||||||
return &App{
|
return &App{
|
||||||
Config: config,
|
Config: config,
|
||||||
Version: version,
|
Version: version,
|
||||||
@ -63,7 +62,6 @@ func (app *App) InitConfig() {
|
|||||||
|
|
||||||
// StartApp starts the app
|
// StartApp starts the app
|
||||||
func (app *App) StartApp() {
|
func (app *App) StartApp() {
|
||||||
|
|
||||||
switch app.Config.Protocol {
|
switch app.Config.Protocol {
|
||||||
case "udp":
|
case "udp":
|
||||||
if app.Config.ServerMode {
|
if app.Config.ServerMode {
|
||||||
@ -125,13 +123,7 @@ func (app *App) StartApp() {
|
|||||||
} else {
|
} else {
|
||||||
tcp.StartClient(app.Iface, *app.Config)
|
tcp.StartClient(app.Iface, *app.Config)
|
||||||
}
|
}
|
||||||
case "http":
|
case "http", "https":
|
||||||
if app.Config.ServerMode {
|
|
||||||
h1.StartServer(app.Iface, *app.Config)
|
|
||||||
} else {
|
|
||||||
h1.StartClient(app.Iface, *app.Config)
|
|
||||||
}
|
|
||||||
case "https":
|
|
||||||
if app.Config.ServerMode {
|
if app.Config.ServerMode {
|
||||||
h1.StartServer(app.Iface, *app.Config)
|
h1.StartServer(app.Iface, *app.Config)
|
||||||
} else {
|
} else {
|
||||||
|
51
tun/tun.go
51
tun/tun.go
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CreateTun creates a tun interface
|
// CreateTun creates a tun interface
|
||||||
func CreateTun(config config.Config) (iface *water.Interface) {
|
func CreateTun(config config.Config) (iFace *water.Interface) {
|
||||||
c := water.Config{DeviceType: water.TUN}
|
c := water.Config{DeviceType: water.TUN}
|
||||||
c.PlatformSpecificParams = water.PlatformSpecificParams{}
|
c.PlatformSpecificParams = water.PlatformSpecificParams{}
|
||||||
os := runtime.GOOS
|
os := runtime.GOOS
|
||||||
@ -23,17 +23,17 @@ func CreateTun(config config.Config) (iface *water.Interface) {
|
|||||||
if config.DeviceName != "" {
|
if config.DeviceName != "" {
|
||||||
c.PlatformSpecificParams.Name = config.DeviceName
|
c.PlatformSpecificParams.Name = config.DeviceName
|
||||||
}
|
}
|
||||||
iface, err := water.New(c)
|
iFace, err := water.New(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("failed to create tun interface:", err)
|
log.Fatalln("failed to create tun interface:", err)
|
||||||
}
|
}
|
||||||
log.Printf("interface created %v", iface.Name())
|
log.Printf("interface created %v", iFace.Name())
|
||||||
setRoute(config, iface)
|
setRoute(config, iFace)
|
||||||
return iface
|
return iFace
|
||||||
}
|
}
|
||||||
|
|
||||||
// setRoute sets the system routes
|
// setRoute sets the system routes
|
||||||
func setRoute(config config.Config, iface *water.Interface) {
|
func setRoute(config config.Config, iFace *water.Interface) {
|
||||||
ip, _, err := net.ParseCIDR(config.CIDR)
|
ip, _, err := net.ParseCIDR(config.CIDR)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("error cidr %v", config.CIDR)
|
log.Panicf("error cidr %v", config.CIDR)
|
||||||
@ -44,42 +44,41 @@ func setRoute(config config.Config, iface *water.Interface) {
|
|||||||
}
|
}
|
||||||
os := runtime.GOOS
|
os := runtime.GOOS
|
||||||
if os == "linux" {
|
if os == "linux" {
|
||||||
netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iface.Name(), "mtu", strconv.Itoa(config.MTU))
|
netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iFace.Name(), "mtu", strconv.Itoa(config.MTU))
|
||||||
netutil.ExecCmd("/sbin/ip", "addr", "add", config.CIDR, "dev", iface.Name())
|
netutil.ExecCmd("/sbin/ip", "addr", "add", config.CIDR, "dev", iFace.Name())
|
||||||
netutil.ExecCmd("/sbin/ip", "-6", "addr", "add", config.CIDRv6, "dev", iface.Name())
|
netutil.ExecCmd("/sbin/ip", "-6", "addr", "add", config.CIDRv6, "dev", iFace.Name())
|
||||||
netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iface.Name(), "up")
|
netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iFace.Name(), "up")
|
||||||
if !config.ServerMode && config.GlobalMode {
|
if !config.ServerMode && config.GlobalMode {
|
||||||
physicalIface := netutil.GetInterface()
|
physicaliFace := netutil.GetInterface()
|
||||||
serverAddrIP := netutil.LookupServerAddrIP(config.ServerAddr)
|
serverAddrIP := netutil.LookupServerAddrIP(config.ServerAddr)
|
||||||
if physicalIface != "" && serverAddrIP != nil {
|
if physicaliFace != "" && serverAddrIP != nil {
|
||||||
if config.LocalGateway != "" {
|
if config.LocalGateway != "" {
|
||||||
netutil.ExecCmd("/sbin/ip", "route", "add", "0.0.0.0/1", "dev", iface.Name())
|
netutil.ExecCmd("/sbin/ip", "route", "add", "0.0.0.0/1", "dev", iFace.Name())
|
||||||
netutil.ExecCmd("/sbin/ip", "route", "add", "128.0.0.0/1", "dev", iface.Name())
|
netutil.ExecCmd("/sbin/ip", "route", "add", "128.0.0.0/1", "dev", iFace.Name())
|
||||||
if serverAddrIP.To4() != nil {
|
if serverAddrIP.To4() != nil {
|
||||||
netutil.ExecCmd("/sbin/ip", "route", "add", serverAddrIP.To4().String()+"/32", "via", config.LocalGateway, "dev", physicalIface)
|
netutil.ExecCmd("/sbin/ip", "route", "add", serverAddrIP.To4().String()+"/32", "via", config.LocalGateway, "dev", physicaliFace)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.LocalGatewayv6 != "" {
|
if config.LocalGatewayv6 != "" {
|
||||||
netutil.ExecCmd("/sbin/ip", "-6", "route", "add", "::/1", "dev", iface.Name())
|
netutil.ExecCmd("/sbin/ip", "-6", "route", "add", "::/1", "dev", iFace.Name())
|
||||||
if serverAddrIP.To16() != nil {
|
if serverAddrIP.To16() != nil {
|
||||||
netutil.ExecCmd("/sbin/ip", "-6", "route", "add", serverAddrIP.To16().String()+"/128", "via", config.LocalGatewayv6, "dev", physicalIface)
|
netutil.ExecCmd("/sbin/ip", "-6", "route", "add", serverAddrIP.To16().String()+"/128", "via", config.LocalGatewayv6, "dev", physicaliFace)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if os == "darwin" {
|
} else if os == "darwin" {
|
||||||
netutil.ExecCmd("ifconfig", iface.Name(), "inet", ip.String(), config.ServerIP, "up")
|
netutil.ExecCmd("ifconfig", iFace.Name(), "inet", ip.String(), config.ServerIP, "up")
|
||||||
netutil.ExecCmd("ifconfig", iface.Name(), "inet6", ipv6.String(), config.ServerIPv6, "up")
|
netutil.ExecCmd("ifconfig", iFace.Name(), "inet6", ipv6.String(), config.ServerIPv6, "up")
|
||||||
if !config.ServerMode && config.GlobalMode {
|
if !config.ServerMode && config.GlobalMode {
|
||||||
physicalIface := netutil.GetInterface()
|
physicaliFace := netutil.GetInterface()
|
||||||
serverAddrIP := netutil.LookupServerAddrIP(config.ServerAddr)
|
serverAddrIP := netutil.LookupServerAddrIP(config.ServerAddr)
|
||||||
if physicalIface != "" && serverAddrIP != nil {
|
if physicaliFace != "" && serverAddrIP != nil {
|
||||||
if config.LocalGateway != "" {
|
if config.LocalGateway != "" {
|
||||||
netutil.ExecCmd("route", "add", "default", config.ServerIP)
|
netutil.ExecCmd("route", "add", "default", config.ServerIP)
|
||||||
netutil.ExecCmd("route", "change", "default", config.ServerIP)
|
netutil.ExecCmd("route", "change", "default", config.ServerIP)
|
||||||
netutil.ExecCmd("route", "add", "0.0.0.0/1", "-interface", iface.Name())
|
netutil.ExecCmd("route", "add", "0.0.0.0/1", "-interface", iFace.Name())
|
||||||
netutil.ExecCmd("route", "add", "128.0.0.0/1", "-interface", iface.Name())
|
netutil.ExecCmd("route", "add", "128.0.0.0/1", "-interface", iFace.Name())
|
||||||
if serverAddrIP.To4() != nil {
|
if serverAddrIP.To4() != nil {
|
||||||
netutil.ExecCmd("route", "add", serverAddrIP.To4().String(), config.LocalGateway)
|
netutil.ExecCmd("route", "add", serverAddrIP.To4().String(), config.LocalGateway)
|
||||||
}
|
}
|
||||||
@ -87,7 +86,7 @@ func setRoute(config config.Config, iface *water.Interface) {
|
|||||||
if config.LocalGatewayv6 != "" {
|
if config.LocalGatewayv6 != "" {
|
||||||
netutil.ExecCmd("route", "add", "-inet6", "default", config.ServerIPv6)
|
netutil.ExecCmd("route", "add", "-inet6", "default", config.ServerIPv6)
|
||||||
netutil.ExecCmd("route", "change", "-inet6", "default", config.ServerIPv6)
|
netutil.ExecCmd("route", "change", "-inet6", "default", config.ServerIPv6)
|
||||||
netutil.ExecCmd("route", "add", "-inet6", "::/1", "-interface", iface.Name())
|
netutil.ExecCmd("route", "add", "-inet6", "::/1", "-interface", iFace.Name())
|
||||||
if serverAddrIP.To16() != nil {
|
if serverAddrIP.To16() != nil {
|
||||||
netutil.ExecCmd("route", "add", "-inet6", serverAddrIP.To16().String(), config.LocalGatewayv6)
|
netutil.ExecCmd("route", "add", "-inet6", serverAddrIP.To16().String(), config.LocalGatewayv6)
|
||||||
}
|
}
|
||||||
@ -117,7 +116,7 @@ func setRoute(config config.Config, iface *water.Interface) {
|
|||||||
} else {
|
} else {
|
||||||
log.Printf("not support os %v", os)
|
log.Printf("not support os %v", os)
|
||||||
}
|
}
|
||||||
log.Printf("interface configured %v", iface.Name())
|
log.Printf("interface configured %v", iFace.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetRoute resets the system routes
|
// ResetRoute resets the system routes
|
||||||
|
Loading…
Reference in New Issue
Block a user