mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-12 04:43:22 +08:00
fix tun for windows
This commit is contained in:
8
lower/config_unix.go
Normal file
8
lower/config_unix.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
//go:build !windows
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package lower
|
||||||
|
|
||||||
|
import "github.com/songgao/water"
|
||||||
|
|
||||||
|
var tuncfg = water.Config{DeviceType: water.TUN}
|
||||||
15
lower/config_windows.go
Normal file
15
lower/config_windows.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//go:build windows
|
||||||
|
// +build windows
|
||||||
|
|
||||||
|
package lower
|
||||||
|
|
||||||
|
import "github.com/songgao/water"
|
||||||
|
|
||||||
|
var tuncfg = water.Config{
|
||||||
|
DeviceType: water.TUN,
|
||||||
|
PlatformSpecificParams: water.PlatformSpecificParams{
|
||||||
|
ComponentID: "root\\tap0901",
|
||||||
|
InterfaceName: "OpenVPN TAP-Windows6",
|
||||||
|
Network: "192.168.233.0/24",
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -23,7 +23,7 @@ type NIC struct {
|
|||||||
// NewNIC 新建 TUN 网络接口卡
|
// NewNIC 新建 TUN 网络接口卡
|
||||||
// 网卡地址为 ip, 所属子网为 subnet
|
// 网卡地址为 ip, 所属子网为 subnet
|
||||||
func NewNIC(ip, subnet string) (n *NIC) {
|
func NewNIC(ip, subnet string) (n *NIC) {
|
||||||
ifce, err := water.New(water.Config{DeviceType: water.TUN})
|
ifce, err := water.New(tuncfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,22 @@
|
|||||||
|
|
||||||
package lower
|
package lower
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
func (n *NIC) prepare() {
|
func (n *NIC) prepare() {
|
||||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "mtu", "1500")
|
_, ipn, err := net.ParseCIDR(n.subnet)
|
||||||
execute("/sbin/ip", "addr", "add", ip, "dev", ifcename)
|
if err != nil {
|
||||||
execute("/sbin/ip", "route", "add", subnet, "dev", ifcename)
|
panic(err)
|
||||||
|
}
|
||||||
|
execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip+"\" mask=\""+(net.IP)(ipn.Mask).String()+"\" gateway=none")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NIC) Up() {
|
func (n *NIC) Up() {
|
||||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "up")
|
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "enabled")
|
||||||
|
// don't need to bring up the device by hand
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NIC) Down() {
|
func (n *NIC) Down() {
|
||||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "down")
|
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "disabled")
|
||||||
|
// don't need to bring up the device by hand
|
||||||
}
|
}
|
||||||
|
|||||||
70
main_win.go
Normal file
70
main_win.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
//go:build windows
|
||||||
|
// +build windows
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// windows 带颜色 log 自定义格式
|
||||||
|
logrus.SetFormatter(&LogFormat{})
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
colorCodePanic = "\x1b[1;31m" // color.Style{color.Bold, color.Red}.String()
|
||||||
|
colorCodeFatal = "\x1b[1;31m" // color.Style{color.Bold, color.Red}.String()
|
||||||
|
colorCodeError = "\x1b[31m" // color.Style{color.Red}.String()
|
||||||
|
colorCodeWarn = "\x1b[33m" // color.Style{color.Yellow}.String()
|
||||||
|
colorCodeInfo = "\x1b[37m" // color.Style{color.White}.String()
|
||||||
|
colorCodeDebug = "\x1b[32m" // color.Style{color.Green}.String()
|
||||||
|
colorCodeTrace = "\x1b[36m" // color.Style{color.Cyan}.String()
|
||||||
|
colorReset = "\x1b[0m"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogFormat specialize for zbp
|
||||||
|
type LogFormat struct{}
|
||||||
|
|
||||||
|
// Format implements logrus.Formatter
|
||||||
|
func (f LogFormat) Format(entry *logrus.Entry) ([]byte, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
buf.WriteString(getLogLevelColorCode(entry.Level))
|
||||||
|
|
||||||
|
buf.WriteByte('[')
|
||||||
|
buf.WriteString(strings.ToUpper(entry.Level.String()))
|
||||||
|
buf.WriteString("] ")
|
||||||
|
buf.WriteString(entry.Message)
|
||||||
|
buf.WriteString(" \n")
|
||||||
|
|
||||||
|
buf.WriteString(colorReset)
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLogLevelColorCode 获取日志等级对应色彩code
|
||||||
|
func getLogLevelColorCode(level logrus.Level) string {
|
||||||
|
switch level {
|
||||||
|
case logrus.PanicLevel:
|
||||||
|
return colorCodePanic
|
||||||
|
case logrus.FatalLevel:
|
||||||
|
return colorCodeFatal
|
||||||
|
case logrus.ErrorLevel:
|
||||||
|
return colorCodeError
|
||||||
|
case logrus.WarnLevel:
|
||||||
|
return colorCodeWarn
|
||||||
|
case logrus.InfoLevel:
|
||||||
|
return colorCodeInfo
|
||||||
|
case logrus.DebugLevel:
|
||||||
|
return colorCodeDebug
|
||||||
|
case logrus.TraceLevel:
|
||||||
|
return colorCodeTrace
|
||||||
|
|
||||||
|
default:
|
||||||
|
return colorCodeInfo
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user