diff --git a/configs/config.go b/configs/config.go new file mode 100644 index 0000000..95e411b --- /dev/null +++ b/configs/config.go @@ -0,0 +1,86 @@ +package configs + +type ( + Config struct { + ServerAddress string + ServerPort int + Username string + Password string + DisableServerConfig bool + DisableZJUConfig bool + DisableZJUDNS bool + DisableMultiLine bool + ProxyAll bool + SocksBind string + SocksUser string + SocksPasswd string + HTTPBind string + ShadowsocksURL string + TUNMode bool + AddRoute bool + DNSTTL uint64 + DisableKeepAlive bool + ZJUDNSServer string + SecondaryDNSServer string + DNSServerBind string + DNSHijack bool + DebugDump bool + PortForwardingList []SinglePortForwarding + CustomDNSList []SingleCustomDNS + CustomProxyDomain []string + TwfID string + } + + SinglePortForwarding struct { + NetworkType string + BindAddress string + RemoteAddress string + } + + SingleCustomDNS struct { + HostName string `toml:"host_name"` + IP string `toml:"ip"` + } +) + +type ( + ConfigTOML struct { + ServerAddress *string `toml:"server_address"` + ServerPort *int `toml:"server_port"` + Username *string `toml:"username"` + Password *string `toml:"password"` + DisableServerConfig *bool `toml:"disable_server_config"` + DisableZJUConfig *bool `toml:"disable_zju_config"` + DisableZJUDNS *bool `toml:"disable_zju_dns"` + DisableMultiLine *bool `toml:"disable_multi_line"` + ProxyAll *bool `toml:"proxy_all"` + SocksBind *string `toml:"socks_bind"` + SocksUser *string `toml:"socks_user"` + SocksPasswd *string `toml:"socks_passwd"` + HTTPBind *string `toml:"http_bind"` + ShadowsocksURL *string `toml:"shadowsocks_url"` + TUNMode *bool `toml:"tun_mode"` + AddRoute *bool `toml:"add_route"` + DNSTTL *uint64 `toml:"dns_ttl"` + DisableKeepAlive *bool `toml:"disable_keep_alive"` + ZJUDNSServer *string `toml:"zju_dns_server"` + SecondaryDNSServer *string `toml:"secondary_dns_server"` + DNSServerBind *string `toml:"dns_server_bind"` + DNSHijack *bool `toml:"dns_hijack"` + DebugDump *bool `toml:"debug_dump"` + PortForwarding []SinglePortForwardingTOML `toml:"port_forwarding"` + CustomDNS []SingleCustomDNSTOML `toml:"custom_dns"` + CustomProxyDomain []string `toml:"custom_proxy_domain"` + } + + SinglePortForwardingTOML struct { + NetworkType *string `toml:"network_type"` + BindAddress *string `toml:"bind_address"` + RemoteAddress *string `toml:"remote_address"` + } + + SingleCustomDNSTOML struct { + HostName *string `toml:"host_name"` + IP *string `toml:"ip"` + } +) diff --git a/init.go b/init.go index 336f927..70939ff 100644 --- a/init.go +++ b/init.go @@ -5,96 +5,12 @@ import ( "flag" "fmt" "github.com/BurntSushi/toml" + "github.com/mythologyli/zju-connect/configs" "os" "regexp" "strings" ) -type ( - Config struct { - ServerAddress string - ServerPort int - Username string - Password string - DisableServerConfig bool - DisableZJUConfig bool - DisableZJUDNS bool - DisableMultiLine bool - ProxyAll bool - SocksBind string - SocksUser string - SocksPasswd string - HTTPBind string - ShadowsocksURL string - TUNMode bool - AddRoute bool - DNSTTL uint64 - DisableKeepAlive bool - ZJUDNSServer string - SecondaryDNSServer string - DNSServerBind string - DNSHijack bool - DebugDump bool - PortForwardingList []SinglePortForwarding - CustomDNSList []SingleCustomDNS - CustomProxyDomain []string - TwfID string - } - - SinglePortForwarding struct { - NetworkType string - BindAddress string - RemoteAddress string - } - - SingleCustomDNS struct { - HostName string `toml:"host_name"` - IP string `toml:"ip"` - } -) - -type ( - ConfigTOML struct { - ServerAddress *string `toml:"server_address"` - ServerPort *int `toml:"server_port"` - Username *string `toml:"username"` - Password *string `toml:"password"` - DisableServerConfig *bool `toml:"disable_server_config"` - DisableZJUConfig *bool `toml:"disable_zju_config"` - DisableZJUDNS *bool `toml:"disable_zju_dns"` - DisableMultiLine *bool `toml:"disable_multi_line"` - ProxyAll *bool `toml:"proxy_all"` - SocksBind *string `toml:"socks_bind"` - SocksUser *string `toml:"socks_user"` - SocksPasswd *string `toml:"socks_passwd"` - HTTPBind *string `toml:"http_bind"` - ShadowsocksURL *string `toml:"shadowsocks_url"` - TUNMode *bool `toml:"tun_mode"` - AddRoute *bool `toml:"add_route"` - DNSTTL *uint64 `toml:"dns_ttl"` - DisableKeepAlive *bool `toml:"disable_keep_alive"` - ZJUDNSServer *string `toml:"zju_dns_server"` - SecondaryDNSServer *string `toml:"secondary_dns_server"` - DNSServerBind *string `toml:"dns_server_bind"` - DNSHijack *bool `toml:"dns_hijack"` - DebugDump *bool `toml:"debug_dump"` - PortForwarding []SinglePortForwardingTOML `toml:"port_forwarding"` - CustomDNS []SingleCustomDNSTOML `toml:"custom_dns"` - CustomProxyDomain []string `toml:"custom_proxy_domain"` - } - - SinglePortForwardingTOML struct { - NetworkType *string `toml:"network_type"` - BindAddress *string `toml:"bind_address"` - RemoteAddress *string `toml:"remote_address"` - } - - SingleCustomDNSTOML struct { - HostName *string `toml:"host_name"` - IP *string `toml:"ip"` - } -) - func getTOMLVal[T int | uint64 | string | bool](valPointer *T, defaultVal T) T { if valPointer == nil { return defaultVal @@ -103,8 +19,8 @@ func getTOMLVal[T int | uint64 | string | bool](valPointer *T, defaultVal T) T { } } -func parseTOMLConfig(configFile string, conf *Config) error { - var confTOML ConfigTOML +func parseTOMLConfig(configFile string, conf *configs.Config) error { + var confTOML configs.ConfigTOML _, err := toml.DecodeFile(configFile, &confTOML) if err != nil { @@ -148,7 +64,7 @@ func parseTOMLConfig(configFile string, conf *Config) error { return errors.New("ZJU Connect: remote address is not set") } - conf.PortForwardingList = append(conf.PortForwardingList, SinglePortForwarding{ + conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{ NetworkType: *singlePortForwarding.NetworkType, BindAddress: *singlePortForwarding.BindAddress, RemoteAddress: *singlePortForwarding.RemoteAddress, @@ -165,7 +81,7 @@ func parseTOMLConfig(configFile string, conf *Config) error { return errors.New("ZJU Connect: IP is not set") } - conf.CustomDNSList = append(conf.CustomDNSList, SingleCustomDNS{ + conf.CustomDNSList = append(conf.CustomDNSList, configs.SingleCustomDNS{ HostName: *singleCustomDns.HostName, IP: *singleCustomDns.IP, }) @@ -241,7 +157,7 @@ func init() { os.Exit(1) } - conf.PortForwardingList = append(conf.PortForwardingList, SinglePortForwarding{ + conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{ NetworkType: "tcp", BindAddress: addressStringList[0], RemoteAddress: addressStringList[1], @@ -258,7 +174,7 @@ func init() { os.Exit(1) } - conf.PortForwardingList = append(conf.PortForwardingList, SinglePortForwarding{ + conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{ NetworkType: "udp", BindAddress: addressStringList[0], RemoteAddress: addressStringList[1], @@ -275,7 +191,7 @@ func init() { os.Exit(1) } - conf.CustomDNSList = append(conf.CustomDNSList, SingleCustomDNS{ + conf.CustomDNSList = append(conf.CustomDNSList, configs.SingleCustomDNS{ HostName: dnsStringSplit[0], IP: dnsStringSplit[1], }) diff --git a/internal/hook_func/initial_func.go b/internal/hook_func/initial_func.go index bd68ebe..85245ed 100644 --- a/internal/hook_func/initial_func.go +++ b/internal/hook_func/initial_func.go @@ -2,10 +2,11 @@ package hook_func import ( "context" + "github.com/mythologyli/zju-connect/configs" "github.com/mythologyli/zju-connect/log" ) -type InitialFunc func(ctx context.Context) error +type InitialFunc func(ctx context.Context, config configs.Config) error type InitialItem struct { f InitialFunc name string @@ -22,11 +23,11 @@ func RegisterInitialFunc(execName string, fun InitialFunc) { }) } -func ExecInitialFunc(ctx context.Context) []error { +func ExecInitialFunc(ctx context.Context, config configs.Config) []error { var errList []error for _, item := range initialFuncList { log.Println("Exec func on initial:", item.name) - if err := item.f(ctx); err != nil { + if err := item.f(ctx, config); err != nil { errList = append(errList, err) log.Println("Exec func on initial ", item.name, "failed:", err) } else { diff --git a/internal/hook_func/initial_func_darwin.go b/internal/hook_func/initial_func_darwin.go index 1bdc451..6fca9f8 100644 --- a/internal/hook_func/initial_func_darwin.go +++ b/internal/hook_func/initial_func_darwin.go @@ -2,14 +2,27 @@ package hook_func import ( "context" + "errors" + "github.com/mythologyli/zju-connect/configs" "os" + "os/user" ) func init() { - RegisterInitialFunc("clean resolver file", func(ctx context.Context) error { + RegisterInitialFunc("clean resolver file", func(ctx context.Context, config configs.Config) error { // discard error _ = os.Remove("/etc/resolver/zju.edu.cn") _ = os.Remove("/etc/resolver/cc98.org") return nil }) + RegisterInitialFunc("check tun mode cap", func(ctx context.Context, config configs.Config) error { + // discard error + if config.TUNMode { + current, _ := user.Current() + if current.Uid != "0" { + return errors.New("请使用sudo运行TUN模式") + } + } + return nil + }) } diff --git a/internal/hook_func/initial_func_linux.go b/internal/hook_func/initial_func_linux.go index 3de122d..4267de1 100644 --- a/internal/hook_func/initial_func_linux.go +++ b/internal/hook_func/initial_func_linux.go @@ -1,5 +1,21 @@ package hook_func -func init() { +import ( + "context" + "github.com/mythologyli/zju-connect/configs" + "github.com/mythologyli/zju-connect/log" + "os/user" +) +func init() { + RegisterInitialFunc("check tun mode cap", func(ctx context.Context, config configs.Config) error { + // discard error + if config.TUNMode { + current, _ := user.Current() + if current.Uid != "0" { + log.Println("检测到TUN模式,但是当前用户不是root,可能会导致无法使用,如果遇到问题请使用sudo运行") + } + } + return nil + }) } diff --git a/main.go b/main.go index 2f03854..793e431 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "github.com/mythologyli/zju-connect/client" + "github.com/mythologyli/zju-connect/configs" "github.com/mythologyli/zju-connect/dial" "github.com/mythologyli/zju-connect/internal/hook_func" "github.com/mythologyli/zju-connect/log" @@ -21,7 +22,7 @@ import ( "syscall" ) -var conf Config +var conf configs.Config const zjuConnectVersion = "0.7.0" @@ -33,9 +34,9 @@ func main() { log.EnableDebug() } - if errs := hook_func.ExecInitialFunc(context.Background()); errs != nil { + if errs := hook_func.ExecInitialFunc(context.Background(), conf); errs != nil { for _, err := range errs { - log.Printf("Initial ZJU-Connect failed:", err) + log.Printf("Initial ZJU-Connect failed: %s", err) } os.Exit(1) } @@ -176,7 +177,7 @@ func main() { log.Println("Shutdown ZJU-Connect ......") if errs := hook_func.ExecTerminalFunc(context.Background()); errs != nil { for _, err := range errs { - log.Printf("Shutdown ZJU-Connect failed:", err) + log.Printf("Shutdown ZJU-Connect failed: %s", err) } } else { log.Println("Shutdown ZJU-Connect success, Bye~") diff --git a/main_tun.go b/main_tun.go index c7392df..0e1a78b 100644 --- a/main_tun.go +++ b/main_tun.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "github.com/mythologyli/zju-connect/client" + "github.com/mythologyli/zju-connect/configs" "github.com/mythologyli/zju-connect/dial" "github.com/mythologyli/zju-connect/internal/hook_func" "github.com/mythologyli/zju-connect/log" @@ -19,7 +20,7 @@ import ( "syscall" ) -var conf Config +var conf configs.Config const zjuConnectVersion = "0.7.0-tun-only" @@ -31,9 +32,9 @@ func main() { log.EnableDebug() } - if errs := hook_func.ExecInitialFunc(context.Background()); errs != nil { + if errs := hook_func.ExecInitialFunc(context.Background(), conf); errs != nil { for _, err := range errs { - log.Printf("Initial ZJU-Connect failed:", err) + log.Printf("Initial ZJU-Connect failed: %s", err) } os.Exit(1) } @@ -162,7 +163,7 @@ func main() { log.Println("Shutdown ZJU-Connect ......") if errs := hook_func.ExecTerminalFunc(context.Background()); errs != nil { for _, err := range errs { - log.Printf("Shutdown ZJU-Connect failed:", err) + log.Printf("Shutdown ZJU-Connect failed: %s", err) } } else { log.Println("Shutdown ZJU-Connect success, Bye~")